SQL Server:½«ÁиüдÓÒ»¸ö±í´«µÝµ½ÁíÒ»¸ö±í
ÎÊ£ºÔõÑùÔÚÒ»¸öUPDATEÓï¾äÖÐʹÓñíBµÄÈý¸öÁиüбíAÖеÄÈý¸öÁУ¿
¡¡¡¡´ð£º¶ÔÕâ¸öÎÊÌ⣬Äú¿ÉÒÔʹÓÃÇ¿´óµÄ¹Øϵ´úÊý¡£±¾Ò³ÖеĴúÂë˵Ã÷ÁËÈçºÎ×éºÏʹÓÃfrom×Ó¾äºÍJOIN²Ù×÷£¬ÒÔ´ïµ½ÓÃÆäËû±íÖÐÊý¾Ý¸üÐÂÖ¸¶¨ÁеÄÄ¿µÄ¡£ÔÚÉè¼Æ¹Øϵ±í´ïʽʱ£¬ÄúÐèÒª¾ö¶¨ÊÇ·ñÐèÒªµ¥Ò»ÐÐÆ¥Åä¶à¸öÐУ¨Ò»¶Ô¶à¹Øϵ£©£¬»òÕßÐèÒª¶à¸öÐÐÆ¥Åä±»Áª½Ó±íÖеĵ¥Ò»ÐÐÒÔ¸üÐÂËùÓÐÐУ¨¶à¶ÔÒ»¹Øϵ£©¡£
¡¡¡¡ÔÚÒ»¶Ô¶à¹ØϵÖУ¬SQL ServerʼÖÕʹÓÃËüÕÒµ½µÄ×îºóÒ»ÐиüÐÂÊý¾Ý¡£µ«ÊÇ£¬ÄúÎÞ·¨¿ØÖÆ×îºóÒ»ÐÐËùÔÚµÄλÖá£ÔڶദÀíÆ÷µÄ¼ÆËã»úÉÏ£¬²éѯ¿ÉÄÜÊÇͬ²½½øÐеģ¬ÏàͬµÄ²éѯ×îºóÒ»ÐеÄλÖÿÉÄܻ᲻ͬ¡£Òò´Ë£¬Microsoft½¨Ò龡Á¿²»ÒªÊ¹ÓÃÒ»¶Ô¶à¹Øϵ¡£
¡¡¡¡Èç¹û´ý¸üеıíÓëfrom×Ó¾äÖеıíÏàͬ£¬²¢ÇÒfrom×Ó¾äÖ»°üº¬¶Ô¸Ã±íµÄÒ»¸öÒýÓ㬱ðÃû¿ÉÄÜδ±»Ö¸¶¨¡£Èç¹û´ý¸üеıíÔÚfrom×Ó¾äÖгöÏÖÁ˶à´Î£¬Ö»ÓиñíµÄÒ»¸öÒýÓÿÉÒÔÊ¡ÂÔ±íµÄ±ðÃû£¬¶Ô¸Ã±íµÄËùÓÐÆäËûÒýÓñØÐë°üº¬Ò»¸ö±í±ðÃû¡£
USE tempdb
GO
CREATE TABLE #t1 (c1 int NOT NULL, c2 char(5), c3 char(5),
c4 char(5))
GO
CREATE TABLE #t2 (c1 int NOT NULL, c2 char(5), c3 char(5),
c4 char(5))
GO
--Êý¾Ý¸³Öµ
INSERT #t1 values (1,'hello','there','fred')
INSERT #t2 values (1,'how','are','you?')
--¸üÐÂÊý¾Ý
UPDATE #t1 SET #t1.c2 = #t2.c2, #t1.c3 = #t2.c3,
#t1.c4 = #t2.c4
from #t2
WHERE #t1.c1 = #t2.c1
--¼ì²é½á¹û
SELECT * from #t1
Ïà¹ØÎĵµ£º
ϵͳ»·¾³£ºWindows 7
Èí¼þ»·¾³£ºVisual C++ 2008 SP1 +SQL Server 2005
±¾´ÎÄ¿µÄ£º±àдһ¸öº½¿Õ¹ÜÀíϵͳ
ÕâÊÇÊý¾Ý¿â¿Î³ÌÉè¼ÆµÄ³É¹û£¬ËäÈ»³É¼¨²»¼Ñ£¬µ«ÊÇ×÷ΪÎÒÓÃVC++ ÒÔÀ´±àдµÄ×î´ó³ÌÐò»¹ÊÇ´«µ½ÍøÉÏ£¬ÒÔ¹©²Î¿¼¡£ÓÃVC++ ×öÊý¾Ý¿âÉè¼Æ²¢²»ÈÝÒ×£¬µ«Ò²²»ÊDz»¿ÉÄÜ¡£ÒÔÏÂÊÇÎҵijÌÐò½çÃ棬ºóÃæ ......
declare @tb3 table (ÉÌÆ·±àºÅ nvarchar(10),Åú´ÎºÅ nvarchar(10),¿â´æÊýÁ¿ int,³ö¿âÊýÁ¿ int)
declare @tb1 table (ÉÌÆ·±àºÅ nvarchar(10),Åú´ÎºÅ nvarchar(10),¿â´æÊýÁ¿ int)
insert into @tb1 select '0001','090801',200
union all select '0001','090501',50
&n ......
1. ¶¨ÒåÓα궨Òå
ÓαêÓï¾äµÄºËÐÄÊǶ¨ÒåÁËÒ»¸öÓαê±êʶÃû£¬²¢°ÑÓαê±êʶÃûºÍÒ»¸ö²éѯÓï¾ä¹ØÁªÆðÀ´¡£DECLAREÓï¾äÓÃÓÚÉùÃ÷Óα꣬Ëüͨ¹ýSELECT²éѯ¶¨ÒåÓαê´æ´¢µÄÊý¾Ý¼¯ºÏ¡£Óï¾ä¸ñʽΪ£º
DECLARE ÓαêÃû³Æ [INSENSITIVE] [SCROLL]
CURSOR FOR selectÓï¾ä
[FOR{READ ONLY|UPDATE[OF ÁÐÃû×Ö±í]}]
²ÎÊý˵Ã÷£º
INSENSITIVEÑ¡Ï ......
DBOÊÇÿ¸öÊý¾Ý¿âµÄĬÈÏÓû§£¬¾ßÓÐËùÓÐÕßȨÏÞ£¬¼´DbOwner
ͨ¹ýÓÃDBO×÷ΪËùÓÐÕßÀ´¶¨Òå¶ÔÏó£¬Äܹ»Ê¹Êý¾Ý¿âÖеÄÈκÎÓû§ÒýÓöø²»±ØÌṩËùÓÐÕßÃû³Æ¡£
±ÈÈ磺ÄãÒÔUser1µÇ¼½øÈ¥²¢½¨±íTable£¬¶øδָ¶¨DBO£¬
µ±Óû§User2µÇ½øÈ¥Ïë·ÃÎÊTableʱ¾ÍµÃÖªµÀÕâ¸öTableÊÇÄãUser1½¨Á¢µÄ£¬ÒªÐ´ÉÏUser1.Table£¬Èç¹ûËû²»ÖªµÀÊÇÄ㽨µÄ£¬Ôò·ÃÎÊ» ......