--------------輸出----------------
print 'hello world'--以文本形式輸出
select 'hello world'--以網格形式輸出,也可以設置成以文本形式輸出
print 'abc'+'cde'
print 3+5
print 'ab'+5--出錯,'ab'不能轉換為int
print 'ab'+convert(varchar,5)--輸出ab5
print '2'+5--輸出7,因為'2'能自動轉換為整型數據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賦值時,查出來的數據行最好是一行,如果查出來多行,會以最后一行的值來進行賦值
----------------全局變量------------------
print @@version--版本信息
print @@servername--本地服務器名稱
insert into stuInfo values('張三','s25305','男',23,'汕頭')
print @@error --最后一個T-sql語句的錯誤號(如果最后一個T-sql語句執行失敗,@@error的值會大于0,執行成功,@@error的值會等于0)
print @@identity--獲取最后一個插入行的標識列的值
update stuInfo set stuAge=32 where stuAge=22
print @@rowcount--受上一個sql語句影響的行數
---------------IF-ELSE分支結構---------------------
use NetBarDB
declare @pcid int--計算機號
set @pcid=3
declare @pcuse int --計算機狀態
select @pcuse=PCUse from PCInfo where PCId=@pcid
if(@pcuse>0)
begin
?? ?print convert(varchar,@pcid)+'號是使用狀態!'
end
else
begin
?? ?print convert(varchar,@pcid)+'號是空閑狀態!'
end
------------while循環語句--------------
--完成:網吧回饋業務
use NetBarDB
declare @count int--存儲余額小于20的用戶數
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元的行數
?? ?if(@count>0)
?? ?begin
?? ??? ?update cardInfo set CardBalance=CardBalance+1
?? ?end
?? ?else
?? ?begin
?? ??? ?break
?? ?end
end
go
---------case..when..then..end多分支語句-----------
--完成:計算機狀態問題
--方法一:union
select *,'空閑' as 狀態 from PCInfo where PCUse=0
union
select *,'使用' as 狀態 from PCInfo where PCUse=1
--方法二:case..when..then..end
select *,
狀態=case
when PCUse=0 then '空閑'
when PCUse=1 then '使用'
else '錯誤狀態'
end
from PCInfo
--------------子查詢------------------
--完成:年齡比'李斯文'大的學員信息
--方法一:普通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='李斯文')
--特別注意:子查詢與<、>、<=、>=...等關系運算符一起使用時,一定要確保子查詢返回的值不多于一個,否則報錯
--完成:筆試成績剛好60分的學員信息
--方法一:表連接
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)
--完成:查詢有參加考試的學員名單
select stuName from stuInfo where stuNo in
(select stuNo from stuMarks)
--上面的sql語句相當于: select stuName from stuInfo where stuNo in ('s25303','s25302','s25301')
--完成:查詢沒參加考試的學員名單
select stuName from stuInfo where stuNo not in
(select stuNo from stuMarks)
--------------Exists的使用-----------------------
--語法: Exists(子查詢)
--返回值:當子查詢能查到數據,返回true,如果子查詢查不到數據,返回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(子查詢)
--返回值:當子查詢能查到數據,返回false,如果子查詢查不到數據,返回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
輸出,變量的使用,子查詢,邏輯語句,循環,case..when..then..end多分支語句,Exists(判斷存在)
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

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