SQL Server 2005 EXCEPTºÍINTERSECTÔËËã·û
--> Title : SQL Server 2005EXCEPTºÍINTERSECTÔËËã·û
--> Author : wufeng4552
--> Date : 2009-10-30
(Ò»)¸ÅÄî
EXCEPTºÍINTERSECTÔËËã·ûʹÄú¿ÉÒԱȽÏÁ½¸ö»ò¶à¸öSELECTÓï¾äµÄ½á¹û²¢·µ»Ø·ÇÖظ´Öµ¡£
(¶þ)區別
EXCEPTÔËËã·û·µ»ØÓÉEXCEPTÔËËã·û×ó²àµÄ²éѯ·µ»Ø¡¢¶øÓÖ²»°üº¬ÔÚÓÒ²à²éѯËù·µ»ØµÄÖµÖеÄËùÓзÇÖظ´Öµ¡£
INTERSECT·µ»ØÓÉINTERSECTÔËËã·û×ó²àºÍÓÒ²àµÄ²éѯ¶¼·µ»ØµÄËùÓзÇÖظ´Öµ¡£
(Èý)×¢Òâ點
(3.1) ʹÓÃEXCEPT»òINTERSECT±È½ÏµÄ½á¹û¼¯±ØÐë¾ßÓÐÏàͬµÄ½á¹¹¡£ËüÃǵÄÁÐÊý±ØÐëÏàͬ£¬²¢ÇÒÏàÓ¦µÄ½á¹û¼¯ÁеÄÊý¾ÝÀàÐͱØÐë¼æÈÝ
(3.2) INTERSECTÔËËã·ûÓÅÏÈÓÚEXCEPT
(3.3) SELECT INTO±ØÐëÊÇ°üº¬INTERSECT»òEXCEPTÔËËã·ûµÄÓï¾äÖеĵÚÒ»¸ö²éѯ£¬ÓÃÀ´´´½¨ÈÝÄÉ×îÖÕ½á¹û¼¯µÄ±í
(3.4)ORDER BY×Ó¾äÖеÄÁÐÃû»ò±ðÃû±ØÐëÒýÓÃ×ó²à²éѯ·µ»ØµÄÁÐÃû
(ËÄ)實Àý
SET NOCOUNT ON
--½¨Á¢測試數據
DECLARE @T1 TABLE(ID INT,[NAME] VARCHAR(10))
INSERT @T1 SELECT 1,'A'
INSERT @T1 SELECT 1,'A'
INSERT @T1 SELECT 2,'B'
INSERT @T1 SELECT 2,'C'
INSERT @T1 SELECT 3,NULL
INSERT @T1 SELECT NULL,'D'
INSERT @T1 SELECT NULL,NULL
DECLARE @T2 TABLE(ID INT,[NAME] VARCHAR(10))
INSERT @T2 SELECT 1,'A'
INSERT @T2 SELECT 2,'E'
INSERT @T2 SELECT 3,'F'
DECLARE @T3 TABLE(ID INT,[NAME] VARCHAR(10))
INSERT @T3 SELECT 4,'G'
INSERT @T3 SELECT 3,NULL
INSERT @T3 SELECT 5,'H'
--1 EXCEPT´Ó×ó²éѯÖзµ»ØÓÒ²éѯ沒ÓÐÕÒµ½µÄËùÓзÇÖظ´Öµ¡£
--ÕÒ³ö@T1±íµÄÖв»´æÔÚ@T2±íµÄËùÓзÇÖظ´Öµ
SELECT * from @T1
EXCEPT
SELECT * from @T2
/*
ID NAME
----------- ----------
NULL NULL
NULL D
2 B
2 C
3 NULL
*/
--MSSQL2000µÄ°æ±¾ÓÃNOT EXISTSʵÏÖEXCEPTµÄ¹¦ÄÜ
SELECT DISTINCT * from @T1 T
WHERE NOT EXISTS(SELECT 1 from @T2 WHERE ID=T.ID AND [NAME]=T.[NAME])
/*
ID  
Ïà¹ØÎĵµ£º
SQLº¯ÊýÖ®ËÄÉáÎåÈ루ת×Ôhttp://ln1058.javaeye.com/blog/191502£©
ÎÊÌâ1£º
SELECT CAST('123.456' as decimal) ½«»áµÃµ½ 123£¨Ð¡ÊýµãºóÃæµÄ½«»á±»Ê¡ÂÔµô£©¡£
Èç¹ûÏ£ÍûµÃµ½Ð¡ÊýµãºóÃæµÄÁ½Î»¡£
ÔòÐèÒª°ÑÉÏÃæµÄ¸ÄΪ
SELECT CAST('123.456' as decimal(38, 2)) ===>123.46
×Ô¶¯ËÄÉáÎåÈëÁË£ ......
1.²»ÓÃÍ˳öSQL2000°²×°³ÌÐò,Ö±½ÓÇл»µ½×ÀÃæ.
¡¡¡¡2.´ò¿ª×¢²á±í±à¼Æ÷£¨ÔÚ“ÔËÐДÖÐÇÃÈë“regedit”Ö®ºó»Ø³µ¼´¿É£©£¬¶¨Î»µ½×¢²á±íµÄHKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager½Úµã.
¡¡¡¡3.µ¥»÷“Session Manager”Õâ¸ö½Úµã£¬ ......
---//´´½¨·ÖÇø
CREATE PARTITION FUNCTION MarketInfo_Bak_partfunc(int)
AS RANGE RIGHT
FOR VALUES (10000,25000,40000)
--//´´½¨·ÖÇø¼Ü¹¹
CREATE PARTITION SCHEME MarketInfo_Bak_partscheme
¡¡¡¡AS PARTITION MarketInfo_Bak_partfunc
¡¡¡¡TO (fg1, fg2, fg3, fg4) --//fg1...fg4Îļþ×éÃû
--//¶Ô±í½ø ......
1.Óà CUBE »ã×ÜÊý¾Ý
CUBE ÔËËã·ûÉú³ÉµÄ½á¹û¼¯ÊǶàάÊý¾Ý¼¯¡£¶àάÊý¾Ý¼¯ÊÇÊÂʵÊý¾ÝµÄÀ©Õ¹£¬ÊÂʵÊý¾Ý¼´¼Ç¼¸ö±ðʼþµÄÊý¾Ý¡£À©Õ¹½¨Á¢ÔÚÓû§´òËã·ÖÎöµÄÁÐÉÏ¡£ÕâЩÁб»³ÆΪά¡£¶àάÊý¾Ý¼¯ÊÇÒ»¸ö½á¹û¼¯£¬ÆäÖаüº¬Á˸÷ά¶ÈµÄËùÓпÉÄÜ×éºÏµÄ½»²æ±í¸ñ¡£ CUBE ÔËËã·ûÔÚ SELECT Óï¾äµÄ GROUP BY ×Ó¾äÖÐÖ¸¶¨¡£¸ÃÓï¾äµÄÑ¡ÔñÁбíÓ ......