惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

T
The Blog of Author Tim Ferriss
Know Your Adversary
Know Your Adversary
P
Palo Alto Networks Blog
D
Darknet – Hacking Tools, Hacker News & Cyber Security
K
Kaspersky official blog
L
LINUX DO - 热门话题
P
Proofpoint News Feed
P
Privacy & Cybersecurity Law Blog
Google DeepMind News
Google DeepMind News
Attack and Defense Labs
Attack and Defense Labs
Cisco Talos Blog
Cisco Talos Blog
AI
AI
L
LINUX DO - 最新话题
H
Heimdal Security Blog
Hacker News: Ask HN
Hacker News: Ask HN
Webroot Blog
Webroot Blog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
The GitHub Blog
The GitHub Blog
I
Intezer
Blog — PlanetScale
Blog — PlanetScale
有赞技术团队
有赞技术团队
S
Securelist
博客园_首页
IT之家
IT之家
Schneier on Security
Schneier on Security
博客园 - 叶小钗
罗磊的独立博客
WordPress大学
WordPress大学
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
MongoDB | Blog
MongoDB | Blog
P
Proofpoint News Feed
阮一峰的网络日志
阮一峰的网络日志
A
Arctic Wolf
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
W
WeLiveSecurity
The Register - Security
The Register - Security
D
DataBreaches.Net
S
Security @ Cisco Blogs
Security Archives - TechRepublic
Security Archives - TechRepublic
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
腾讯CDC
Recorded Future
Recorded Future
NISL@THU
NISL@THU
N
News and Events Feed by Topic
T
Tailwind CSS Blog
N
News and Events Feed by Topic
Cyberwarzone
Cyberwarzone
T
Tor Project blog
www.infosecurity-magazine.com
www.infosecurity-magazine.com

博客园 - zencorn

.NET Core 6 类的私有静态变量关联异常 使用 WinSCP 下载 FTP 文件并用 计划任务 复制到 远程 共享目录 【翻译】什么是 eCPM & RPM 与其计算公式 PMP Fundamentals MySQL 存储配置 MS-TEST 批处理执行测试时的资源文件目录问题 测试换行显示 如何制作简单的 3D 打印模型 搬家Testing. Test TFS 自动同步Server 端文件的批处理命令 Perl初级教程 (5) 遍历文件夹内指定扩展名文件,查找匹配关键字的输出。 最方便的批处理延时方法 SQLServer2005 remove log file. Disable Windows server 2003 Security Warning. Perl Scalar Perl 基于 Windows 环境 搭建 Package you execution files with Iexpress.exe Automation testing framework for RFT execution with STAF+STAX . [Session-1]
T-SQL 总结
zencorn · 2015-08-26 · via 博客园 - zencorn

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'0'
    

    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'0'
    

    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'

View Code

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

View Code

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

View Code

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'

View Code

- Make people around you successful is the biggest contribution to ourselves. -