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 总结的更多相关文章

  1. 最近帮客户实施的基于SQL Server AlwaysOn跨机房切换项目

    最近帮客户实施的基于SQL Server AlwaysOn跨机房切换项目 最近一个来自重庆的客户找到走起君,客户的业务是做移动互联网支付,是微信支付收单渠道合作伙伴,数据库里存储的是支付流水和交易流水 ...

  2. SQL Server 大数据搬迁之文件组备份还原实战

    一.本文所涉及的内容(Contents) 本文所涉及的内容(Contents) 背景(Contexts) 解决方案(Solution) 搬迁步骤(Procedure) 搬迁脚本(SQL Codes) ...

  3. Sql Server系列:分区表操作

    1. 分区表简介 分区表在逻辑上是一个表,而物理上是多个表.从用户角度来看,分区表和普通表是一样的.使用分区表的主要目的是为改善大型表以及具有多个访问模式的表的可伸缩性和可管理性. 分区表是把数据按设 ...

  4. SQL Server中的高可用性(2)----文件与文件组

        在谈到SQL Server的高可用性之前,我们首先要谈一谈单实例的高可用性.在单实例的高可用性中,不可忽略的就是文件和文件组的高可用性.SQL Server允许在某些文件损坏或离线的情况下,允 ...

  5. EntityFramework Core Raw SQL

    前言 本节我们来讲讲EF Core中的原始查询,目前在项目中对于简单的查询直接通过EF就可以解决,但是涉及到多表查询时为了一步到位就采用了原始查询的方式进行.下面我们一起来看看. EntityFram ...

  6. 从0开始搭建SQL Server AlwaysOn 第一篇(配置域控)

    从0开始搭建SQL Server AlwaysOn 第一篇(配置域控) 第一篇http://www.cnblogs.com/lyhabc/p/4678330.html第二篇http://www.cnb ...

  7. 从0开始搭建SQL Server AlwaysOn 第二篇(配置故障转移集群)

    从0开始搭建SQL Server AlwaysOn 第二篇(配置故障转移集群) 第一篇http://www.cnblogs.com/lyhabc/p/4678330.html第二篇http://www ...

  8. 从0开始搭建SQL Server AlwaysOn 第三篇(配置AlwaysOn)

    从0开始搭建SQL Server AlwaysOn 第三篇(配置AlwaysOn) 第一篇http://www.cnblogs.com/lyhabc/p/4678330.html第二篇http://w ...

  9. 从0开始搭建SQL Server AlwaysOn 第四篇(配置异地机房节点)

    从0开始搭建SQL Server AlwaysOn 第四篇(配置异地机房节点) 第一篇http://www.cnblogs.com/lyhabc/p/4678330.html第二篇http://www ...

  10. SQL Server on Linux 理由浅析

    SQL Server on Linux 理由浅析 今天的爆炸性新闻<SQL Server on Linux>基本上在各大科技媒体上刷屏了 大家看到这个新闻都觉得非常震精,而美股,今天微软开 ...

随机推荐

  1. codeforces 98 div2 C.History 水题

    C. History time limit per test 2 seconds memory limit per test 256 megabytes input standard input ou ...

  2. shell 输入不显示在监视器上

    #!/bin/bash read -s -p "Enter your password:" pass echo "your password is $pass" ...

  3. python 在列表中添加元组元素,按照元组第一个值进行排序

    >>> import bisect >>> scores = [(, , , , 'python')] >>> bisect.insort(sco ...

  4. Java IO流-Properties

    2017-11-05 21:37:50 Properties Properties:Properties 类表示了一个持久的属性集.Properties 可保存在流中或从流中加载.属性列表中每个键及其 ...

  5. Android学习必备--java工具15个

    Weka .Weka集成了数据挖掘工作的机器学习算法.这些算法可以直接应用于一个数据集上或者你可以自己编写代码来调用.Weka包括一系列的工具,如数据预处理.分类.回归.聚类.关联规则以及可视化. M ...

  6. 浅析promise

    Promise是一个构造函数,可以通过new 操作符获取一个promise对象,promise者,人如其名也.对,就是承诺.显示生活中,我们承诺别人一件事,一般会在将来某个时间兑现承诺.而 Promi ...

  7. C primer plus 5 读书笔记3

    1.ASCII 码前31位控制字符(非打印字符)的表示方法:(1)使用ASCII码表示比如蜂鸣字符用7(十进制)表示:(2),使用特殊的符号序列,即转义序列.如 ‘\a’表示蜂鸣字符.(3),使用十六 ...

  8. ps/sql developer 登录远程服务器

    Ref PLSQL Developer远程登录的方法

  9. windows10企业版2016长期服务版激活 -------转

    原地址: https://blog.csdn.net/chaoyu168/article/details/79241506 win10 2016 长期服务版的ISO文件中本身就带有KMS激活KEY,不 ...

  10. imshow(K)和imshow(K,[]) 的区别

    参考文献 imshow(K)直接显示K:imshow(K,[])显示K,并将K的最大值和最小值分别作为纯白(255)和纯黑(0),中间的K值映射为0到255之间的标准灰度值.