sqlÌõ¼þÓï¾ä
Transact-SQL ÓïÑÔʹÓõÄÁ÷³Ì¿ØÖÆÃüÁîÓë³£¼ûµÄ³ÌÐòÉè¼ÆÓïÑÔÀàËÆÖ÷ÒªÓÐÒÔϼ¸ÖÖ¿ØÖÆÃüÁî¡£
4.6.1 IF…ELSE
Æä
Óï·¨ÈçÏ£º
IF <Ìõ¼þ±í´ïʽ>
<ÃüÁîÐлò³ÌÐò¿é>
[ELSE [Ìõ¼þ±í´ïʽ]
<ÃüÁîÐÐ
»ò³ÌÐò¿é>]
ÆäÖÐ<Ìõ¼þ±í´ïʽ>¿ÉÒÔÊǸ÷ÖÖ±í´ïʽµÄ×éºÏ£¬µ«±í´ïʽµÄÖµ±ØÐëÊÇÂß¼Öµ“Õæ”»ò“¼Ù”¡£ELSE×Ó¾äÊÇ¿ÉÑ¡µÄ£¬×î
¼òµ¥µÄIFÓï¾äûÓÐELSE×Ӿ䲿·Ö¡£IF…ELSEÓÃÀ´Åжϵ±Ä³Ò»Ìõ¼þ³ÉÁ¢Ê±Ö´ÐÐij¶Î³ÌÐò£¬Ìõ¼þ²»³ÉÁ¢Ê±Ö´ÐÐÁíÒ»¶Î³ÌÐò¡£Èç¹û²»Ê¹ÓóÌÐò¿é£¬IF»ò
ELSEÖ»ÄÜÖ´ÐÐÒ»ÌõÃüÁî¡£IF…ELSE¿ÉÒÔ½øÐÐǶÌס£
Àý4-9
declare@x
int,@y int,@z int
select @x=1,@y=2, @z=3
if@x>@y
print'x>y'
--´òÓ¡×Ö·û´®'x>y'
else if@y>@z
print'y>z'
else print'z>y'
ÔËÐнá¹ûÈçÏÂ
z>y
×¢Ò⣺ÔÚTransact-SQLÖÐ×î¶à¿ÉǶÌ×32¼¶¡£
4.6.2
BEGIN…END
ÆäÓï·¨ÈçÏ£º
BEGIN
<ÃüÁîÐлò³ÌÐò¿é>
END
BEGIN…ENDÓÃÀ´É趨
Ò»¸ö³ÌÐò¿é£¬½«ÔÚBEGIN…ENDÄÚµÄËùÓгÌÐòÊÓΪһ¸öµ¥ÔªÖ´ÐÐBEGIN…END¾³£ÔÚÌõ¼þÓï¾ä£¬ÈçIF…ELSEÖÐʹÓá£ÔÚBEGIN…ENDÖпÉ
ǶÌ×ÁíÍâµÄBEGIN…ENDÀ´¶¨ÒåÁíÒ»³ÌÐò¿é¡£
4.6.3 CASE
CASE ÃüÁîÓÐÁ½ÖÖÓï¾ä¸ñʽ:
CASE
<ÔËËãʽ>
WHEN <ÔËËãʽ>THEN<ÔËËãʽ>
…
WHEN<ÔËËã
ʽ>THEN<ÔËËãʽ>
[ELSE<ÔËËãʽ>]
END
CASE
WHEN
<Ìõ¼þ±í´ïʽ> THEN <ÔËËãʽ>
WHEN <Ìõ¼þ±í´ïʽ> THEN <ÔËËãʽ>
[ELSE
<ÔËËãʽ>]
END
CASEÃüÁî¿ÉÒÔǶÌ×µ½SQLÃüÁîÖС£
Àý4-10£ºµ÷ÕûÔ±¹¤¹¤×Ê£¬¹¤×÷¼¶±ðΪ“1”µÄÉϵ÷
8%£¬¹¤×÷¼¶±ðΪ“2”µÄÉϵ÷7%£¬¹¤×÷¼¶±ðΪ“3”µÄÉϵ÷6%£¬ÆäËüÉϵ÷5%¡£
use pangu
update employee
set
e_wage =
case
when job_level = ’1’ then e_wage*1.08
when
job_level = ’2’ then e_wage*1.07
when job_level = ’3’ then
e_wage*1.06
else e_wage*1.05
end
×¢Òâ:Ö´ÐÐCASE×Ó¾äʱ£¬Ö»ÔËÐеÚÒ»¸öÆ¥ÅäµÄ×ÓÃû¡£
4.6.4
WHILE…CONTINUE…BREAK
ÆäÓï·¨ÈçÏÂ:
WHILE <Ìõ¼þ±í´ïʽ>
BEGIN
<Ãü
ÁîÐлò³ÌÐò¿é>
[BREAK]
[CONTINUE]
[ÃüÁîÐлò³ÌÐò¿é]
END
WHILE
Ã
Ïà¹ØÎĵµ£º
ʹÓÃSQLÓï¾ä
SQLÓïÑÔ°üÀ¨Êý¾Ý²éѯÓïÑÔ(SELECT)¡¢Êý¾Ý²Ù×ÝÓïÑÔ(INSERT¡¢UPDATE¡¢DELETE)¡¢ÊÂÎñ¿ØÖÆÓïÑÔ(COMMIT¡¢ROLLBACK¡¢SAVEPOINT)¡¢Êý¾Ý¶¨ÒåÓïÑÔ(CREATE TABLE¡¢ALTER TABLE¡¢DROP)¡¢Êý¾Ý¿ØÖÆÓïÑÔ(GRANT¡¢REVOKE)µÈÎå¸ö²¿·Ö¡£
µ±±àдPL/SQLÓ¦ÓóÌÐòʱ£¬Ö»ÄÜÖ±½ÓǶÈëSELECT ......
±¾ÎÄ´Ó¶à¸ö½Ç¶È²ûÊöÁËSQL ServerÓë´æ´¢¹ý³ÌµÄ±È½Ï¡£
¢ÙΪʲôҪʹÓô洢¹ý³Ì£¿
ÒòΪËü±ÈSQLÓï¾äÖ´Ðп졣
¢Ú´æ´¢¹ý³ÌÊÇʲô?
°ÑÒ»¶ÑSQLÓï¾äÂÞÔÚÒ»Æð,»¹¿ÉÒÔ¸ù¾ÝÌõ¼þÖ´Ðв»Í¨SQLÓï¾ä¡£
¢ÛÀ´Ò»¸ö×î¼òµ¥µÄ´æ´¢¹ý³Ì£º
CREATE PROCEDURE dbo.testProcedure_AX
AS
select userID from
USERS order by userid ......
ÔÚ°²×°Visual Studio+SQL ServerµÄ¿ª·¢»·¾³µÄʱºòÍùÍù»áÓкܶà´íÎó£¬ÆäÖÐ×îÂé·³µÄÒ»Ìõ¾ÍËÀ°²×°Íê³ÉÖ®ºóûÓÐÊý¾Ý¿â¹ÜÀí¹¤¾ß£¬¼´Ã»ÓÐSQL Server 2005 Management Studio¡£³öÏÖÕâÑùµÄÎÊÌâÖ÷ÒªÓÐÁ½¸öÔÒò£¬Ò»ÊÇϵͳÒѾ´æÔÚÆäËû°æ±¾µÄSQLÊý¾Ý¿â»òÕßExpress°æ±¾£¬¶þÊÇ°²×°»·¾³µÄʱºòÏñÎÒÒ»ÑùÏÈ°²×°ÁËVisual Studio 2005/2008£¬È» ......
Ç°Ò»¶Îʱ¼äÔÚά»¤Ò»¸öÀÏÏîÄ¿µÄʱºò·¢ÏÖ£¬ÔÚÖ´ÐÐÒ»¸ö½Ï´óµÄ´æ´¢¹ý³Ìʱ£¬×ÜÊDZ¨“³£¹æÍøÂç´íÎó£¬Çë¼ì²éÄúµÄÍøÂçÎĵµ”¡£ÔÚºóÐøµÄά»¤¼ì²éÖз¢ÏÖ£¬ÊÇÊÂÎﳬʱµ¼ÖÂÖ´ÐÐÖÐÖ¹µÄ¡£
ÏÂÃæÊǵ÷Õû¹ý³Ì£º
Ò»¡¢°ÑSQL SERVERµÄµÈ´ýÏìӦʱ¼ ......
Æô¶¯ÓëÅäÖòÎÊý ¸ü¸ÄϵͳԪÊý¾ÝºÍ²ÎÊýÅäÖà ¶¯Ì¬ÊÓͼ ´æ´¢µÄÎïÀíºÍÂß¼½á¹¹ Æô¶¯ºÍ¹Ø±Õ
topÆô¶¯ÓëÅäÖòÎÊý
ÔÚSQL Server°²×°µÄʱºò£¬Í¬Ê±Ò²»áÍùWindows×¢²á±íÀïÃæÌí¼ÓһЩ¼Ç¼£¬ÕâЩע²á±í¼üÖµÖ¸¶¨ÁËʵÀýËùÐèÒªµÄ¸÷ÖÖ²ÎÊý¡£±ÈÈ磬ÓеÄ×¢²á±í¼üÖµÖ¸¶¨ÁË´íÎóÈÕÖ¾µÄ±£´æĿ¼£¬ÁíÍâµÄÖ¸¶¨ÁËĬÈϵı¸·ÝĿ¼µÈµÈ¡£SQL ServerÔÚÔ ......