SQLÖеÄCASEʹÓ÷½·¨
Case¾ßÓÐÁ½ÖÖ¸ñʽ¡£¼òµ¥Caseº¯ÊýºÍCaseËÑË÷º¯Êý¡£
--¼òµ¥Caseº¯Êý
CASE sex
WHEN '1' THEN 'ÄÐ'
WHEN '2' THEN 'Å®'
ELSE 'ÆäËû' END
--CaseËÑË÷º¯Êý
CASE WHEN sex = '1' THEN 'ÄÐ'
WHEN sex = '2' THEN 'Å®'
ELSE 'ÆäËû' END
ÕâÁ½ÖÖ·½Ê½£¬¿ÉÒÔʵÏÖÏàͬµÄ¹¦ÄÜ¡£¼òµ¥Caseº¯ÊýµÄд·¨Ïà¶Ô±È½Ï¼ò½à£¬µ«ÊǺÍCaseËÑË÷º¯ÊýÏà±È£¬¹¦ÄÜ·½Ãæ»áÓÐЩÏÞÖÆ£¬±ÈÈçдÅжÏʽ¡£
»¹ÓÐÒ»¸öÐèҪעÒâµÄÎÊÌ⣬Caseº¯ÊýÖ»·µ»ØµÚÒ»¸ö·ûºÏÌõ¼þµÄÖµ£¬Ê£ÏµÄCase²¿·Ö½«»á±»×Ô¶¯ºöÂÔ¡£
--±ÈÈç˵£¬ÏÂÃæÕâ¶ÎSQL£¬ÄãÓÀÔ¶ÎÞ·¨µÃµ½“µÚ¶þÀà”Õâ¸ö½á¹û
CASE WHEN col_1 IN ( 'a', 'b') THEN 'µÚÒ»Àà'
WHEN col_1 IN ('a') THEN 'µÚ¶þÀà'
ELSE'ÆäËû' END
ÏÂÃæÎÒÃÇÀ´¿´Ò»Ï£¬Ê¹ÓÃCaseº¯Êý¶¼ÄÜ×öЩʲôÊÂÇé¡£
Ò»£¬ÒÑÖªÊý¾Ý°´ÕÕÁíÍâÒ»ÖÖ·½Ê½½øÐзÖ×飬·ÖÎö¡£
ÓÐÈçÏÂÊý¾Ý:(ΪÁË¿´µÃ¸üÇå³þ£¬ÎÒ²¢Ã»ÓÐʹÓùú¼Ò´úÂ룬¶øÊÇÖ±½ÓÓùú¼ÒÃû×÷ΪPrimary Key)
¹ú¼Ò£¨country£© ÈË¿Ú£¨population£©
Öйú 600
ÃÀ¹ú 100
¼ÓÄôó 100
Ó¢¹ú 200
·¨¹ú 300
ÈÕ±¾ 250
µÂ¹ú 200
Ä«Î÷¸ç 50
Ó¡¶È 250
¸ù¾ÝÕâ¸ö¹ú¼ÒÈË¿ÚÊý¾Ý£¬Í³¼ÆÑÇÖ޺ͱ±ÃÀÖÞµÄÈË¿ÚÊýÁ¿¡£Ó¦¸ÃµÃµ½ÏÂÃæÕâ¸ö½á¹û¡£
ÖÞ ÈË¿Ú
ÑÇÖÞ 1100
±±ÃÀÖÞ 250
ÆäËû 700
ÏëÒª½â¾öÕâ¸öÎÊÌ⣬Äã»áÔõô×ö£¿Éú³ÉÒ»¸ö´øÓÐÖÞCodeµÄView£¬ÊÇÒ»¸ö½â¾ö·½·¨£¬µ«ÊÇÕâÑùºÜÄѶ¯Ì¬µÄ¸Ä±äͳ¼ÆµÄ·½Ê½¡£
Èç¹ûʹÓÃCaseº¯Êý£¬SQL´úÂëÈçÏÂ:
SELECT SUM(population),
CASE country
WHEN 'Öйú' THEN 'ÑÇÖÞ'
WHEN 'Ó¡¶È' &
Ïà¹ØÎĵµ£º
sql ·þÎñÆ÷½ÇÉ« Êý¾Ý¿â½ÇÉ«
·þÎñÆ÷½ÇÉ«
ËùÓеķþÎñÆ÷½ÇÉ«¶¼ÊÇ“¹Ì¶¨µÄ”½ÇÉ«£¬²¢ÇÒ£¬´ÓÒ»¿ªÊ¼¾Í´æÔÚÓÚÄÇÀï——×Ô°²×°ÍêSQL ServerµÄÄÇÒ»¿ÌÆð£¬Ä㽫ӵÓеÄËùÓзþÎñÆ÷½ÇÉ«¾ÍÒѾ´æÔÚÁË¡£
½Ç¡¡¡¡É«
ÌØ¡¡¡¡ÐÔ
sysadmin
¸Ã½ÇÉ«Äܹ»Ö´ÐÐSQL ServerÉϵÄÈκβÙ× ......
Èç¹ûÄãµÄÉí·ÝÑéÖ¤·½Ê½ÎªWindows¼¯³ÉÉí·ÝÑéÖ¤¸ü¸ÄΪ»ìºÏ·½Ê½µÄÉí·ÝÑéÖ¤£¬ÐèÒªÔÚSQL Server Configuration ManagementÖУ¬°ÑTCP/IPµÄ»î¶¯ÆôÓã¬Ò²¾ÍÊǵÚÒ»¸öÑ¡ÔñµÄµØ·½£¬Ñ¡ÔñÆôÓᣲ¢ÇÒʹÓü¯³ÉÉí·ÝÑéÖ¤·½Ê½µÇ¼ºó£¬ÔÚÊý¾Ý¿â·þÎñÆ÷ÉÏÑ¡ÔñÊôÐÔ£¬°Ñ°²È«ÐÔϵÄWINDOWSģʽ¸ÄΪSQLÓëWINDOWSģʽ ......
º¯ÊýÈçÏÂ
CREATE FUNCTION StockBalance_AmountIn(@SortID int)
RETURNS numeric(18,4)
AS
BEGIN
Declare @dblReturn numeric(18,4)
Select @dblReturn = SUM(AmountCurrentMonthIn) from AT_Materiel_StockBalance Where SortID = @SortID
Return (@dblReturn)
END
......
SQL code
/*----------------------------------------------------------------
-- Author :feixianxxx(poofly)
-- Date :2010-04-20 20:10:41
-- Version:
-- Microsoft SQL Server 2008 (SP1) - 10.0.2531.0 (Intel X86)
Mar 29 2009 10:27:29
Copyright (c) 1988-2008 Microsoft Co ......