MS SQL高階——邏輯語句與子查詢
阿新 • • 發佈:2022-05-29
邏輯語句與子查詢
變數的分類
區域性變數:(僅在過程中使用)
- 區域性變數必須以標記@作為字首,如@age。區域性變數的使用也是先宣告(使用declare),再賦值。
全域性變數:(任何時候均可以使用)
- 全域性變數必須以標記@@作為字首,如@@version。全域性變數由系統定義和維護,我們只能讀取,不能修改全域性變數值。
區域性變數定義與賦值
區域性變數的定義語法
DECLARE @變數名 資料型別
賦值方法
- SET @變數名=值
- SELECT @變數名=值
使用Select賦值確保篩選出的記錄只有1條
問題:編寫T-SQL查詢李銘及其學號相鄰的學員?
區域性變數的使用
程式碼編寫:
示例:
1 use StudentManageDB 2 go 3 --宣告學號變數 4 declare @stuid int,@stuname varchar(20) 5 6 --查詢李銘的資訊 7 set @stuname='李銘' 8 select StudentId,StudentName,Gender,StudentIdNo from Students 9 where StudentName=@stuname 10 11 --查詢李銘的學號 12 select @stuId=StudentId from Students where StudentName=View Code@stuname 13 14 --查詢與李銘學號相鄰的學員 15 select StudentId,StudentName,Gender,StudentIdNo from Students 16 where StudentId=(@stuId+1) or StudentId=(@stuId-1)
set與select比較
示例:
1 declare @stuAddress nvarchar(100) , @stuName nvarchar(100) 2 --set @stuAddress='天津', @stuName='張三' --不允許這樣賦值View Code3 select @stuaddress='天津', @stuName='王小虎' --允許 4 5 --set @stuAddress = (select StudentAddress from Students) --不允許 6 select @stuAddress = StudentAddress from Students --得到最後一個 7 set @stuAddress = (select StudentAddress from Students where 1<0) --NULL值 8 select @stuAddress = StudentAddress from Students where 1<0 --保持原值
全域性變數示例
全域性變數都使用兩個@標誌作為字首
變數 | 含義 |
@@ERROR | 最後一個T-SAQL錯誤的錯誤號 |
@@IDENTITY | 最後一次插入的標識值 |
@@LANGUAGE | 當前使用的語言的名稱 |
@@MAX_CONNECTIONS | 可以建立的同時連線的最大數目 |
@@ROWCOUNT | 受上一個SQL語句影響的行數 |
@@SERVERNAME | 本地伺服器的名稱 |
@@TRANSCOUNT | 當前連線開啟的事務數 |
@@VERSIONSQL | Server的版本資訊 |
1 PRINT '伺服器的名稱: ' + @@SERVERNAME 2 PRINT 'SQL Server的版本' + @@VERSION 3 4 SELECT @@SERVERNAME AS '伺服器名稱' 5 SELECT @@VERSION AS 'SQL Server的版本'View Code
1 use StudentManageDB 2 go 3 --插入學員資訊 4 insert into Students (StudentName,Gender,Age,Birthday,StudentIdNo,PhoneNumber,StudentAddress,ClassId) 5 values('王小欣','男',28,'1988-08-07',120223198808071111,'022-22222222','天津市南開區',10) 6 7 8 --獲取最後一條SQL語句的執行錯誤號 9 print @@errorView Code
T-SQL資料型別轉換
常用的兩種轉換方法
- CONVERT(資料型別,表示式,樣式)第三個引數可以省略,它一般用於日期型別資料轉換為字元型別,或浮點型別資料轉換為字元型別,不同的樣式使轉換後字元資料的顯示格式不同
- CAST(表示式AS資料型別)
CONVERT()與CAST()的不同點是:可以指定轉換的樣式
1 use StudentManageDB 2 go 3 --定義變數並查詢 4 declare @sumScore int 5 select @sumScore=(CSharp+SQLServerDB) from ScoreList 6 where StudentId=100003 7 --輸出 8 --print '學號=100003總成績:'+@sumScore 9 10 print '學號=100003總成績:'+convert(varchar(20),@sumScore)View Code
兩種不同型別的轉換比較
1 use StudentManageDB 2 go 3 --使用CAST轉換 4 select StudentName + '的出生日期是:' + CAST(Birthday as varchar(50)) AS '學生資訊' 5 from Students where StudentId=100005 6 --使用CONVERT轉換 7 select StudentName + '的出生日期是:' + CONVERT(varchar(50),Birthday,102) AS '學生資訊' 8 from Students where StudentId=100005View Code
T-SQL中的其他函式
問題:查詢學號等於100002的學員年齡?
1 use StudentManageDB 2 go 3 --定義變數 4 declare @birthday datetime,@days int,@age int 5 --查詢出生日期 6 select @birthday=Birthday from Students where StudentId=100002 7 --計算出生天數 8 set @days=datediff(dayofyear,@birthday,getdate()) 9 --計算年齡 10 set @age=floor(@days/365) 11 --輸出資訊 12 print '100002學員年齡:'+convert(varchar(20),@age) 13 14 --直接查詢 15 select FLOOR(DATEDIFF(dy, Birthday, GETDATE())/365) 年齡 16 from Students where StudentId=100002View Code
邏輯控制語句
分支結構
- IF-ELSE語句
- CASE-END語句
迴圈結構
- WHILE語句
IF-ELSE語句示例
1 use StudentManageDB 2 go 3 --查詢成績 4 declare @cAvg int 5 select @cAvg=avg(CSharp) from ScoreList 6 inner join Students on ScoreList.StudentId=Students.StudentId where ClassId=1 7 print 'C#平均成績:'+convert(varchar(20),@cAvg) 8 --判斷成績 9 if(@cAvg>=80) 10 print '軟體一班成績優秀!' 11 else 12 print '軟體一班成績一般!'View Code
WHILE語句示例
問題:將所有C#成績不及格的學員加分到60
1 use StudentManageDB 2 go 3 print '加分之前的C#成績:' 4 select StudentId,CSharp from ScoreList 5 declare @CSharp int,@StuId int 6 while(1=1) 7 begin 8 select top 1 @CSharp=CSharp,@StuId=StudentId 9 from ScoreList where CSharp<60 10 if (@CSharp<60) 11 update ScoreList set CSharp=CSharp+1 12 where StudentId=@StuId 13 if((select count(*) from ScoreList where CSharp<60)=0) 14 break 15 end 16 print '加分之後的C#成績:' 17 select StudentId,CSharp from ScoreListView Code
CASE-END語句示例
- ELSE表示CASE中所有WHEN條件均不為TRUE時返回的結果
- 如果省略ELSE且WHEN條件都為FALSE時,CASE語句返回NULL
問題:學員成績進行評比,90以上為A;80-89為B;70-79為C;60-69:為D;60以下為:不及格
1 use StudentManageDB 2 go 3 select 學號=StudentId, 4 總評=CASE 5 when (CSharp+SQLServerDB)/2>=90 then 'A' 6 when (CSharp+SQLServerDB)/2 between 80 and 89 then 'B' 7 when (CSharp+SQLServerDB)/2 between 70 and 79 then 'C' 8 when (CSharp+SQLServerDB)/2 between 60 and 69 then 'D' 9 else '不及格' 10 end 11 from ScoreListView Code
EXISTS子查詢
回顧:如何用SQL語句檢測資料庫或資料表是否已經建立?
語法規範
IF EXISTS(子查詢)
語句
用法總結果
- 子查詢的結果非空,即記錄條數1條以上,則EXISTS(子查詢)將返回真(true),否則返回假(false)。
- EXISTS也可以作為WHERE語句的子查詢,但一般都能用IN子查詢替換。
END