/* Compare the two codes that implement the same function (use a temporary table to compare with a non-temporary table )*/
If (not exists (select * From DBO. sysobjects where id = object_id (n' [DBO]. [old_outid] ') and objectproperty (ID, N 'isusertable') = 1 ))
Begin
Select customerid, outid into old_outid from t_customers
-- Update t_customers set outid = 'enter a new employee ID'
/* -- Restore the new employee ID to the old employee ID
Update t_customers
Set t_customers.outid = old_outid.outid
From old_outid
Where old_outid.customerid = t_customers.customerid
*/
End
Select * into # DPT from (select dpcode1 + dpcode2 + dpcode3 as department code, dpname1 + '/' + dpname2 + '/' + dpname3 as department name from t_department where dpname1 is not null and dpname2 is not null and dpname3 is not null Union all select dpcode1 + dpcode2 + dpcode3 as department code, dpname1 + '/' + dpname2 as department name from t_department where dpname1 is not null and dpname2 is not null and dpname3 is null Union all select dpcode1 + dpcode2 + dpcode3 as department code, dpname1 as department name from t_department where dpname1 is not null and dpname2 is null and dpname3 is null) L
Select customerid, max (opdt) as the last card supplement time into # renewcard_maxdt
From t_renewcard
Group by customerid
Select Department name, name as name, alias as Alias, cardType as card class, C. outid as new employee ID, O. outid as old employee ID, last card supplement time
From t_customers C inner join # dpt d on D. Department code = C. Account inner join # renewcard_maxdt R on
R. customerid = C. customerid inner join old_outid o on C. customerid = O. customerid
Where (C. cardType in (1, 2 ))
Drop table # DPT
Drop table # renewcard_maxdt
/* -- View code. The function is the same as above. It is not easy to read, understand, modify, and update, but it is widely used.
Select D. Department name, C. Name as name, C. Alias as Alias, C. cardType as card class,
C. outid as new employee ID, O. outid as old employee ID, R. Last card supplement time
From t_customers C inner join
(Select dpcode1 + dpcode2 + dpcode3 as department code,
Dpname1 + '/' + dpname2 + '/' + dpname3 as department name
From t_department
Where dpname1 is not null and dpname2 is not null and
Dpname3 is not null
Union all
Select dpcode1 + dpcode2 + dpcode3 as department code,
Dpname1 + '/' + dpname2 as department name
From t_department
Where dpname1 is not null and dpname2 is not null and
Dpname3 is null
Union all
Select dpcode1 + dpcode2 + dpcode3 as department code, dpname1 as department name
From t_department
Where dpname1 is not null and dpname2 is null and dpname3 is null)
D on D. Department code = C. Account inner join
(Select customerid, max (opdt) as last time of completing the card
From t_renewcard
Group by customerid) R on R. customerid = C. customerid inner join
Old_outid o on C. customerid = O. customerid
Where (C. cardType in (1, 2 ))
*/