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

推荐订阅源

D
Docker
爱范儿
爱范儿
T
The Exploit Database - CXSecurity.com
量子位
T
Tailwind CSS Blog
T
Threatpost
The GitHub Blog
The GitHub Blog
AWS News Blog
AWS News Blog
云风的 BLOG
云风的 BLOG
K
Kaspersky official blog
P
Proofpoint News Feed
博客园 - 司徒正美
L
LangChain Blog
T
Threat Research - Cisco Blogs
C
CERT Recently Published Vulnerability Notes
罗磊的独立博客
酷 壳 – CoolShell
酷 壳 – CoolShell
博客园 - 叶小钗
S
Secure Thoughts
The Last Watchdog
The Last Watchdog
Spread Privacy
Spread Privacy
H
Hacker News: Front Page
T
Troy Hunt's Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Google DeepMind News
Google DeepMind News
W
WeLiveSecurity
A
Arctic Wolf
Apple Machine Learning Research
Apple Machine Learning Research
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
P
Proofpoint News Feed
T
Tor Project blog
T
The Blog of Author Tim Ferriss
I
Intezer
P
Privacy & Cybersecurity Law Blog
美团技术团队
N
Netflix TechBlog - Medium
博客园_首页
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
V
Vulnerabilities – Threatpost
Application and Cybersecurity Blog
Application and Cybersecurity Blog
G
Google Developers Blog
Attack and Defense Labs
Attack and Defense Labs
T
Tenable Blog
月光博客
月光博客
Stack Overflow Blog
Stack Overflow Blog
J
Java Code Geeks
腾讯CDC
Microsoft Security Blog
Microsoft Security Blog
A
About on SuperTechFans
Last Week in AI
Last Week in AI

博客园 - black263

因为数据库正在使用,所以无法获得对数据库的独占访问权---还原或删除数据库的解决方法 获取表的列信息,包括列名,列数据类型,主键列.. SQL Server 阻止了对组件 'Ad Hoc Distributed Queries' 的 STATEMENT'OpenRowset/OpenDatasource' 的访问 sql server 客户端 登录名 痕迹删除 sql 为数字加千分位 Sql Server2005恢复备份数据库问题-Error:3154 Slice a PSD Use a CSS Reset Learn Photoshop Google Code University Debug Javascript With Firebug – video tutorial CSS Compressor clean css css Optimiser CSS-Frameworks normalize.css google free web fonts 新公司报道第一天 C#连接数据库 模板类
动态获取表每一列变更记录
black263 · 2012-04-18 · via 博客园 - black263

今天做了一个存储过程

可实现对人事基本资料表中的所有栏位进行check,若有变化,则显示出来

参数:emp_id

原理:在服务器上有两个数据库备份,对比 emp_info 这两张表中相同emp_id的记录 ,

若某一列值不同,则显示出来。

主要问题点:

1.若不能用 OPENDATASOURCE,则可以通过以下sql开启权限。

  1. exec sp_configure 'show advanced options',1  
  2. reconfigure  
  3. exec sp_configure 'Ad Hoc Distributed Queries',1  
  4. reconfigure  

使用完成后,关闭Ad Hoc Distributed Queries:

  1. exec sp_configure 'Ad Hoc Distributed Queries',0  
  2. reconfigure  
  3. exec sp_configure 'show advanced options',0  
  4. reconfigure   

 2.对比时,注意栏位编码不同,需要使用 COLLATE Chinese_PRC_CI_AS 

 3.对比时,需要注意对栏位null值的处理。 

 4.动态sql的使用。

USE [EasyHRO_ContinentalChina_20120222]

GO

/****** Object:  StoredProcedure [dbo].[sp_get_emp_info_diff]    Script Date: 04/18/2012 13:27:25 ******/

SET ANSI_NULLS ON

GO

SET QUOTED_IDENTIFIER ON

GO

ALTER PROC [dbo].[sp_get_emp_info_diff]

  @emp_id nvarchar(50)

--WITH ENCRYPTION

AS  

BEGIN

/*

select *

from 

OPENDATASOURCE('SQLOLEDB','Data Source=CDPR9B0B6A;User ID=sa;Password=Init1234').EasyHRO_ContinentalChina_20120221.[dbo].[emp_info] a

where emp_id=@emp_id

select distinct xtype from dbo.syscolumns where  id=object_id('emp_info') and name not in('emp_id','emp_workid')

select * from systypes

where systypes.xusertype in(56,59,61,104,231)

*/

declare @col_name nvarchar(20);

declare @col_type_id int;

if exists (select * from tempdb.dbo.sysobjects where id = object_id(N'tempdb..#tb') and type='U') 

begin

drop table #tempcitys

end

create table #tb(emp_id int , 

emp_workid nvarchar(200) , 

emp_name nvarchar(200),

old_valus nvarchar(200),

new_valus nvarchar(200),

[col_name] nvarchar(200),

col_cname nvarchar(200)

)

  declare cur cursor fast_forward for

     select name,xusertype from dbo.syscolumns where  id=object_id('emp_info') and name not in('emp_id','emp_workid');

  open cur;

  fetch next from cur into @col_name,@col_type_id;

  while @@fetch_status=0

  begin

      --做你要做的事

     DECLARE @dongtaisql nVARCHAR(4000);

     if(@col_type_id=231)--nvarchar

     begin

set @dongtaisql = 'insert into #tb select a.emp_id,a.emp_workid,a.emp_name,a.'+@col_name+',b.'+@col_name+','''+@col_name+''','''' from emp_info a,'

+' OPENDATASOURCE(''SQLOLEDB'',''Data Source=CDPR9B0B6A;User ID=sa;Password=Init1234'').EasyHRO_ContinentalChina_20120221.[dbo].[emp_info] b '

+' where a.emp_id=b.emp_id and isnull(a.'+@col_name+','''')<>isnull(b.'+@col_name +','''') COLLATE Chinese_PRC_CI_AS '

+' and a.emp_id=@emp_id '

--+' and a.emp_id='+QUOTENAME(@emp_id, '''')

     end

     else if(@col_type_id=56 or @col_type_id=59 or @col_type_id=104)--int,real,bit

     begin

set @dongtaisql = ' insert into #tb select a.emp_id,a.emp_workid,a.emp_name,a.'+@col_name+',b.'+@col_name+','''+@col_name+''','''' from emp_info a,'

+' OPENDATASOURCE(''SQLOLEDB'',''Data Source=CDPR9B0B6A;User ID=sa;Password=Init1234'').EasyHRO_ContinentalChina_20120221.[dbo].[emp_info] b '

+' where a.emp_id=b.emp_id and isnull(a.'+@col_name+',0)<>isnull(b.'+@col_name+',0) '

+' and a.emp_id=@emp_id '

--+' and a.emp_id='+QUOTENAME(@emp_id, '''')

     end

     else if(@col_type_id=61)--datetime

     begin

set @dongtaisql = ' insert into #tb select a.emp_id,a.emp_workid,a.emp_name,a.'+@col_name+',b.'+@col_name+','''+@col_name+''','''' from emp_info a,'

+' OPENDATASOURCE(''SQLOLEDB'',''Data Source=CDPR9B0B6A;User ID=sa;Password=Init1234'').EasyHRO_ContinentalChina_20120221.[dbo].[emp_info] b '

+' where a.emp_id=b.emp_id and isnull(a.'+@col_name+',''1900-01-01'')<>isnull(b.'+@col_name+',''1900-01-01'') '

+' and a.emp_id=@emp_id '

--+' and a.emp_id='+QUOTENAME(@emp_id, '''')

     end

     else

     begin

set @dongtaisql = ' insert into #tb select a.emp_id,a.emp_workid,a.emp_name,CONVERT(nvarchar(10),a.'+@col_name+',112),CONVERT(nvarchar(10),b.'+@col_name+',112),'''+@col_name+''','''' from emp_info a,'

+' OPENDATASOURCE(''SQLOLEDB'',''Data Source=CDPR9B0B6A;User ID=sa;Password=XXXX'').EasyHRO_ContinentalChina_20120221.[dbo].[emp_info] b '

+' where a.emp_id=b.emp_id and a.'+@col_name+'<>b.'+@col_name

+' and a.emp_id=@emp_id '

--+' and a.emp_id='+QUOTENAME(@emp_id, '''')

     end

    print @dongtaisql

--exec @dongtaisql

--insert into #tb

EXEC SP_EXECUTESQL @dongtaisql, N'@emp_id nvarchar(50)',@emp_id

 --    exec( 

--'select a.emp_id,a.emp_workid,a.emp_name,a.'+@col_name+',b.'+@col_name+' from emp_info a,'

--+' OPENDATASOURCE(''SQLOLEDB'',''Data Source=CDPR9B0B6A;User ID=sa;Password=XXXX'').EasyHRO_ContinentalChina_20120221.[dbo].[emp_info] b '

--+' where a.emp_id=b.emp_id and a.'+@col_name+'<>b.'+@col_name

--+' and a.emp_id='+cast(@emp_id as nvarchar)+' ')

fetch next from cur into @col_name,@col_type_id; 

  end

  close cur;

  deallocate cur;

delete from #tb

where emp_id is null

update #tb

set col_cname=

(case when exists(select pla_lan_001 from pla_language

where pla_lan_code =[col_name] COLLATE Chinese_PRC_CI_AS and comp_lvcode=10001000)

then (select pla_lan_001 from pla_language

where pla_lan_code =[col_name] COLLATE Chinese_PRC_CI_AS and comp_lvcode=10001000)

else

(select pla_lan_001 from pla_language

where pla_lan_code =[col_name] COLLATE Chinese_PRC_CI_AS and comp_lvcode=1)

end

)

select emp_id,emp_workid, 

emp_name,

case when isdate(old_valus)=1 then CONVERT(nvarchar(10),old_valus,121) else old_valus end as old_valus,

case when isdate(new_valus)=1 then CONVERT(nvarchar(10),new_valus,121) else new_valus end as new_valus,

[col_name],

col_cname 

from #tb

/*

select a.emp_id,a.emp_workid,a.emp_name,a.emp_actid,b.emp_actid from emp_info a,

OPENDATASOURCE('SQLOLEDB','Data Source=CDPR9B0B6A;User ID=sa;Password=Init1234').EasyHRO_ContinentalChina_20120221.[dbo].[emp_info] b

where a.emp_id=b.emp_id and a.emp_actid<>b.emp_actid

and a.emp_id=@emp_id

*/

END