Sql server® 2012 t-sql fundamentals



Download 10,93 Mb.
Pdf ko'rish
bet315/443
Sana26.01.2022
Hajmi10,93 Mb.
#411755
1   ...   311   312   313   314   315   316   317   318   ...   443
Bog'liq
BookSQL

 

CHAPTER 8

 

Data Modification



 

 271

INSERT INTO dbo.Customers(custid, companyname, phone, address) 

VALUES 

  (1, 'cust 1', '(111) 111-1111', 'address 1'), 

  (2, 'cust 2', '(222) 222-2222', 'address 2'), 

  (3, 'cust 3', '(333) 333-3333', 'address 3'), 

  (4, 'cust 4', '(444) 444-4444', 'address 4'), 

  (5, 'cust 5', '(555) 555-5555', 'address 5'); 

 

IF OBJECT_ID('dbo.CustomersStage', 'U') IS NOT NULL DROP TABLE dbo.



CustomersStage; 

GO 


 

CREATE TABLE dbo.CustomersStage 

  custid      INT         NOT NULL, 



  companyname VARCHAR(25) NOT NULL, 

  phone       VARCHAR(20) NOT NULL, 

  address     VARCHAR(50) NOT NULL, 

  CONSTRAINT PK_CustomersStage PRIMARY KEY(custid) 

); 

 

INSERT INTO dbo.CustomersStage(custid, companyname, phone, address) 



VALUES 

  (2, 'AAAAA', '(222) 222-2222', 'address 2'), 

  (3, 'cust 3', '(333) 333-3333', 'address 3'), 

  (5, 'BBBBB', 'CCCCC', 'DDDDD'), 

  (6, 'cust 6 (new)', '(666) 666-6666', 'address 6'), 

  (7, 'cust 7 (new)', '(777) 777-7777', 'address 7');

Run the following query to examine the contents of the Customers table.

SELECT * FROM dbo.Customers;

This query returns the following output.

custid      companyname      phone                address 

----------- ---------------- -------------------- ------------ 

1           cust 1           (111) 111-1111       address 1 

2           cust 2           (222) 222-2222       address 2 

3           cust 3           (333) 333-3333       address 3 

4           cust 4           (444) 444-4444       address 4 

5           cust 5           (555) 555-5555       address 5

Run the following query to examine the contents of the CustomersStage table.

SELECT * FROM dbo.CustomersStage;

www.it-ebooks.info



272  

Microsoft SQL Server 2012 T-SQL Fundamentals

This query returns the following output.

custid      companyname      phone                address 

----------- ---------------- -------------------- ------------ 

2           AAAAA            (222) 222-2222       address 2 

3           cust 3           (333) 333-3333       address 3 

5           BBBBB            CCCCC                DDDDD 

6           cust 6 (new)     (666) 666-6666       address 6 

7           cust 7 (new)     (777) 777-7777       address 7

The purpose of the first example of the MERGE statement that I’ll demonstrate is to merge the 

contents of the CustomersStage table (the source) into the Customers table (the target). More specifi-

cally, the example will add customers that do not exist, and update the attributes of customers that 

already exist. 

If you already feel comfortable with the sections that covered deletions and updates based on 

joins, you should feel quite comfortable with MERGE, which is based on join semantics. You specify 

the target table name in the MERGE clause and the source table name in the USING clause. You de-

fine a merge condition by specifying a predicate in the ON clause, very much as you do in a join. The 

merge condition defines which rows in the source table have matches in the target and which don’t. 

You define the action to take when a match is found in a clause called WHEN MATCHED THEN, and 

the action to take when a match is not found in the WHEN NOT MATCHED THEN clause.

Here’s the first example for the MERGE statement: adding nonexistent customers and updating 

existing ones.

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);



note

  It is mandatory to terminate the MERGE statement with a semicolon, whereas in most 

other statements in T-SQL, this is optional. But if you follow best practices to terminate all 

statements with a semicolon (as I mentioned earlier in this book), this shouldn’t concern you.

This MERGE statement defines the Customers table as the target (in the MERGE clause) and the 

CustomersStage table as the source (in the USING clause). Notice that you can assign aliases to the 

target and source tables for brevity (TGT and SRC in this case). The predicate TGT.custid = SRC.custid 

is used to define what is considered a match and what is considered a nonmatch. In this case, if a 

customer ID that exists in the source also exists in the target, that’s a match. If a customer ID in the 

source does not exist in the target, that’s a nonmatch. 

www.it-ebooks.info





Download 10,93 Mb.

Do'stlaringiz bilan baham:
1   ...   311   312   313   314   315   316   317   318   ...   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