Sql server® 2012 t-sql fundamentals



Download 10,93 Mb.
Pdf ko'rish
bet334/443
Sana26.01.2022
Hajmi10,93 Mb.
#411755
1   ...   330   331   332   333   334   335   336   337   ...   443
Bog'liq
BookSQL

 

CHAPTER 8

 

Data Modification



 

 295

UPDATE O 

  SET shipcountry = C.country, 

      shipregion = C.region, 

      shipcity = C.city 

FROM dbo.Orders AS O 

  JOIN dbo.Customers AS C 

    ON O.custid = C.custid 

WHERE C.country = 'UK';

Another solution to this exercise uses CTEs. You can define a CTE based on a SELECT query that 

joins dbo.Orders and dbo.Customers and returns both the target location attributes from dbo.Orders 

and the source location attributes from dbo.Customers. The outer query would then be an UPDATE 

statement modifying the target attributes with the values of the source attributes. Here’s the com-

plete solution statement.

WITH CTE_UPD AS 

  SELECT 



    O.shipcountry AS ocountry, C.country AS ccountry, 

    O.shipregion  AS oregion,  C.region  AS cregion, 

    O.shipcity    AS ocity,    C.city    AS ccity 

  FROM dbo.Orders AS O 

    JOIN dbo.Customers AS C 

      ON O.custid = C.custid 

  WHERE C.country = 'UK' 

UPDATE CTE_UPD 



  SET ocountry = ccountry, oregion = cregion, ocity = ccity;

You can also use the MERGE statement to achieve this task. As explained earlier, even though in a 



MERGE statement you usually want to specify both the WHEN MATCHED and WHEN NOT MATCHED 

clauses, the statement supports specifying only one of the clauses. Using only a WHEN MATCHED 

clause with an UPDATE action, you can write a solution that is logically equivalent to the last two solu-

tions. Here’s the complete solution statement.

MERGE INTO dbo.Orders AS O 

USING dbo.Customers AS C 

   ON O.custid = C.custid 

   AND C.country = 'UK' 

WHEN MATCHED THEN 

  UPDATE SET shipcountry = C.country, 

             shipregion = C.region, 

             shipcity = C.city;

www.it-ebooks.info



www.it-ebooks.info



Download 10,93 Mb.

Do'stlaringiz bilan baham:
1   ...   330   331   332   333   334   335   336   337   ...   443




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish