【发布时间】:2014-02-04 18:46:24
【问题描述】:
我创建了两个表 customersrc 和 customertemp 与列:
客户温度
ID name age addr cityid isactive
34 Gi 24 Chennai 1 1
customersrc
CustomerId CustomerName CustomerAge CustomerAddress
1 Gi 24 madurai
2 Pa 23 Tirupur
3 MI 27 Tirupur
现在我需要将 pa 和 mi 数据值插入到临时表中,因为它与 customertemp 的行不匹配。并且匹配的行 gi 数据将被更新。
我使用了以下MERGE 声明
DECLARE @cityid INT SET @cityid=1
MERGE Temp.dbo.customersrc as src_customer
USING ( SELECT CustomerName,CustomerAge,CustomerAddress FROM customertemp) as temp_customer
ON src_customer.name=temp_customer.CustomerName
AND
src_customer.cityid=@cityid
WHEN MATCHED THEN
UPDATE SET
src_customer.age=temp_customer.CustomerAge,
src_customer.addr=temp_customer.CustomerAddress,
src_customer.isactive=1
WHEN NOT MATCHED BY SOURCE THEN
UPDATE SET src_customer.isactive=0 ; -- here i need the insert statement to insert in another table
问题:
- 可以在when not match by source query里面写insert语句吗?
- 如果不可能,那么如何使用
merge来实现这一点? - 在一个简单的集合论中,我需要将customersrc(table_B)-customertemp (table_A) 放入。 B-A 值到另一个或临时表中。
【问题讨论】:
-
这是一个不寻常的要求。为什么要插入到另一个表?您稍后会将这些数据插入到 customersrc 中吗?
-
no 显示不在 customertemp 表中的用户。
标签: sql sql-server tsql