£Í£å£ò£ç£å SQL 2008
merge [target] t
using [source] s on t.id = s.id
when matched then update t.name = s.name, t.age = s.age -- use "rowset1"
when not matched then insert values(id,name,age) -- use "rowset2"
when source not matched then delete; -- use "rowset3"
MERGE dbo.table AS im --¶Ô±È±í
USING (SELECT * from dbo.table_MID with(nolock) ) AS src --Ô´
ON im.ProductID = src. ProductID
WHEN MATCHED THEN
UPDATE SET im.ProductID = src. ProductID
,im.BrandName=src.BrandName
,im.ProductName = src.ProductName
,im.ProductType=src.ProductType
,indate=getdate()
WHEN NOT MATCHED THEN
INSERT (ProductID,BrandName,Series,ModelNumber,ProductName,ProductType,Category,indate)
VALUES (src.ProductID,src.BrandName,src.Series,src.ModelNumber,src.ProductName,src.ProductType,src.Category,src.indate)
WHEN NOT MATCHED BY SOURCE then delete;
Ïà¹ØÎĵµ£º
4£®Êý¾ÝÀàÐÍת»»º¯Êý
¡ñÒþʽת»»
¸³ÖµÊ±¿É½øÐеÄÒþʽת»»ÓÐ
VARCHAR2»òCHAR —¡µNUMBER
VARCHAR2»òCHAR —¡µDATE
NUMBER —¡µVARCHAR ......
ÄãÊÇ·ñÔÚǧ·½°Ù¼ÆÓÅ»¯SQL Server Êý¾Ý¿âµÄÐÔÄÜ?Èç¹ûÄãµÄÊý¾Ý¿âÖк¬ÓдóÁ¿µÄ±í¸ñ£¬°ÑÕâЩ±í¸ñ·ÖÇø·ÅÈë¶ÀÁ¢µÄÎļþ×é¿ÉÄÜ»áÈÃÄãÊÜÒæ·Ëdz¡£SQL Server 2005ÒýÈëµÄ±í·ÖÇø¼¼Êõ£¬ÈÃÓû§Äܹ»°ÑÊý¾Ý·ÖÉ¢´æ·Åµ½²»Í¬µÄÎïÀí´ÅÅÌÖУ¬Ìá¸ßÕâЩ´ÅÅ̵IJ¢Ðд¦ÀíÐÔÄÜÒÔÓÅ»¯²éѯÐÔÄÜ¡£
¡¡¡¡SQL ServerÊý¾Ý¿â±í·ÖÇø²Ù×÷¹ý³ÌÓÉÈý¸ö²½Öè×é³É£º
¡ ......
--1.¹ØÓÚwhereɸѡÆ÷ÖгöÏÖÖ¸¶¨ÐÇÆÚ¼¸µÄÇó½â
SQL code
--»·¾³
create table test_1
(
id int,
value varchar(10),
t_time datetime
)
insert test_1
select 1,'a','2009-04-19' union
select 2,'b','2009-04-20' union
select 3,'c','2009-04-21' union
select 4,'d','2009-04-22' union
s ......