1.使用if else語句
①在學(xué)生表中,查找名字為王剛的同學(xué),如果存在,顯示該同學(xué)的信息,否則顯示查無此人
if exists(select sno from student where sname='王剛')
else?
print '查無此人'
go
②查看有無選修00008號課程的記錄,如果有,則顯示有,并查詢選修00008號課程的人數(shù)
if exists(select * from sc where cno='00008')
begin
print '有'
select cno,count(cno) from sc where cno='00008' group by cno
end
提示
if else 語句只對后面的一條語句有效,如果后面要執(zhí)行的語句多于一條,那么這些語句要用begin ?end 括起來
2.使用while語句
①假設(shè)變量x的初始值為0,每次加1,直至x的值變?yōu)?
declare @x int
set @x=0
while
@x<3
begin?
set @x=@x+1
print 'x='+CONVERT(CHAR(1),@x)
end
go
//continue 語句的使用
declare @x int
set @x=0
while
@x<3
begin
set @x=@x+1
if(@x=2) continue
print 'x='+CONVERT(CHAR(1),@x)
end
go
//break 語句的使用
declare @x int
set @x=0
while
@x<3
begin
set @x=@x+1
print 'x='+CONVERT(CHAR(1),@x)
break
end
go
?
3、使用waitfor語句
①指示sqlserver等到當(dāng)天下午14:30:00,才能執(zhí)行查詢操作
use sm
go
waitfor time '14:30:00'
select * from student
go
②指示sqlserver等待10s后查詢student表
use sm?
go
waitfor delay '00:00:10'
select * from student
go
4.使用goto語句
在學(xué)生表中,查找名字為‘王剛’的同學(xué),如果存在現(xiàn)實該同學(xué)的信息;否則顯示”查無此人“
if exists(select sno from student where sname='王剛')
goto noation
else
begin
print '查無此人'
return
end
noation:
select * from student where sname='王剛'
go
5.使用case語句
使用case表達(dá)式,判斷ctno的值,如果為'00',則顯示'專業(yè)基礎(chǔ)課';如果為'01',則顯示‘公共基礎(chǔ)課’;如果為‘02’則顯示‘專業(yè)課’否則顯示‘待定’
use sm
go
select cno,cname,ctno=
case ctno
when '00' then '專業(yè)基礎(chǔ)課'
when '01' then '公共基礎(chǔ)課'
when '02' then '專業(yè)課'
when '03' then '待定'
end
from course
?6、使用raiserror語句
在屏幕上顯示一個信息,信息中列出當(dāng)前使用的數(shù)據(jù)庫標(biāo)識號和名稱,信息由格式化字符串直接給出
use NewPlat1
go
declare @dbid int
set @dbid=db_id()
declare @dbname nvarchar(128)
set @dbname=db_name()
raiserror('當(dāng)前數(shù)據(jù)庫的id值為:%d,數(shù)據(jù)庫名稱為%s',1,1,@dbid,@dbname)
go
?
?
?
?
?
?
?
?
?
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯(lián)系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機(jī)微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
