一、T-SQL:transact SQL,提供了變數定義,賦值操作,流程控制,函式等陳述句供用戶使用
背景知識:
一、變數定義:declare @識別符號 型別
二、變數賦值:set @變數1 = value1,select @變數1 = value1,@變數2 = value2
三、流程控制:流程空值和程式設計語言中的流程控制類似,流程空值陳述句是SQL提供給用戶用于改變陳述句執行順序的控制陳述句
四、函式:包括系統函式和用戶自定義函式
五、游標:可以被視作一個指標,可以指向結果集中的任意一個位置并對其進行處理
名稱解釋(不區分大小寫)
- student學生表:
- 包含屬性列:sno學號、sname學生姓名、age年齡、sex性別、dno學院編號、birthday生日
- sc選課資訊表:
- 包含屬性列:sno學號、cno課程號、grade成績
- dept學院資訊表:
- 包含屬性列:dno學院編號、dname學院名稱、dean學院負責人
- course課程資訊表:
- 包含屬性列:cno課程號、cname課程名稱、tname老師名稱、credit學分、room教室
下面陳述句可以直接復制到SQL Server運行
方法不唯一,有問題歡迎留言討論!代碼塊見文章末尾
運行時注意:由于函式、視圖等必須是批處理陳述句中僅有的陳述句,可能需要單獨創建SQL檔案,
且變數命名不可重復,需要運行請自行創建新的SQL檔案,
- 運行方法,選中需要執行的陳述句

二、代碼
–系統資料型別
declare @Lee int
set @Lee = 32
select @Lee
print @Lee
select @@SERVERNAME --回傳SQL服務器名稱
select @@LANGUAGE --回傳當前使用語言名
select @@VERSION --回傳SQL服務器安裝日期、版本和處理器型別
print APP_NAME() --回傳當前會話應用程式
print USER_NAME() --回傳用戶資料庫用戶名
print GETDATE() --回傳當前時間
print DATENAME(YYYY ,GETDATE())
– 補:回傳年份print DATENAME(YYYY,GETDATE()) 或 select DATENAME(YYYY,GETDATE())
– 日期函式
print DATENAME(YYYY,GETDATE())
print DATEPART(MM,GETDATE())
–定義一個實型變數,并將其值輸出
declare @f float
set @f = 456.26
print cast(@f as varchar(12))
–定義一個字符變數,并將其處理后輸出
declare @mynumber char(20)
set @mynumber = ‘test’
select ‘compute’ + @mynumber as ‘計算結果’
–根據授課班號自定義變數,查詢符合要求的學生成績
declare @cn char(20),@f float
select @cn = ‘218801’,@f = 84
select * from sc where cno = @cn and grade >= @f
–BEGIN……END……的使用
begin
declare @myvar float
set @myvar = 1233.23
begin
print(‘變數@myval 的值為’)
print cast(@myvar as char(20))
end
end
–利用 CASE 查看學生的成績等級
select sno,cno,
(case
when grade >= 80 then ‘A’
when grade >= 70 then ‘B’
when grade >= 60 then ‘C’
else ‘D’
end)
as 等級
from sc
–創建一個視圖,統計每個學生的學習情況,若其平均成績超過 90,則其學習情況為優秀;若其平均成績在 80 和 90 之間,則其學習情況為優秀良好;依次類推,
create view study_view
as(
select sc.sno,sname,cno,
case
when (select avg(grade) from sc where sc.sno = student.sno) > 90 then ‘優秀’
when (select avg(grade) from sc where sc.sno = student.sno) > 80 then ‘良好’
when (select avg(grade) from sc where sc.sno = student.sno) > 70 then ‘中等’
when (select avg(grade) from sc where sc.sno = student.sno) > 60 then ‘合格’
else ‘不合格’
end
from student)
–利用 WHILE 結構,求:1+2+3+…+100 的值
declare @s int,@j int
select @j=0,@s=0
while(@j<=100)
begin
select @s = @s + @j,@j = @j + 1
end
print '1+2+3+4+5+6+…+100 = ’ + cast(@s as char)
–利用 IF…ELSE,查詢課程號為’234901’的學生的總平均成績,如果大于 90,輸出優秀,在 80-90 之間,輸出優良,其它輸出一般
declare @avg int
select @avg = avg(grade) from sc where sno = ‘234901’
if @avg > 90
print ‘優秀’
else
begin
if @avg > 80
print ‘優良’
else
print ‘一般’
end
–定義一函式,實作如下功能,對于一給定的學號 studentSNO,查詢該值在 student 中是否存在,存在回傳 1,不存在回傳 0,
create function check_id
(@sno char(8))
returns integer
as
begin
declare @num
if(exists(select * from student where sno = @sno))
set @num = 1
else
set @num = 0
return @num
end
–使用下面的 SQL 陳述句呼叫第 9 題的函式,要求:當向表 student 中插入一條記錄時,首先呼叫函式 check_id,檢查該記錄的學號在表 student 中是否存在,不存在,才可以插入,
declare @num int
select @num = XSGL.check_id(‘20081200’)
if @num = 0
insert into student(sno,sname) values(‘20081300’,‘張文’)
–求學生選課的門數,列出其姓名及選課門數
select sname,(select count() from sc where sc.sno = student.sno)as ‘選課門數’ from student
–根據課程號動態查詢學生的選課人數
–可以利用變數以及字串連接字符‘+’,動態生成 SQL 陳述句,達到依據條件,進行資料庫動態查詢目的,其中,連接字串中若包含單引號’字符,則必須用兩個’’,表示一個單引號’字符,
DECLARE @cno char(8)
DECLARE @sql varchar(8000)
SET @cno=‘218801’
–動態生成 SQL 陳述句
SET @sql='SELECT COUNT() FROM sc WHERE CNO=’’’+@cno+’’’’
EXEC(@sql)
SET @cno=‘203402’
SET @sql=’ SELECT COUNT(*) FROM sc WHERE CNO=’’’+@cno+’’’’
EXEC (@sql)
–用游標結合回圈,輸出全校各種姓氏及其人數
–定義記錄數和回圈變數
declare @count int,@i int
–定義姓氏和人數
declare @xs char(6),@rs int
–定義游標
declare @cursor cursor
–為游標賦值
set @cursor = cursor local scroll for
select distinct substring(sname,1,1) as xs,count(*) as 人數 from student
group by substring(sname,1,1)
–變數初始化
select @count=0,@i=0
–打開游標
open @cursor
–@@cursor_rows是系統變數,用于記錄游標所指向的結果集的行數
set @count = @@cursor_rows
–列印記錄數
print ‘人數’ + cast(@count as char)
–對游標中記錄進行操作
if(@count <= 0)
begin
print(‘沒有記錄’)
end
else
begin
while(@i < @count)
begin
fetch next from @cursor into @xs,@rs
print cast(@i as char) + @xs + cast(@rs as char)
set @i = @i + 1
end
–關閉游標
close @cursor
end
–利用游標結合回圈,統計學院各種姓氏的人數
DECLARE @count int
DECLARE @i int
DECLARE @xing char(6)
DECLARE @rs int
DECLARE @sql varchar(8000)
SET @sql=’’
SET @i=0
DECLARE @name_cursOR CURSOR
SET @name_cursOR =CURSOR LOCAL SCROLL FOR
SELECT DISTINCT SUBSTRING(SNAME,1,1) AS xing,count() AS rs
FROM student
GROUP BY SUBSTRING(SNAME,1,1)
OPEN @name_cursOR
SET @count=@@CURSOR_ROWS
IF @count<= 0
BEGIN
PRINT ‘沒有記錄’
END
ELSE
BEGIN
WHILE(@i<@count-300)
BEGIN
FETCH @name_cursOR INTO @xing,@rs
–動態生成前 300 個姓氏 SQL 陳述句
SET @sql=@sql+’(SELECT COUNT() FROM student WHERE DNO=a.DNO
AND SUBSTRING(SNAME,1,1)=’’’+@xing+’’’) AS ‘+@xing+’,’
SET @i=@i+1
END
FETCH @name_cursOR INTO @xing,@rs
–動態生成第 301 個姓氏 SQL 陳述句
SET @sql=@sql+’(SELECT count(*) FROM student WHERE DNO=a.DNO
AND SUBSTRING(SNAME,1,1)=’’’+@xing+’’’) AS '+@xing
CLOSE @name_cursOR
PRINT @sql
SET @sql=‘SELECT DNAME ,’+@sql+‘FROM (SELECT DISTINCT
STUDENT.DNO,DNAME FROM student,DEPT WHERE student.DNO=DEPT.DNO) AS a’
EXEC(@sql)
END
–利用 CASE 實作學生表中學院編號到學院名稱的映射
select sno,sname,
(case
when substring(sno,5,1) = ‘1’ then ‘機電學院’
when substring(sno,5,1) = ‘2’ then ‘資訊學院’
when substring(sno,5,1) = ‘3’ then ‘工商學院’
end) as 學院
from student
–定義一函式,實作如下功能,對于一給定的學號 studentSNO 和課程號 studentCNO 查詢該值在 student 和 course 中是否存在,存在回傳 1,不存在回傳 0,
create function check
(@sno char(8),@cno char(8))
returns integer
as
begin
declare @num int
if(exists(select * from student where sno = @sno) and exists(select * from course where cno = @cno))
set @num = 1
else
set @num = 0
return @num
end
–根據教師名自定義變數,查詢符合要求的教師授課情況
declare @tname char(8)
set @tname = ‘張聰’
select * from course where tname = @tname
–求授課班號及選修該授課班號的學生人數
select cno,(select count(*) from sc where sc.cno = course.cno) as 選修人數 from course
–定義一函式,根據學號回傳學生的選課門數(參考 INSERT 觸發器)
create function choose_lesson
(@sno char(8))
returns integer
as
begin
declare @count int
select @count = count(*) from sc where sno = @sno
return @count
end
–修改學生的成績,若大于 80 分,增加 5 分,否則,增加 8 分
update sc
set grade = (
case
when(grade > 80) then grade + 5
else grade + 8
end
)
–(1) 按課程名稱,統計其平均分,列出其課程名稱和平均分
declare @cname char(8)
set @cname = ‘線性代數’
select cname,course.cno,avg(grade) from sc,course where cname = @cname group by cname,course.cno
select * from course where cname = ‘線性代數’
–(2) 求每個學生選課的門數及其平均分,列出其姓名、課程門數及平均分
select sname,student.sno,count(cno),avg(grade) from sc,student group by sname,student.sno
–(3) 定義一函式,依據學生的姓名,查詢其所選課程的門數
create function lesson_count
(@sname char(8))
returns integer
as
begin
declare @count int
select @count = count(*) from sc where sno in (select sno from student where sname = @sname)
return @count
end
–(4) 根據學院名稱,統計學生人數,列出學院名稱和學生人數
declare @dname char(16)
set @dname = ‘機電工程學院’
select dname,count(sno) as ‘人數’
from dept,student
where dname = @dname and dept.dno = student.dno
group by dname
–(5) 若存在學號為‘20081200’的學生,則顯示其姓名,否則,顯示相應提示資訊
declare @sno char(8)
set @sno = ‘20081200’
begin
if(exists(select * from student where sno = @sno))
select * from student where sno = @sno
else
print(‘不存在學生’)
end
–(6) 查找每個學生超過他選修課程平均成績的課程相關資訊,列出學號,課程號,成績,選課平均成績
select sno,cno,grade,avg(grade) 平均成績 from sc a
where grade >= (
select avg(grade) from sc b
where a.sno = b.sno)
group by sno,cno,grade
–(7) 創建一視圖,統計每門課程的學習情況,若課程平均成績超過 90,則其學習情況為優秀;若課程平均成績在 80 和 90 之間,則其學習情況為優秀良好;依次類推,
create view count_view
as
select cno,
(case
when avg(grade) > 90 then ‘優秀’
when avg(grade) > 80 then ‘良好’
when avg(grade) > 60 then ‘合格’
else ‘不合格’
end)
from sc
group by cno
–(8) 利用游標結合回圈,統計各門課程的各種分數的人數,
select * from sc
order by cno
declare @i int,@count int
declare @cursor cursor
declare @rs int,@chengji float,@cno char(20)
declare @sql char(8000)
select @i=0,@count=0,@sql=’’
set @cursor = cursor local scroll for
select cno,grade,count(*) as 人數 from sc
group by cno,grade
order by cno
open @cursor
set @count = @@cursor_rows
print @count
if @count <= 0
begin
print ‘沒有記錄’
end
else
begin
while @i < @count
begin
print cast(@i as char) + @cno + cast(@chengji as char) + cast(@rs as char)
set @i = @i + 1
end
close @cursor
end
三、代碼塊
--系統資料型別
declare @Lee int
set @Lee = 32
select @Lee
print @Lee
select @@SERVERNAME --回傳SQL服務器名稱
select @@LANGUAGE --回傳當前使用語言名
select @@VERSION --回傳SQL服務器安裝日期、版本和處理器型別
print APP_NAME() --回傳當前會話應用程式
print USER_NAME() --回傳用戶資料庫用戶名
print GETDATE() --回傳當前時間
print DATENAME(YYYY ,GETDATE())
-- 補:回傳年份print DATENAME(YYYY,GETDATE()) 或 select DATENAME(YYYY,GETDATE())
-- 日期函式
print DATENAME(YYYY,GETDATE())
print DATEPART(MM,GETDATE())
--定義一個實型變數,并將其值輸出
declare @f float
set @f = 456.26
print cast(@f as varchar(12))
--定義一個字符變數,并將其處理后輸出
declare @mynumber char(20)
set @mynumber = 'test'
select 'compute' + @mynumber as '計算結果'
--根據授課班號自定義變數,查詢符合要求的學生成績
declare @cn char(20),@f float
select @cn = '218801',@f = 84
select * from sc where cno = @cn and grade >= @f
--BEGIN……END……的使用
begin
declare @myvar float
set @myvar = 1233.23
begin
print('變數@myval 的值為')
print cast(@myvar as char(20))
end
end
--利用 CASE 查看學生的成績等級
select sno,cno,
(case
when grade >= 80 then 'A'
when grade >= 70 then 'B'
when grade >= 60 then 'C'
else 'D'
end)
as 等級
from sc
--創建一個視圖,統計每個學生的學習情況,若其平均成績超過 90,則其學習情況為優秀;若其平均成績在 80 和 90 之間,則其學習情況為優秀良好;依次類推,
create view study_view
as(
select sc.sno,sname,cno,
case
when (select avg(grade) from sc where sc.sno = student.sno) > 90 then '優秀'
when (select avg(grade) from sc where sc.sno = student.sno) > 80 then '良好'
when (select avg(grade) from sc where sc.sno = student.sno) > 70 then '中等'
when (select avg(grade) from sc where sc.sno = student.sno) > 60 then '合格'
else '不合格'
end
from student)
--利用 WHILE 結構,求:1+2+3+..+100 的值
declare @s int,@j int
select @j=0,@s=0
while(@j<=100)
begin
select @s = @s + @j,@j = @j + 1
end
print '1+2+3+4+5+6+..+100 = ' + cast(@s as char)
--利用 IF…ELSE,查詢課程號為'234901'的學生的總平均成績,如果大于 90,輸出優秀,在 80-90 之間,輸出優良,其它輸出一般
declare @avg int
select @avg = avg(grade) from sc where sno = '234901'
if @avg > 90
print '優秀'
else
begin
if @avg > 80
print '優良'
else
print '一般'
end
--定義一函式,實作如下功能,對于一給定的學號 studentSNO,查詢該值在 student 中是否存在,存在回傳 1,不存在回傳 0,
create function check_id
(@sno char(8))
returns integer
as
begin
declare @num
if(exists(select * from student where sno = @sno))
set @num = 1
else
set @num = 0
return @num
end
--使用下面的 SQL 陳述句呼叫第 9 題的函式,要求:當向表 student 中插入一條記錄時,首先呼叫函式 check_id,檢查該記錄的學號在表 student 中是否存在,不存在,才可以插入,
declare @num int
select @num = XSGL.check_id('20081200')
if @num = 0
insert into student(sno,sname) values('20081300','張文')
--求學生選課的門數,列出其姓名及選課門數
select sname,(select count(*) from sc where sc.sno = student.sno)as '選課門數' from student
--根據課程號動態查詢學生的選課人數
--可以利用變數以及字串連接字符‘+’,動態生成 SQL 陳述句,達到依據條件,進行資料庫動態查詢目的,其中,連接字串中若包含單引號’字符,則必須用兩個’’,表示一個單引號’字符,
DECLARE @cno char(8)
DECLARE @sql varchar(8000)
SET @cno='218801'
--動態生成 SQL 陳述句
SET @sql='SELECT COUNT(*) FROM sc WHERE CNO='''+@cno+''''
EXEC(@sql)
SET @cno='203402'
SET @sql=' SELECT COUNT(*) FROM sc WHERE CNO='''+@cno+''''
EXEC (@sql)
--用游標結合回圈,輸出全校各種姓氏及其人數
--定義記錄數和回圈變數
declare @count int,@i int
--定義姓氏和人數
declare @xs char(6),@rs int
--定義游標
declare @cursor cursor
--為游標賦值
set @cursor = cursor local scroll for
select distinct substring(sname,1,1) as xs,count(*) as 人數 from student
group by substring(sname,1,1)
--變數初始化
select @count=0,@i=0
--打開游標
open @cursor
--@@cursor_rows是系統變數,用于記錄游標所指向的結果集的行數
set @count = @@cursor_rows
--列印記錄數
print '人數' + cast(@count as char)
--對游標中記錄進行操作
if(@count <= 0)
begin
print('沒有記錄')
end
else
begin
while(@i < @count)
begin
fetch next from @cursor into @xs,@rs
print cast(@i as char) + @xs + cast(@rs as char)
set @i = @i + 1
end
--關閉游標
close @cursor
end
--利用游標結合回圈,統計學院各種姓氏的人數
DECLARE @count int
DECLARE @i int
DECLARE @xing char(6)
DECLARE @rs int
DECLARE @sql varchar(8000)
SET @sql=''
SET @i=0
DECLARE @name_cursOR CURSOR
SET @name_cursOR =CURSOR LOCAL SCROLL FOR
SELECT DISTINCT SUBSTRING(SNAME,1,1) AS xing,count(*) AS rs
FROM student
GROUP BY SUBSTRING(SNAME,1,1)
OPEN @name_cursOR
SET @count=@@CURSOR_ROWS
IF @count<= 0
BEGIN
PRINT '沒有記錄'
END
ELSE
BEGIN
WHILE(@i<@count-300)
BEGIN
FETCH @name_cursOR INTO @xing,@rs
--動態生成前 300 個姓氏 SQL 陳述句
SET @sql=@sql+'(SELECT COUNT(*) FROM student WHERE DNO=a.DNO
AND SUBSTRING(SNAME,1,1)='''+@xing+''') AS '+@xing+','
SET @i=@i+1
END
FETCH @name_cursOR INTO @xing,@rs
--動態生成第 301 個姓氏 SQL 陳述句
SET @sql=@sql+'(SELECT count(*) FROM student WHERE DNO=a.DNO
AND SUBSTRING(SNAME,1,1)='''+@xing+''') AS '+@xing
CLOSE @name_cursOR
PRINT @sql
SET @sql='SELECT DNAME ,'+@sql+'FROM (SELECT DISTINCT
STUDENT.DNO,DNAME FROM student,DEPT WHERE student.DNO=DEPT.DNO) AS a'
EXEC(@sql)
END
--利用 CASE 實作學生表中學院編號到學院名稱的映射
select sno,sname,
(case
when substring(sno,5,1) = '1' then '機電學院'
when substring(sno,5,1) = '2' then '資訊學院'
when substring(sno,5,1) = '3' then '工商學院'
end) as 學院
from student
--定義一函式,實作如下功能,對于一給定的學號 studentSNO 和課程號 studentCNO 查詢該值在 student 和 course 中是否存在,存在回傳 1,不存在回傳 0,
create function check
(@sno char(8),@cno char(8))
returns integer
as
begin
declare @num int
if(exists(select * from student where sno = @sno) and exists(select * from course where cno = @cno))
set @num = 1
else
set @num = 0
return @num
end
--根據教師名自定義變數,查詢符合要求的教師授課情況
declare @tname char(8)
set @tname = '張聰'
select * from course where tname = @tname
--求授課班號及選修該授課班號的學生人數
select cno,(select count(*) from sc where sc.cno = course.cno) as 選修人數 from course
--定義一函式,根據學號回傳學生的選課門數(參考 INSERT 觸發器)
create function choose_lesson
(@sno char(8))
returns integer
as
begin
declare @count int
select @count = count(*) from sc where sno = @sno
return @count
end
--修改學生的成績,若大于 80 分,增加 5 分,否則,增加 8 分
update sc
set grade = (
case
when(grade > 80) then grade + 5
else grade + 8
end
)
--(1) 按課程名稱,統計其平均分,列出其課程名稱和平均分
declare @cname char(8)
set @cname = '線性代數'
select cname,course.cno,avg(grade) from sc,course where cname = @cname group by cname,course.cno
select * from course where cname = '線性代數'
--(2) 求每個學生選課的門數及其平均分,列出其姓名、課程門數及平均分
select sname,student.sno,count(cno),avg(grade) from sc,student group by sname,student.sno
--(3) 定義一函式,依據學生的姓名,查詢其所選課程的門數
create function lesson_count
(@sname char(8))
returns integer
as
begin
declare @count int
select @count = count(*) from sc where sno in (select sno from student where sname = @sname)
return @count
end
--(4) 根據學院名稱,統計學生人數,列出學院名稱和學生人數
declare @dname char(16)
set @dname = '機電工程學院'
select dname,count(sno) as '人數'
from dept,student
where dname = @dname and dept.dno = student.dno
group by dname
--(5) 若存在學號為‘20081200’的學生,則顯示其姓名,否則,顯示相應提示資訊
declare @sno char(8)
set @sno = '20081200'
begin
if(exists(select * from student where sno = @sno))
select * from student where sno = @sno
else
print('不存在學生')
end
--(6) 查找每個學生超過他選修課程平均成績的課程相關資訊,列出學號,課程號,成績,選課平均成績
select sno,cno,grade,avg(grade) 平均成績 from sc a
where grade >= (
select avg(grade) from sc b
where a.sno = b.sno)
group by sno,cno,grade
--(7) 創建一視圖,統計每門課程的學習情況,若課程平均成績超過 90,則其學習情況為優秀;若課程平均成績在 80 和 90 之間,則其學習情況為優秀良好;依次類推,
create view count_view
as
select cno,
(case
when avg(grade) > 90 then '優秀'
when avg(grade) > 80 then '良好'
when avg(grade) > 60 then '合格'
else '不合格'
end)
from sc
group by cno
--(8) 利用游標結合回圈,統計各門課程的各種分數的人數,
select * from sc
order by cno
declare @i int,@count int
declare @cursor cursor
declare @rs int,@chengji float,@cno char(20)
declare @sql char(8000)
select @i=0,@count=0,@sql=''
set @cursor = cursor local scroll for
select cno,grade,count(*) as 人數 from sc
group by cno,grade
order by cno
open @cursor
set @count = @@cursor_rows
print @count
if @count <= 0
begin
print '沒有記錄'
end
else
begin
while @i < @count
begin
print cast(@i as char) + @cno + cast(@chengji as char) + cast(@rs as char)
set @i = @i + 1
end
close @cursor
end
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/235555.html
標籤:其他
上一篇:抖音資料采集教程,高級版
