Compare the two codes that implement the same functions (use temporary tables and do not use temporary tables)

Source: Internet
Author: User
/* 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 ))
*/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.