T-SQL 总结
SP0_AddLinkedServer.sql [创建Linked SQL Server ]
USE [master]
GO if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[SP_Temp_AddLinkedServer]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure dbo.SP_Temp_AddLinkedServer;
GO create procedure dbo.SP_Temp_AddLinkedServer
@@cloudServerIP nvarchar(250),
@@cloudServerSA nvarchar(50),
@@cloudSAPWD nvarchar(50)
AS
begin IF EXISTS(SELECT * FROM sys.servers WHERE name = @@cloudServerIP)
EXEC master.sys.sp_dropserver @@cloudServerIP,'droplogins' declare @TargetServer nvarchar(50)
declare @strcloudServerIP nvarchar(50)
declare @strcloudServerSA nvarchar(50)
declare @strcloudSAPWD nvarchar(50) set @strcloudServerIP = @@cloudServerIP
set @strcloudServerSA = @@cloudServerSA
set @strcloudSAPWD = @@cloudSAPWD
EXEC master.dbo.sp_addlinkedserver @server = @strcloudServerIP, @srvproduct=N'SQL Server'
/* For security reasons the linked server remote logins password is changed with ######## */
EXEC master.dbo.sp_addlinkedsrvlogin @rmtsrvname=@strcloudServerIP,@useself=N'False',@locallogin=NULL,@rmtuser=@strcloudServerSA ,@rmtpassword=@strcloudSAPWD EXEC master.dbo.sp_serveroption @server=@strcloudServerIP, @optname=N'collation compatible', @optvalue=N'false' EXEC master.dbo.sp_serveroption @server=@strcloudServerIP, @optname=N'data access', @optvalue=N'true' EXEC master.dbo.sp_serveroption @server=@strcloudServerIP, @optname=N'dist', @optvalue=N'false' EXEC master.dbo.sp_serveroption @server=@strcloudServerIP, @optname=N'pub', @optvalue=N'false' EXEC master.dbo.sp_serveroption @server=@strcloudServerIP, @optname=N'rpc', @optvalue=N'false' EXEC master.dbo.sp_serveroption @server=@strcloudServerIP, @optname=N'rpc out', @optvalue=N'false' EXEC master.dbo.sp_serveroption @server=@strcloudServerIP, @optname=N'sub', @optvalue=N'false' EXEC master.dbo.sp_serveroption @server=@strcloudServerIP, @optname=N'connect timeout', @optvalue=N'' EXEC master.dbo.sp_serveroption @server=@strcloudServerIP, @optname=N'collation name', @optvalue=null EXEC master.dbo.sp_serveroption @server=@strcloudServerIP, @optname=N'lazy schema validation', @optvalue=N'false' EXEC master.dbo.sp_serveroption @server=@strcloudServerIP, @optname=N'query timeout', @optvalue=N'' EXEC master.dbo.sp_serveroption @server=@strcloudServerIP, @optname=N'use remote collation', @optvalue=N'true' EXEC master.dbo.sp_serveroption @server=@strcloudServerIP, @optname=N'remote proc transaction promotion', @optvalue=N'true' end --use master exec SP_Temp_AddLinkedServer '10.1.12.110','sa','Sequoia2012'
SP1_LoadTablesName.sql [SQL读取文本文件,并插入表内。]
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[SP_Temp_GetCompareNameList]') and OBJECTPROPERTY(id, N'IsProcedure') = 1) drop procedure dbo.SP_Temp_GetCompareNameList;
GO create procedure dbo.SP_Temp_GetCompareNameList
@@filePath varchar(250)
--@@totalAmount numeric(15,3)=0 output
AS BEGIN
declare @TableNameConfigureFile varchar(250)
declare @SQLExeStr varchar(300) set @TableNameConfigureFile = @@filePath /* Remove temp table */
if object_id('Temp_Table_CompareTableName') is not null
Begin drop table Temp_Table_CompareTableName End exec ('CREATE TABLE Temp_Table_CompareTableName(strName varchar(100))') set @SQLExeStr = 'bulk INSERT Temp_Table_CompareTableName
from ' + '''' + @TableNameConfigureFile + ''''+
' with(
FIELDTERMINATOR = ''\t'',
ROWTERMINATOR = ''\n''
) ' exec (@SQLExeStr) --select *from #CompareTableName
--select *from tempdb..#CompareTableName
--print @SQLExeStr
-- exec SP_Temp_GetCompareNameList 'C:\ComparedTableList.txt'
--select *from CompareTableName
end
SP2_GetTableColumns.sql [调用系统表功能,读取某一表的所有列信息。]
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[SP_Temp_GetTableColumns]') and OBJECTPROPERTY(id, N'IsProcedure') = 1) drop procedure dbo.SP_Temp_GetTableColumns;
GO create procedure dbo.SP_Temp_GetTableColumns
@@tableName nvarchar(250) AS BEGIN
declare @strTableName varchar(250)
declare @SQLExeStr varchar(4000) set @strTableName = @@tableName /* Remove temp table */
if object_id('Temp_Table_TableColumns') is not null
Begin
drop table Temp_Table_TableColumns
End
set @SQLExeStr = 'Select name into Temp_Table_TableColumns from SysColumns Where id=Object_Id(''' + @strTableName + ''')
and name <> ''AuditTimestamp''
and name <> ''CreatedTime''
and name <> ''InstallTime''
and name <> ''StartTime''
and name <> ''ModificationTime''
and name <> ''LockTime''
and name <> ''LastActivityDate''
and name <> ''MasterPageImage''
and name <> ''LastLoginDate''
'
--print @SQLExeStr
exec (@SQLExeStr)
end --exec SP_Temp_GetTableColumns 'dbo.RexStatementTransaction'
--select *from TableColumns
SP3_CompareTwoTable.sql [ 使用SQL Except 语句,比较同结构表内容 ]
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[SP_Temp_CompareTables]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure dbo.SP_Temp_CompareTables;
GO create procedure dbo.SP_Temp_CompareTables
@@compareTableName nvarchar(250),
@@compareRes int output,
@@cloudServerIP nvarchar(50) ,
@@cloudDBName nvarchar(50) AS BEGIN declare @TableName nvarchar(250)
declare @SQLExeStr nvarchar(300)
declare @strSQLLoadTableColumns nvarchar(300)
declare @strcloudServerIP nvarchar(300)
declare @strcloudDBName nvarchar(300) declare @selColumns nvarchar(3000)
declare @finalSelColumns nvarchar(3000)
DECLARE @count int -- column numbers
DECLARE @testResult int /*Test Result flag*/ set @TableName = @@compareTableName
set @strcloudServerIP = @@cloudServerIP
set @strcloudDBName = @@cloudDBName
set @testResult = 1 /*Default Value as false*/ /*Print out the info of comparing table*/
select dbo.FN_Temp_LogInfo( 'Testing The Table:' +@TableName) set @strSQLLoadTableColumns = 'exec SP_Temp_GetTableColumns ' + '''' + @TableName + ''''
exec(@strSQLLoadTableColumns) -- call SP for insert the table's column to our test using table
--SELECT @count= COUNT(1) FROM Temp_Table_TableColumns
select @selColumns = isnull(@selColumns,'') + ',' + name from Temp_Table_TableColumns where name=name
--print @selColumns
set @finalSelColumns = SubString(@selColumns,2,LEN(@selColumns)) BEGIN
declare @sqlCloudQueryStr nvarchar(3000)
declare @sqlGroundQueryStr nvarchar(3000)
declare @finalSqlExec nvarchar(4000)
declare @withStart nvarchar(100)
declare @withEnd nvarchar(100) set @withStart = 'select @a=count(1)' + ' from ('
set @withEnd = ')T'
/* Get the final exec SQL compare statement*/
set @sqlGroundQueryStr = ' select ' + @finalSelColumns + ' from ' + @TableName + ' except '
set @sqlCloudQueryStr = 'select *from OPENQUERY( [' + @strcloudServerIP + '], ''select ' + @finalSelColumns + ' from ' + @strcloudDBName +'.' + @TableName + ''''+ ')'
set @finalSqlExec = @withStart + @sqlGroundQueryStr + @sqlCloudQueryStr + @withEnd declare @num int
BEGIN TRY
EXECUTE sp_executesql @finalSqlExec,N'@a int output',@num output IF(@num > 0)
Begin
set @testResult = 1
/*Set the case info*/
select dbo.FN_Temp_LogInfo(' >>>>>> Comparing Exception <<<<<<' )
select dbo.FN_Temp_LogInfo(' Exception Table:' + @TableName )
select dbo.FN_Temp_LogInfo(' Exception Query:' + @finalSqlExec)
select dbo.FN_Temp_LogInfo(' ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^')
End
else
set @testResult = 0
End TRY
BEGIN CATCH
set @testResult = 1
select dbo.FN_Temp_LogInfo(' >>>>>> SQL Execution Exception <<<<<<' )
select dbo.FN_Temp_LogInfo(' Exception Table:' + @TableName )
select dbo.FN_Temp_LogInfo(' Exception Query:' + @finalSqlExec)
select dbo.FN_Temp_LogInfo(' SQL Error:' + ERROR_MESSAGE())
select dbo.FN_Temp_LogInfo(' ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^')
End Catch
END set @@compareRes = @testResult
/*Set the case result*/
select dbo.[FN_Temp_LogRes](@TableName,@testResult)
return @@compareRes
end --declare @iTemp int
--exec SP_Temp_CompareTables 'ApxAdaptorDb101.APXAdaptor.Session', @iTemp output,'10.1.12.110', 'ApxAdaptorDb101'
T-SQL 总结的更多相关文章
- 最近帮客户实施的基于SQL Server AlwaysOn跨机房切换项目
最近帮客户实施的基于SQL Server AlwaysOn跨机房切换项目 最近一个来自重庆的客户找到走起君,客户的业务是做移动互联网支付,是微信支付收单渠道合作伙伴,数据库里存储的是支付流水和交易流水 ...
- SQL Server 大数据搬迁之文件组备份还原实战
一.本文所涉及的内容(Contents) 本文所涉及的内容(Contents) 背景(Contexts) 解决方案(Solution) 搬迁步骤(Procedure) 搬迁脚本(SQL Codes) ...
- Sql Server系列:分区表操作
1. 分区表简介 分区表在逻辑上是一个表,而物理上是多个表.从用户角度来看,分区表和普通表是一样的.使用分区表的主要目的是为改善大型表以及具有多个访问模式的表的可伸缩性和可管理性. 分区表是把数据按设 ...
- SQL Server中的高可用性(2)----文件与文件组
在谈到SQL Server的高可用性之前,我们首先要谈一谈单实例的高可用性.在单实例的高可用性中,不可忽略的就是文件和文件组的高可用性.SQL Server允许在某些文件损坏或离线的情况下,允 ...
- EntityFramework Core Raw SQL
前言 本节我们来讲讲EF Core中的原始查询,目前在项目中对于简单的查询直接通过EF就可以解决,但是涉及到多表查询时为了一步到位就采用了原始查询的方式进行.下面我们一起来看看. EntityFram ...
- 从0开始搭建SQL Server AlwaysOn 第一篇(配置域控)
从0开始搭建SQL Server AlwaysOn 第一篇(配置域控) 第一篇http://www.cnblogs.com/lyhabc/p/4678330.html第二篇http://www.cnb ...
- 从0开始搭建SQL Server AlwaysOn 第二篇(配置故障转移集群)
从0开始搭建SQL Server AlwaysOn 第二篇(配置故障转移集群) 第一篇http://www.cnblogs.com/lyhabc/p/4678330.html第二篇http://www ...
- 从0开始搭建SQL Server AlwaysOn 第三篇(配置AlwaysOn)
从0开始搭建SQL Server AlwaysOn 第三篇(配置AlwaysOn) 第一篇http://www.cnblogs.com/lyhabc/p/4678330.html第二篇http://w ...
- 从0开始搭建SQL Server AlwaysOn 第四篇(配置异地机房节点)
从0开始搭建SQL Server AlwaysOn 第四篇(配置异地机房节点) 第一篇http://www.cnblogs.com/lyhabc/p/4678330.html第二篇http://www ...
- SQL Server on Linux 理由浅析
SQL Server on Linux 理由浅析 今天的爆炸性新闻<SQL Server on Linux>基本上在各大科技媒体上刷屏了 大家看到这个新闻都觉得非常震精,而美股,今天微软开 ...
随机推荐
- error: device offline - waiting for device -
解决方法:重启服务 一.关闭 adb kill-server 二.启动 adb start-server 三.连接 adb connect 192.168.1.10 四.查看设备 adb device ...
- Rails 5 Test Prescriptions 第5章 Testing Models
Rails,model层包含业务逻辑和储存逻辑.其中储存逻辑被ActiveRecord处理. 在model中,不是每件事都必须是ActiveRecord对象.model layer可以包含各种服务,对 ...
- sqlserver版本分类下载以及各个版本之间的区别是什么
很多用visual studio做开发的朋友经常会用到sqlserver数据库,但是往往在选择的时候就不知道该使用哪个版本了,今天亦是美网络就给大家分享一下sqlserver各个版本之间的区别,以及各 ...
- 011PHP文件处理——文件处理 文件内容分页操作类
<?php /** * 文件内容分页操作类: */ //访问地址:http://basicphp.com/006file/011.php?&page=1 class StrPage { ...
- 微信小程序navigateTo /redirectTo/navigateBack 三者区别
navigateTo 不会将旧页面出栈: redirectTo 会将旧页面出栈,再将需要跳转到的页面入栈: navigateBack 则是将页面栈最后一个元素出栈,因此倒数第二个元素会成为最后一个元素 ...
- hdu 3694 10 福州 现场 E - Fermat Point in Quadrangle 费马点 计算几何 难度:1
In geometry the Fermat point of a triangle, also called Torricelli point, is a point such that the t ...
- bzoj2442
题解: 单调队列+dp f[i]=max(f[j-1]+sum[i]-sum[j]) 然后维护f[j-1]-sum[j]单调性 代码: #include<bits/stdc++.h> us ...
- HDFS读写流程
01.并行读取 02.逐个节点写入
- Shell排序算法和合并排序算法
Shell排序(希尔排序)算法Shell排序严格来说基于插入排序的思想,其又称为希尔排序或者缩小增量排序. Shell排序的流程:1.将由n个元素的数组分成n/2个数字序列,第1个数据和第n/2+1个 ...
- ImportError: No module named 'serial'
/******************************************************************************** * ImportError: No ...