Two code comparisons that implement the same functionality (use temporary tables compared to no temporary tables)

Source: Internet
Author: User
Tags implement join
Comparison | temporary tables

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= ' Please enter a new work number '
*--Restore the new work number to the old work number value
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_departme NT where dpname1 are NOT null and dpname2 are NOT null and DPNAME3 are NOT NULL UNION ALL SELECT DPCODE1+DPCODE2+DPCODE3 as Gate code, dpname1+ '/' +dpname2 as department name from T_department where dpname1 are not null and dpname2 are NOT null and DPNAME3 is null U Nion all SELECT dpcode1+dpcode2+dpcode3 as department code, dpname1 as department name from T_department where dpname1 are not null and dpname2 I s null and DPNAME3 is null) l


SELECT CustomerID, MAX (OPDT) as last replenishment 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 work number, O.outid as old work number, last replenishment 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


/*-For View code, function ibid, inconvenient to read and modify the update, but widely applicable.
SELECT d. department name, C.name as name, C.alias as Alias, C.cardtype as card class,
C.outid as new work number, O.outid as old work number, R. Last Replenishment 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 are not null and dpname2 are 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 are not null and dpname2 are NOT null and
Dpname3 is NULL
UNION All
SELECT Dpcode1 + dpcode2 + dpcode3 as department code, dpname1 as department name
From T_department
WHERE dpname1 are not null and dpname2 are null and DPNAME3 is null)
D on D. department code = C.account INNER JOIN
(SELECT CustomerID, MAX (OPDT) as last replenishment time
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.