Merge的山寨版「聯機幫助」

2022-03-01 07:33:05 字數 2076 閱讀 9392

if

notobject_id('

demo_allproducts

') is

null

drop

table

demo_allproducts

gocreate

table

demo_allproducts

( pkid

intnot

null

identity(1,1) primary

key,

dname

nvarchar(20) null

, dcode

nvarchar(20) null

, ddate

datetime

null)go

insert

into

demo_allproducts

(dname,dcode,ddate)

values('

demoa

','aaa

',getdate

()),('

demob

','bbb

',getdate

()),('

democ

','ccc

',getdate

()),('

demod

','ddd

',getdate

()),('

demoe

','eee

',getdate

())select

*from

demo_allproducts

ifnot

object_id('

demo_shop1_product

') is

null

drop

table

demo_shop1_product

create

table

demo_shop1_product

( pkid

intnot

null

identity(1,1) primary

key,

dname

nvarchar(20) null

, dcode

nvarchar(20) null

, ddate

datetime

null)go

insert

into

demo_shop1_product

values('

demoa

','aaa

',getdate

()),('

demob

','ccc

',getdate

()),('

demof

','fff

',getdate

())select

*from

demo_shop1_product dsp

--確定目標表

merge into

demo_allproducts p

--查詢編碼相同的產品

using demo_shop1_product s on p.dcode=

s.dcode

--如果編碼相同,名稱不同,更新目標表【demo_allproducts】名稱

when matched and p.dname<>s.dname then

update

set p.dname =

s.dname

--如果目標表不存在則插入

when

not matched by target then

insert(dname,dcode,ddate) values

(s.dname,s.dcode,s.ddate)

--如果目標表資料,子表不存在,刪除目標表記錄

when

not matched by source then

delete ;

土豆的 merge 幫助文件,比聯機幫助牛b,哈哈

有感於聯機幫助

關於資料庫備份,聯機幫助上有如下兩個說法 backup database 的 differential 選項說明 指定資料庫備份或檔案備份應該與上一次完整備份後改變的資料庫或檔案部分保持一致。差異備份一般會比完整備份占用更少的空間。對於上一次完整備份時備份的全部單個日誌,使用該選項可以不必再進行備份...

有感於聯機幫助

關於資料庫備份,聯機幫助上有如下兩個說法 backup database 的 differential 選項說明 指定資料庫備份或檔案備份應該與上一次完整備份後改變的資料庫或檔案部分保持一致。差異備份一般會比完整備份占用更少的空間。對於上一次完整備份時備份的全部單個日誌,使用該選項可以不必再進行備份...

有感於聯機幫助

關於資料庫備份,聯機幫助上有如下兩個說法 backup database 的 differential 選項說明 指定資料庫備份或檔案備份應該與上一次完整備份後改變的資料庫或檔案部分保持一致。差異備份一般會比完整備份占用更少的空間。對於上一次完整備份時備份的全部單個日誌,使用該選項可以不必再進行備份...