ÎåÖÖÌá¸ß SQL ÐÔÄܵķ½·¨
±¾ÎĽÚÑ¡×ÔMSDNµÄÎÄÕ¡¶ÎåÖÖÌá¸ß SQL ÐÔÄܵķ½·¨¡·£¬Ìá³öÈçºÎÌá¸ß»ùÓÚSQL ServerÓ¦ÓóÌÐòµÄÔËÐÐЧÂÊ£¬·Ç³£ÖµµÃÍƼö¡£¶ÔһЩTrafficºÜ¸ßµÄÓ¦ÓÃϵͳ¶øÑÔ£¬ÈçºÎÌá¸ßºÍ¸Ä½øSQLÖ¸ÁÊǷdz£ÖØÒªµÄ£¬Ò²ÊÇÒ»¸öºÜºÃµÄÍ»ÆƵ㡣
*ÎÄÕÂÖ÷Òª°üÀ¨ÈçÏÂһЩÄÚÈÝ£¨Èç¸ÐÐËȤ£¬ÇëÖ±½Ó·ÃÎÊÏÂÃæµÄURLÔĶÁÍêÕûµÄÖÐÓ¢ÎÄÎĵµ£©£º
1, ´Ó INSERT ·µ»Ø IDENTITY
SELECT @@IDENTITY
2, ÄÚǶÊÓͼÓëÁÙʱ±í
ÁÙʱ±í £ ÔÚ tempdb ÖеÄÁÙʱ±í»áµ¼Ö²éѯ½øÐдóÁ¿ I/O ²Ù×÷ºÍ´ÅÅÌ·ÃÎÊ£¬ÁÙʱ±í»áÏûºÄ´óÁ¿×ÊÔ´¡£
ÄÚǶÊÓͼ £Ê¹ÓÃÄÚǶÊÓͼȡ´úÁÙʱ±í¡£ÄÚǶÊÓͼֻÊÇÒ»¸ö¿ÉÒÔÁª½Óµ½ from ×Ó¾äÖеIJéѯ¡£Èç¹ûÖ»ÐèÒª½«Êý¾ÝÁª½Óµ½ÆäËû²éѯ£¬Ôò¿ÉÒÔÊÔÊÔʹÓÃÄÚǶÊÓͼ£¬ÒÔ½ÚÊ¡×ÊÔ´¡£
3, ±ÜÃâ LEFT JOIN ºÍ NULL
LEFT
JOIN ÏûºÄµÄ×ÊÔ´·Ç³£Ö®¶à£¬ÒòΪËüÃÇ°üº¬Óë NULL£¨²»´æÔÚ£©Êý¾ÝÆ¥ÅäµÄÊý¾Ý¡£ÔÚijЩÇé¿öÏ£¬ÕâÊDz»¿É±ÜÃâµÄ£¬µ«ÊÇ´ú¼Û¿ÉÄܷdz£¸ß¡£LEFT
JOIN ±È INNER JOIN ÏûºÄ×ÊÔ´¸ü¶à£¬ËùÒÔÈç¹ûÄú¿ÉÒÔÖØбàд²éѯÒÔʹµÃ¸Ã²éѯ²»Ê¹ÓÃÈκΠLEFT JOIN£¬Ôò»áµÃµ½·Ç³£¿É¹ÛµÄ»Ø±¨¡£
¼Ó¿ìʹÓà LEFT JOIN µÄ²éѯËٶȵÄÒ»Ïî¼¼ÊõÉæ¼°´´½¨Ò»¸ö TABLE Êý¾ÝÀàÐÍ
£¬
²åÈëµÚÒ»¸ö±í£¨LEFT JOIN ×ó²àµÄ±í£©ÖеÄËùÓÐÐУ¬È»ºóʹÓõڶþ¸ö±íÖеÄÖµ¸üРTABLE Êý¾ÝÀàÐÍ¡£´Ë¼¼ÊõÊÇÒ»¸öÁ½²½µÄ¹ý³Ì£¬µ«Óë±ê×¼µÄ
LEFT JOIN
Ïà±È£¬¿ÉÒÔ½ÚÊ¡´óÁ¿Ê±¼ä¡£Ò»¸öºÜºÃµÄ¹æÔòÊdz¢ÊÔ¸÷ÖÖ²»Í¬µÄ¼¼Êõ²¢¼Ç¼ÿÖÖ¼¼ÊõËùÐèµÄʱ¼ä£¬Ö±µ½»ñµÃÓÃÓÚÄúµÄÓ¦ÓóÌÐòµÄÖ´ÐÐÐÔÄÜ×î¼ÑµÄ²éѯ¡£
DECLARE @tblMonths TABLE
(sMonth VARCHAR(7))
4, Áé»îʹÓõѿ¨¶û³Ë»ý
¶Ô
Óڴ˼¼ÇÉ£¬ÎÒ½«½øÐзdz£ÏêϸµÄ½éÉÜ£¬²¢ÌᳫÔÚijЩÇé¿öÏÂʹÓõѿ¨¶û³Ë»ý¡£³öÓÚijЩÔÒò£¬µÑ¿¨¶û³Ë»ý (CROSS JOIN)
Ôâµ½Á˺ܶàÇ´Ô𣬿ª·¢ÈËԱͨ³£»á±»¾¯¸æ¸ù±¾¾Í²»ÒªÊ¹ÓÃËüÃÇ¡£ÔÚÐí¶àÇé¿öÏ£¬ËüÃÇÏûºÄµÄ×ÊÔ´Ì«¶à£¬´Ó¶øÎÞ·¨¸ßЧʹÓᣵ«ÊÇÏñ SQL
ÖеÄÈκι¤¾ßÒ»Ñù£¬Èç¹ûÕýȷʹÓã¬ËüÃÇÒ²»áºÜÓмÛÖµ¡£
ÆäÖÐÒ»¶ÎʾÀý´úÂ룬ֵµÃЧ·Â£º
--
µÑ¿¨¶û³Ë»ýÔò¿ÉÒÔ·µ»ØËùÓÐÔ·ݵÄËùÓпͻ§¡£µÑ¿¨¶û³Ë»ý»ù±¾ÉÏÊǽ«µÚÒ»¸ö±íÓëµÚ¶þ¸ö±íÏà³Ë£¬Éú³ÉÒ»¸öÐм¯ºÏ£¬ÆäÖаüº¬µÚÒ»¸ö±íÖеÄÐÐÊýÓëµÚ¶þ¸ö±íÖеÄÐÐÊýÏà
³ËµÄ½á¹û¡£Òò´Ë£¬µÑ¿¨¶û³Ë»ý»áÏò±í @tblFinal ·µ»Ø 12£¨ËùÓÐÔ·ݣ©*81£¨ËùÓпͻ§£©=972
ÐС£×îºóµÄ²½ÖèÊÇʹÓôËÈÕÆÚ·¶Î§ÄÚÿ¸ö¿Í»§µÄÔÂÏúÊÛ¶î×ܼƸüР@tblFinal ±í£¬ÒÔ¼°Ñ¡Ôñ×îÖÕµÄÐм¯¡£
DECLARE @tblMonths TABLE (sMont
Ïà¹ØÎĵµ£º
ÔÚ½øÐÐÊý¾Ý¿â²Ù×÷ʱ£¬Î޷ǾÍÊÇÌí¼Ó¡¢É¾³ý¡¢Ð޸ģ¬ÕâµÃÉè¼Æµ½Ò»Ð©³£ÓõÄSQLÓï¾ä£¬ÈçÏ£º
SQL³£ÓÃÃüÁîʹÓ÷½·¨£º
(1) Êý¾Ý¼Ç¼ɸѡ£º
sql="select * from Êý¾Ý±í where ×Ö¶ÎÃû=×Ö¶ÎÖµ order by ×Ö¶ÎÃû [desc]"
sql="select * from Êý¾Ý±í where ×Ö¶ÎÃû like %×Ö¶ÎÖµ% order by ×Ö¶ÎÃû [desc]"
sql="select top 10 * fro ......
--Óï ¾ä¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¹¦ ÄÜ
--Êý¾Ý²Ù×÷
SELECT¡¡¡¡¡¡--´ÓÊý¾Ý¿â±íÖмìË÷Êý¾ÝÐкÍÁÐ
INSERT¡¡¡¡¡¡--ÏòÊý¾Ý¿â±íÌí¼ÓÐÂÊý¾ÝÐÐ
DELETE¡¡¡¡¡¡--´ÓÊý¾Ý¿â±íÖÐɾ³ýÊý¾ÝÐÐ
UPDATE¡¡¡¡¡¡--¸üÐÂÊý¾Ý¿â±íÖеÄÊý¾Ý
--Êý¾Ý¶¨Òå
CREATE TABLE¡¡¡¡--´´½¨Ò»¸öÊý¾Ý¿â±í
DROP TABLE¡¡¡¡ --´ÓÊý¾Ý¿âÖÐɾ³ý±í
ALTER ......
1.×Ö·û´®º¯Êý £º
datalength(Char_expr) ·µ»Ø×Ö·û´®°üº¬×Ö·ûÊý,µ«²»°üº¬ºóÃæµÄ¿Õ¸ñ
length(expression,variable)Ö¸¶¨×Ö·û´®»ò±äÁ¿Ãû³ÆµÄ³¤¶È¡£
substring(expression,start,length) ²»¶à˵ÁË,È¡×Ó´®
right(char_expr,int_expr) ·µ»Ø×Ö·û´®ÓÒ±ßint_expr¸ö×Ö·û
concat(str1,str2,...)·µ»ØÀ´×ÔÓÚ²ÎÊýÁ¬½áµÄ×Ö·û´®¡£dat ......
SQL SERVERµÄ·ÖÀà»ã×Ü
SQL SERVERÖÐʹÓÃGROUP BY¶ÔÊý¾Ý½øÐзÖÀà»ã×Ü£¬ÎÒÃÇÒ²¿ÉÒÔʹÓÃWITH ROLLUPºÍWITH CUBEÅäºÏGROUP BY½øÐГÔöÇ¿”Á˵ķÖÀà»ã×Ü£¬ÄÇôËûÃÇÁ½¸öÊÇÈçºÎÔöÇ¿GROUP BYµÄ»ã×ÜÄÜÁ¦µÄÄØ£¿
Ò».¹¦ÄÜÔöÇ¿
1.ʹÓÃWITH ROLLUP
ÓÃÏÂÃæµÄÀý×Ó˵Ã÷,GROUPʹÓÃÁË3¸ö·Ö×é×ֶΣºGROUP BY A, B, C WITH ROLLUP ......
±¾ÎĽéÉÜÁËSQL Server 2008ÀïһЩеÄÌصãÒÔ¼°·¢ÏÖµÄһЩ¾«ÃîÖ®´¦……
¡¡¡¡°²×°
¡¡¡¡SQL
Server
2008µÄÉèÖúͰ²×°Ò²ÓÐËù¸Ä½ø¡£ÅäÖÃÊý¾ÝºÍÒýÇæλÒѾ·Ö¿ªÁË£¬ËùÒÔËüʹ´´½¨»ù±¾µÄδÅäÖÃϵͳµÄ´ÅÅÌͼÏñ±äµÃ¿ÉÄÜÁË£¬Ëüʹ·Ö²¼µ½¶à¸ö·þÎñÆ÷±äµÃ¸üÈÝÒ×ÁË¡£
´Ó΢ÈíµÄÕ¾µãÒ²¿ÉÒÔÕÒµ½°²×°¿ÉÓõÄ×îиüС£ÁíÒ»¸öÌص ......