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
Ã
Ïà¹ØÎĵµ£º
create table students (
name varchar(25),
class varchar(25),
grade int
)
insert into students values ('ÕÅÈý','ÓïÎÄ',20)
insert into students values ('ÕÅÈý','Êýѧ',90)
insert into students values ('ÕÅÈý','Ó¢Óï',50)
insert into students values ('ÀîËÄ','ÓïÎÄ',81)
insert in ......
1¡¢ SQL Server£ºÈëÕ¾¶Ë¿ÚΪTCP 1433£¬³öÕ¾¶Ë¿ÚΪTCPÈÎÒâ¶Ë¿Ú£¨1024¡«5000Ö®¼äµÄ¶Ë¿ÚºÅ£©£»
2¡¢ SQL Browser £ºÈëÕ¾¶Ë¿ÚΪUDP 1434£¬³öÕ¾¶Ë¿ÚΪUDPÈÎÒâ¶Ë¿Ú£¨1024¡«5000Ö®¼äµÄ¶Ë¿ÚºÅ£©£»
3¡¢²é¿´»òÐÞ¸ÄSQL2005ʵÀýµÄ¶Ë¿ÚºÅ£¬Èç¹ûϵͳÖÐͬʱ°²×°SQL 2000ºÍSQL 2005£¬SQL 2005µÄÁ¬½Ó¶Ë¿Ú¿ÉÄܲ»ÊÇ1433£¬ÎÒÃÇÐèÒª¸ü¸ÄSQL 2005 ......
ͬ²½Ò»ÏÂÎÒbaiduµÄblog£ºhttp://hi.baidu.com/ncheng/blog/item/bc65f82a1a6a7c335343c11c.html
Sql×¢ÈëÊÇÒ»ÖÖÈëÃż«µÍÆÆ»µ¼«´óµÄ¹¥»÷·½Ê½¡£Èç¹ûsqlÊÇÓÃ×Ö·û´®Æ´½Ó³öÀ´µÄ»°£¬ÄÇô¿Ï¶¨»á±»×¢Èë¹¥»÷£¬Ç°¶Îʱ¼ä»¹´«³öÁËij¹úÍâ´óÐÍÉç½»ÍøÕ¾±»SQL×¢Èë¹¥»÷¡£
Sql×¢Èë¹¥»÷µÄ·½Ê½£¬À´ÕâÀï¿´µÄͬ־ÃÇÓ¦¸ÃºÜÇå³þÁË£¬¾ÍÊÇÔÚÆ´½Ó×Ö·û´ ......
¿´¿´ÒÔÏÂSQLÓï¾ä£º
select row_number() over(partition by xs.xsbh, xs.kch order by coalesce(xs.bkxnxqh, xs.xnxqh) desc) rn
row_number()£º´ú±íÁÐ
partition by ´ú±í°´Ê²Ã´½øÐзÖ×é
order by¶Ôÿһ×éÐÅÏ¢½øÐÐÅÅÐò
coalesce()ÊÇÌæ»»µÄÒâ˼ ÀýÈ磺ÉÏÃæµÄSQLÓï¾äµÄÒâ˼ÊÇ£¬Èç¹ûbkxnxqhΪ¿Õ£¬ÄÇô¾ÍÈ¥xnx ......
Ç°Ò»¶Îʱ¼äÔÚά»¤Ò»¸öÀÏÏîÄ¿µÄʱºò·¢ÏÖ£¬ÔÚÖ´ÐÐÒ»¸ö½Ï´óµÄ´æ´¢¹ý³Ìʱ£¬×ÜÊDZ¨“³£¹æÍøÂç´íÎó£¬Çë¼ì²éÄúµÄÍøÂçÎĵµ”¡£ÔÚºóÐøµÄά»¤¼ì²éÖз¢ÏÖ£¬ÊÇÊÂÎﳬʱµ¼ÖÂÖ´ÐÐÖÐÖ¹µÄ¡£
ÏÂÃæÊǵ÷Õû¹ý³Ì£º
Ò»¡¢°ÑSQL SERVERµÄµÈ´ýÏìӦʱ¼ ......