我使用 SQL 编写 SQL。通过执行此语句:
select
'select n.updates_id as ID,
n.submission_id as SubmissionID,
n.Name as name,
'''+sc.name+''' as [Field Updated],
convert(varchar(max), o.'+sc.name+') as [Old Value],
convert(varchar(max), n.'+sc.name+') as [New Value]
from orderedUpdates o
inner join orderedUpdates n
on o.submission_id = n.submission_id
and o.rowId = n.rowId + 1
where n.effDate > dateadd(month,-1,getdate())
and (o.'+sc.name+' <> n.'+sc.name+' OR o.'+sc.name+' is null and n.'+sc.name+' is not null OR o.'+sc.name+' is not null and n.'+sc.name+' is null) UNION '
from sysobjects so inner join syscolumns sc
on so.id = sc.id
where so.name = 'updates'
我能够为 Updates 表上的每一列生成一个 UNION 语句。然后将该列表配对并将其与 CTE 组合产生以下结果:
with orderedUpdates(rowId,updates_id,submission_id,policyno,
loanno,
banker,
effdate,
name,
name2,
address,
address2,
city,
state,
zip,
county)
as
(
select Rank() over(partition by submission_id order by updates_id), updates_id, submission_id,policyno,
loanno,
banker,
effdate,
name,
name2,
address,
address2,
city,
state,
zip,
county
from updates
)
select n.updates_id as ID, n.submission_id as SubmissionID, n.Name as name, 'policyno' as [Field Updated], convert(varchar(max), o.policyno) as [Old Value], convert(varchar(max), n.policyno) as [New Value] from orderedUpdates o inner join orderedUpdates n on o.submission_id = n.submission_id and o.rowId = n.rowId + 1 where n.effDate > dateadd(month,-1,getdate()) and (o.policyno <> n.policyno OR o.policyno is null and n.policyno is not null OR o.policyno is not null and n.policyno is null) UNION
select n.updates_id as ID, n.submission_id as SubmissionID, n.Name as name, 'loanno' as [Field Updated], convert(varchar(max), o.loanno) as [Old Value], convert(varchar(max), n.loanno) as [New Value] from orderedUpdates o inner join orderedUpdates n on o.submission_id = n.submission_id and o.rowId = n.rowId + 1 where n.effDate > dateadd(month,-1,getdate()) and (o.loanno <> n.loanno OR o.loanno is null and n.loanno is not null OR o.loanno is not null and n.loanno is null) UNION
select n.updates_id as ID, n.submission_id as SubmissionID, n.Name as name, 'banker' as [Field Updated], convert(varchar(max), o.banker) as [Old Value], convert(varchar(max), n.banker) as [New Value] from orderedUpdates o inner join orderedUpdates n on o.submission_id = n.submission_id and o.rowId = n.rowId + 1 where n.effDate > dateadd(month,-1,getdate()) and (o.banker <> n.banker OR o.banker is null and n.banker is not null OR o.banker is not null and n.banker is null) UNION
select n.updates_id as ID, n.submission_id as SubmissionID, n.Name as name, 'effdate' as [Field Updated], convert(varchar(max), o.effdate) as [Old Value], convert(varchar(max), n.effdate) as [New Value] from orderedUpdates o inner join orderedUpdates n on o.submission_id = n.submission_id and o.rowId = n.rowId + 1 where n.effDate > dateadd(month,-1,getdate()) and (o.effdate <> n.effdate OR o.effdate is null and n.effdate is not null OR o.effdate is not null and n.effdate is null) UNION
select n.updates_id as ID, n.submission_id as SubmissionID, n.Name as name, 'name' as [Field Updated], convert(varchar(max), o.name) as [Old Value], convert(varchar(max), n.name) as [New Value] from orderedUpdates o inner join orderedUpdates n on o.submission_id = n.submission_id and o.rowId = n.rowId + 1 where n.effDate > dateadd(month,-1,getdate()) and (o.name <> n.name OR o.name is null and n.name is not null OR o.name is not null and n.name is null) UNION
select n.updates_id as ID, n.submission_id as SubmissionID, n.Name as name, 'name2' as [Field Updated], convert(varchar(max), o.name2) as [Old Value], convert(varchar(max), n.name2) as [New Value] from orderedUpdates o inner join orderedUpdates n on o.submission_id = n.submission_id and o.rowId = n.rowId + 1 where n.effDate > dateadd(month,-1,getdate()) and (o.name2 <> n.name2 OR o.name2 is null and n.name2 is not null OR o.name2 is not null and n.name2 is null) UNION
select n.updates_id as ID, n.submission_id as SubmissionID, n.Name as name, 'address' as [Field Updated], convert(varchar(max), o.address) as [Old Value], convert(varchar(max), n.address) as [New Value] from orderedUpdates o inner join orderedUpdates n on o.submission_id = n.submission_id and o.rowId = n.rowId + 1 where n.effDate > dateadd(month,-1,getdate()) and (o.address <> n.address OR o.address is null and n.address is not null OR o.address is not null and n.address is null) UNION
select n.updates_id as ID, n.submission_id as SubmissionID, n.Name as name, 'address2' as [Field Updated], convert(varchar(max), o.address2) as [Old Value], convert(varchar(max), n.address2) as [New Value] from orderedUpdates o inner join orderedUpdates n on o.submission_id = n.submission_id and o.rowId = n.rowId + 1 where n.effDate > dateadd(month,-1,getdate()) and (o.address2 <> n.address2 OR o.address2 is null and n.address2 is not null OR o.address2 is not null and n.address2 is null) UNION
select n.updates_id as ID, n.submission_id as SubmissionID, n.Name as name, 'city' as [Field Updated], convert(varchar(max), o.city) as [Old Value], convert(varchar(max), n.city) as [New Value] from orderedUpdates o inner join orderedUpdates n on o.submission_id = n.submission_id and o.rowId = n.rowId + 1 where n.effDate > dateadd(month,-1,getdate()) and (o.city <> n.city OR o.city is null and n.city is not null OR o.city is not null and n.city is null) UNION
select n.updates_id as ID, n.submission_id as SubmissionID, n.Name as name, 'state' as [Field Updated], convert(varchar(max), o.state) as [Old Value], convert(varchar(max), n.state) as [New Value] from orderedUpdates o inner join orderedUpdates n on o.submission_id = n.submission_id and o.rowId = n.rowId + 1 where n.effDate > dateadd(month,-1,getdate()) and (o.state <> n.state OR o.state is null and n.state is not null OR o.state is not null and n.state is null) UNION
select n.updates_id as ID, n.submission_id as SubmissionID, n.Name as name, 'zip' as [Field Updated], convert(varchar(max), o.zip) as [Old Value], convert(varchar(max), n.zip) as [New Value] from orderedUpdates o inner join orderedUpdates n on o.submission_id = n.submission_id and o.rowId = n.rowId + 1 where n.effDate > dateadd(month,-1,getdate()) and (o.zip <> n.zip OR o.zip is null and n.zip is not null OR o.zip is not null and n.zip is null) UNION
select n.updates_id as ID, n.submission_id as SubmissionID, n.Name as name, 'county' as [Field Updated], convert(varchar(max), o.county) as [Old Value], convert(varchar(max), n.county) as [New Value] from orderedUpdates o inner join orderedUpdates n on o.submission_id = n.submission_id and o.rowId = n.rowId + 1 where n.effDate > dateadd(month,-1,getdate()) and (o.county <> n.county OR o.county is null and n.county is not null OR o.county is not null and n.county is null )
order by 1,2