1. 程式人生 > 實用技巧 >3.SqlServer-深入T-SQL高階程式設計

3.SqlServer-深入T-SQL高階程式設計

1.區域性變數的使用

--宣告學號變數
declare @stuId int,@stuName varchar(20)

--查詢小明的資訊
set @stuName='小明'
select * from Students where StudentName=@stuName;

--查詢與小明學號相鄰的學員
select * from Students where StudentId=(@stuId+1) or StudentId=(@stuId-1);

2.set與 select的比較

declare @stuAddress nvarchar(100), @stuName nvarchar
(100) --set @stuAddress='合肥',@stuName='小紅' --已經報錯了,不允許這樣賦值 --select @stuAddress='合肥蜀山',@stuName='大明' --允許這樣賦值 --set @stuAddress=StudentAddress from Students;--不允許 --set @stuAddress=(select StudentAddress from Students);--不允許,因為得到的結果不止一個 --select @stuAddress= StudentAddress from Students;--可以 --
set @stuAddress=(select StudentAddress from Students where 1<0);--null值 select @stuAddress= StudentAddress from Students where 1<0;--和原來一樣

3.全域性變數

-------------------------示例1--------------------------------
print '伺服器的名稱:'+@@servername;
print 'SqlServer的版本'+@@version;

select @@SERVERNAME AS '
伺服器名稱'; select @@VERSION AS 'SqlServer 的版本'; ----------------------------示例2--------------------------------- use StudentManageDB go --插入學員資訊 insert into Students(StudentName,Gender,Age,Birthday,StudentIdNo,PhoneNumber,StudentAddress,ClassId) values('袁崇煥','',20,'1990-05-01',342222199005014677,'15756008899','合肥市包河區',10) --獲取最後一條SQL語句的執行錯誤號 print @@error

4.資料型別轉換

convert(型別,變數)

use StudentManageDB
go
--定義變數並查詢
declare @sumScore int
select @sumScore=(CSharp+SQLServerDB) from ScoreList
where StudentId=100003

print '學號=100003總成績:'+convert(varchar(20),@sumScore)

--------------------兩種不同型別轉換比較------------------------------
--使用CAST轉換
select StudentName + '的出生日期是:'+CAST(Birthday as varchar(50)) AS '學生資訊' 
from Students where StudentId =100005;
--使用 CONVERT
select StudentName +'的出生日期是:'+CONVERT(varchar(50),Birthday,120) AS '學生資訊'
from Students where StudentId=100005;-- 120 : 樣式號,這裡是為一種日期格式

5.T-SQL中的其他函式

use StudentManageDB
go
--定義變數
declare @birthday datetime,@days int, @age int
--查詢出生日期
select @birthday=Birthday from Students where StudentId=100002;
--計算出生天數
set @days = datediff(DAYOFYEAR,@birthday,GETDATE());
--計算年齡
set @age=FLOOR(@days/365);

print '年齡為:'+convert(varchar(20),@age);

--直接查詢
select floor(datediff(dy,Birthday,getdate())/365) 年齡 
from Students where StudentId=100002

6.if-else語句

use StudentManageDB
go
--查詢成績
declare @cAvg int
select @cAvg=avg(CSharp) from ScoreList s1
inner join Students s2 on  s1.StudentId=s2.StudentId 
where ClassId=1;
print 'C#平均成績:' + convert(varchar(20),@cAvg);

if(@cAvg >= 80)
   print '成績優秀'
else
   print '成績一般'

7.while語句

use StudentManageDB
go
print '加分之前的C#成績:'
select StudentId,CSharp from ScoreList

declare @CSharp int, @Stuid int
while(1=1)  --一定要對齊,良好的程式碼規範
    begin
        select top 1 @CSharp=CSharp,@Stuid=StudentId  --找出第一個小於60分
                from ScoreList where CSharp<60
        if(@CSharp<60)
            update ScoreList set CSharp=CSharp+1     --小於60就加1
                where StudentId=@Stuid
        if((select count(*) from ScoreList where CSharp<60)=0) --全部大於60  就跳出
            break
    end
print '加分之後的C#成績'
select StudentId,CSharp from ScoreList

8.case-end語句示例

use StudentManageDB
go
select 學號=StudentId,
    總評=CASE    
                when (CSharp+SQLServerDB)/2 >= 90 then 'A'
                when (CSharp+SQLServerDB)/2 between 80 and 89 then 'B'
                when (CSharp+SQLServerDB)/2 between 70 and 79 then 'C'
                when (CSharp+SQLServerDB)/2 between 60 and 69 then  'D'
                else '不及格'
         end
from ScoreList;