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 ÃüÁîÔÚÉ趨µÄÌõ¼þ³ÉÁ
Ïà¹ØÎĵµ£º
sql2005ÖÐÒ»¸öxml¾ÛºÏµÄÀý×Ó ÊÕ²Ø
¸ÃÎÊÌâÀ´×ÔÂÛ̳ÌáÎÊ£¬ÑÝʾSQL´úÂëÈçÏÂ
--½¨Á¢²âÊÔ»·¾³
set nocount on
create table test(ID varchar(20),NAME varchar(20))
insert into test select '1','aaa'
insert into test select '1','bbb'
insert into test select '1','ccc'
insert into test select '2','ddd'
inser ......
(
8)
SELECT
(
9)
DISTINCT
(
11)
<
TOP_specification>
<
select_list>
(
1)
from
<
left_table>
(
3)
<
join_type>
JOIN
<
right_table>
(
2)
ON
<
join_codition>
(
4)
WHERE
<
where_condition>
(
5)
GROUP
BY
& ......
XËø¼´ÊÇÎÒÃdz£ËµµÄÅÅËüËø£¬UËøºÜÉÙ¼û£¬Ö»ÔÚ¸üÐÂʱ»á³öÏÖ¡£UËøÆäʵÊÇXËøµÄÒ»ÖÖ£¬µ±Êý¾Ý¸üÐÂʱ£¬UËøÊ×Ïȼӵ½Òª¸üеÄÊý¾ÝÉÏ(¼´Ê¹Ä¿±êÉÏÃæÓÐSËø£¬UËø»¹ÊÇ¿ÉÒÔ¼ÓÉÏ)£¬µÈÄ¿±ê¿ÉÒÔ±»¸üÐÂʱ£¬UËø¼´×ª±äΪXËø¡£ ÈçÐèתÔØ£¬Çë×¢Ã÷±¾ÎÄÔ´´×ÔCSDN TJVictorרÀ¸£ºhttp://blog.csdn.net/tjvictor ......
ÓÐÁ½ÖÖ·½·¨¶¯Ì¬ÖرàÒë´æ´¢¹ý³Ì£º 1.ÔÚCreateʱ¼ÓÉÏRECOMPILEÑ¡Ïî CREATE PROCEDURE dbo.PersonAge (@MinAge INT, @MaxAge INT)
WITH RECOMPILE
AS
SELECT *
from dbo.tblTable 2.ÔÚÖ´ÐÐʱ¼ÓÉÏRECOMPILEÑ¡Ïî EXEC dbo.PersonAge 65,70 WITH RECOMPILE ²»ÍƼöʹÓõڶþÖÖ·½·¨£¬ÓÈÆäÔÚÉú²ú»·¾³ ......