Sql server® 2012 t-sql fundamentals



Download 10,93 Mb.
Pdf ko'rish
bet325/443
Sana26.01.2022
Hajmi10,93 Mb.
#411755
1   ...   321   322   323   324   325   326   327   328   ...   443
Bog'liq
BookSQL

MERGE

 with 

OUTPUT

You can also use the OUTPUT clause with the MERGE statement, but remember that a single MERGE 

statement can invoke multiple different DML actions based on conditional logic. This means that 

a single MERGE statement might return through the OUTPUT clause rows that were produced by 

different DML actions. To identify which DML action produced the output row, you can invoke a func-

tion called $action in the OUTPUT clause, which will return a string representing the action (INSERT, 

UPDATE, or DELETE). To demonstrate the use of the OUTPUT clause with the MERGE statement, I’ll 

use one of the examples from the “Merging Data” section earlier in this chapter. To run this example, 

make sure you rerun Listing 8-2 to re-create the dbo.Customers and dbo.CustomersStage tables.

The following code merges the contents of CustomersStage into Customers, updating the attri-

butes of customers who already exist in the target and adding customers who don’t.

MERGE INTO dbo.Customers AS TGT 

USING dbo.CustomersStage AS SRC 

  ON TGT.custid = SRC.custid 

WHEN MATCHED THEN 

  UPDATE SET 

    TGT.companyname = SRC.companyname, 

    TGT.phone = SRC.phone, 

    TGT.address = SRC.address 

WHEN NOT MATCHED THEN  

  INSERT (custid, companyname, phone, address) 

  VALUES (SRC.custid, SRC.companyname, SRC.phone, SRC.address) 

OUTPUT $action AS theaction, inserted.custid, 

  deleted.companyname AS oldcompanyname, 

  inserted.companyname AS newcompanyname, 

  deleted.phone AS oldphone, 

  inserted.phone AS newphone, 

  deleted.address AS oldaddress, 

  inserted.address AS newaddress;

www.it-ebooks.info





Download 10,93 Mb.

Do'stlaringiz bilan baham:
1   ...   321   322   323   324   325   326   327   328   ...   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