--------------輸出----------------
print 'hello world'--以文本形式輸出
select 'hello world'--以網(wǎng)格形式輸出,也可以設(shè)置成以文本形式輸出
print 'abc'+'cde'
print 3+5
print 'ab'+5--出錯(cuò),'ab'不能轉(zhuǎn)換為int
print 'ab'+convert(varchar,5)--輸出ab5
print '2'+5--輸出7,因?yàn)?2'能自動(dòng)轉(zhuǎn)換為整型數(shù)據(jù)2
----------------局部變量-------------------
--聲明局部變量
declare @age int
---set賦值(set一次只能給一個(gè)局部變量賦值)
set @age=22
set @age=@age+5
print @age
--select賦值(select一次可以給多個(gè)局部變量賦值)
declare @stuAge int
declare @stuName nvarchar(20)
select @stuName=stuName,@stuAge=stuAge from stuInfo where stuNo='s25302'
print '姓名是:'+@stuName+' 年齡是:'+convert(varchar,@stuAge)
--注意:在使用select賦值時(shí),查出來的數(shù)據(jù)行最好是一行,如果查出來多行,會(huì)以最后一行的值來進(jìn)行賦值
----------------全局變量------------------
print @@version--版本信息
print @@servername--本地服務(wù)器名稱
insert into stuInfo values('張三','s25305','男',23,'汕頭')
print @@error --最后一個(gè)T-sql語句的錯(cuò)誤號(hào)(如果最后一個(gè)T-sql語句執(zhí)行失敗,@@error的值會(huì)大于0,執(zhí)行成功,@@error的值會(huì)等于0)
print @@identity--獲取最后一個(gè)插入行的標(biāo)識(shí)列的值
update stuInfo set stuAge=32 where stuAge=22
print @@rowcount--受上一個(gè)sql語句影響的行數(shù)
---------------IF-ELSE分支結(jié)構(gòu)---------------------
use NetBarDB
declare @pcid int--計(jì)算機(jī)號(hào)
set @pcid=3
declare @pcuse int --計(jì)算機(jī)狀態(tài)
select @pcuse=PCUse from PCInfo where PCId=@pcid
if(@pcuse>0)
begin
?? ?print convert(varchar,@pcid)+'號(hào)是使用狀態(tài)!'
end
else
begin
?? ?print convert(varchar,@pcid)+'號(hào)是空閑狀態(tài)!'
end
------------while循環(huán)語句--------------
--完成:網(wǎng)吧回饋業(yè)務(wù)
use NetBarDB
declare @count int--存儲(chǔ)余額小于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多分支語句-----------
--完成:計(jì)算機(jī)狀態(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 '錯(cuò)誤狀態(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)系運(yùn)算符一起使用時(shí),一定要確保子查詢返回的值不多于一個(gè),否則報(bào)錯(cuò)
--完成:筆試成績(jī)剛好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號(hào)聯(lián)系: 360901061
您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對(duì)您有幫助就好】元
