亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

輸出,變量的使用,子查詢,邏輯語句,循環(huán),ca

系統(tǒng) 2660 0

--------------輸出----------------
print 'hello world'--以文本形式輸出
select 'hello world'--以網(wǎng)格形式輸出,也可以設(shè)置成以文本形式輸出
print 'abc'+'cde'
print 3+5
print 'ab'+5--出錯,'ab'不能轉(zhuǎn)換為int
print 'ab'+convert(varchar,5)--輸出ab5
print '2'+5--輸出7,因為'2'能自動轉(zhuǎn)換為整型數(shù)據(jù)2

----------------局部變量-------------------
--聲明局部變量
declare @age int
---set賦值(set一次只能給一個局部變量賦值)
set @age=22
set @age=@age+5
print @age

--select賦值(select一次可以給多個局部變量賦值)
declare @stuAge int
declare @stuName nvarchar(20)
select @stuName=stuName,@stuAge=stuAge from stuInfo where stuNo='s25302'
print '姓名是:'+@stuName+' 年齡是:'+convert(varchar,@stuAge)
--注意:在使用select賦值時,查出來的數(shù)據(jù)行最好是一行,如果查出來多行,會以最后一行的值來進行賦值


----------------全局變量------------------
print @@version--版本信息
print @@servername--本地服務(wù)器名稱
insert into stuInfo values('張三','s25305','男',23,'汕頭')
print @@error --最后一個T-sql語句的錯誤號(如果最后一個T-sql語句執(zhí)行失敗,@@error的值會大于0,執(zhí)行成功,@@error的值會等于0)
print @@identity--獲取最后一個插入行的標(biāo)識列的值
update stuInfo set stuAge=32 where stuAge=22
print @@rowcount--受上一個sql語句影響的行數(shù)

---------------IF-ELSE分支結(jié)構(gòu)---------------------
use NetBarDB
declare @pcid int--計算機號
set @pcid=3
declare @pcuse int --計算機狀態(tài)
select @pcuse=PCUse from PCInfo where PCId=@pcid
if(@pcuse>0)
begin
?? ?print convert(varchar,@pcid)+'號是使用狀態(tài)!'
end
else
begin
?? ?print convert(varchar,@pcid)+'號是空閑狀態(tài)!'
end


------------while循環(huán)語句--------------
--完成:網(wǎng)吧回饋業(yè)務(wù)
use NetBarDB
declare @count int--存儲余額小于20的用戶數(shù)
update cardInfo set CardBalance=CardBalance+50 where DATEDIFF(DAY,TransactTime,GETDATE())>=365
update cardInfo set CardBalance=CardBalance+10 where DATEDIFF(DAY,TransactTime,GETDATE())<365
while(1=1)
begin
?? ?select @count=COUNT(*) from cardInfo where CardBalance<20--查出余額不足20元的行數(shù)
?? ?if(@count>0)
?? ?begin
?? ??? ?update cardInfo set CardBalance=CardBalance+1
?? ?end
?? ?else
?? ?begin
?? ??? ?break
?? ?end
end
go


---------case..when..then..end多分支語句-----------
--完成:計算機狀態(tài)問題
--方法一:union
select *,'空閑' as 狀態(tài) from PCInfo where PCUse=0
union
select *,'使用' as 狀態(tài) from PCInfo where PCUse=1
--方法二:case..when..then..end
select *,
狀態(tài)=case
when PCUse=0 then '空閑'
when PCUse=1 then '使用'
else '錯誤狀態(tài)'
end
from PCInfo


--------------子查詢------------------
--完成:年齡比'李斯文'大的學(xué)員信息
--方法一:普通T-SQL
use stuDB
declare @age int
select @age=stuAge from stuInfo where stuName='李斯文'? --先拿到'李斯文'的年齡
select * from stuInfo where stuAge>@age? --再以'李斯文'的年齡作為篩選條件
--方法二:子查詢
select * from stuInfo where stuAge>
(select stuAge from stuInfo where stuName='李斯文')
--特別注意:子查詢與<、>、<=、>=...等關(guān)系運算符一起使用時,一定要確保子查詢返回的值不多于一個,否則報錯

--完成:筆試成績剛好60分的學(xué)員信息
--方法一:表連接
select stuName from stuInfo join stuMarks
on stuInfo.stuNo=stuMarks.stuNo
where stuMarks.writtenExam=60
--方法二:子查詢
select stuName from stuInfo where stuNo in
(select stuNo from stuMarks where writtenExam=60)

--完成:查詢有參加考試的學(xué)員名單
select stuName from stuInfo where stuNo in
(select stuNo from stuMarks)
--上面的sql語句相當(dāng)于: select stuName from stuInfo where stuNo in ('s25303','s25302','s25301')

--完成:查詢沒參加考試的學(xué)員名單
select stuName from stuInfo where stuNo not in
(select stuNo from stuMarks)


--------------Exists的使用-----------------------
--語法: Exists(子查詢)
--返回值:當(dāng)子查詢能查到數(shù)據(jù),返回true,如果子查詢查不到數(shù)據(jù),返回false
use stuDB
if exists(select * from stuMarks where writtenExam>80)--判斷是否有筆試超過80分的
begin
?? ?update stuMarks set writtenExam=writtenExam+2
end
else
begin
?? ?update stuMarks set writtenExam=writtenExam+5
end

--------------not Exists的使用-----------------------
--語法: not Exists(子查詢)
--返回值:當(dāng)子查詢能查到數(shù)據(jù),返回false,如果子查詢查不到數(shù)據(jù),返回true
use stuDB
if not exists(select * from stuMarks where writtenExam>60 and LabExam>60)
begin
?? ?update stuMarks set writtenExam=writtenExam+3,LabExam=LabExam+3
end
else
begin
?? ?update stuMarks set writtenExam=writtenExam+1,LabExam=LabExam+1
end
go

輸出,變量的使用,子查詢,邏輯語句,循環(huán),case..when..then..end多分支語句,Exists(判斷存在)


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯(lián)系: 360901061

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

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發(fā)表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 奇米影视盒7777 | 成人午夜在线视频 | 国产亚洲精品精品国产亚洲综合 | 免费精品久久久久久中文字幕 | 成人交性视频免费看 | 亚洲综合国产 | 高清在线一区二区三区亚洲综合 | 国产高清在线视频一区二区三区 | 国产中的精品一区的 | 国产精品女在线观看 | 在线欧美亚洲 | 欧洲一级毛片免费 | 天天操天天干视频 | 91精品国产三级在线观看 | 四虎成人精品在永久免费 | 天天想天天干 | 久久不射网| 四虎成人精品 | 成年黄网站免费大全毛片 | 狠狠狠色| 日本欧美一区二区三区在线 | 91亚洲精品视频 | 亚洲久草 | 成人观看网站a | 国产精品视频男人的天堂 | 四虎影视地址 | 久久99中文字幕久久 | 国产精品一区二区资源 | 日日拍夜夜嗷嗷叫国产 | 天天爽夜夜爽天天做夜夜做 | 新香蕉视频在线 | 国产羞羞羞视频在线观看 | 亚洲一区二区三区高清 | 婷婷在线观看网站 | a级毛片高清免费视频 | 四虎精品在线 | 一及 片日本 | 国产精品夜色视频一级区 | 国产区综合另类亚洲欧美 | 91久久国产青草亚洲 | 日韩毛片高清免费 |