Transact_SQL 編程


create database Stu;
go
use Stu
create table stuInfo(
    StuNo varchar(20) not null primary key,
    StuName varchar(20) not null,
    StuSex char(2) not null,
    StuAge int not null,
    StuSeat int not null
)
go
use Stu
insert into stuInfo values('S2001','張三豐','男',17,1),('S2002','張無(wú)忌','男',15,2),('S2003','梅超風(fēng)','女',20,3)
go

use Stu
--查找張無(wú)忌前后同桌
--定義一個(gè)表示學(xué)生座位號(hào)的變量
declare @Seat int
--使用select將查詢的結(jié)果賦予變量@Seat
select @Seat = StuSeat from stuInfo where StuName = '張無(wú)忌'

--查詢@Seat的值
select @Seat

--根據(jù)‘張無(wú)忌’的座位號(hào)找到前后的同桌
select * from StuInfo where (StuSeat=@Seat+1) or (StuSeat=@Seat-1)
GO

--查詢表中年齡最小的學(xué)生姓名
declare @StuName varchar(10)
select @StuName = StuName from StuInfo order by StuAge Desc 
print @StuName



--最后一個(gè)T-SQL錯(cuò)誤的錯(cuò)誤號(hào)
print @@Error
--最后一次插入的標(biāo)識(shí)值
print @@identity
--當(dāng)前使用的語(yǔ)言的名稱
print @@language
--可以創(chuàng)建的同時(shí)連接的最大數(shù)目
print @@max_connections
--受上一個(gè)SQL語(yǔ)句影響的行數(shù)
print @@rowcount
--本地服務(wù)器的名稱
print @@servername
--當(dāng)前連接打開(kāi)的事務(wù)數(shù)
print @@TRANSCOUNT  

--SQL Server的版本信息
print @@version


--print 變量或表達(dá)式
print '數(shù)據(jù)庫(kù)服務(wù)器名:'+@@servicename

--select變量或表達(dá)式
select 15*8

--顯示自動(dòng)編號(hào)
insert into StuInfo(StuName,StuSex,StuBirth) values('諸葛亮','true','2002-03-14')
print '當(dāng)前自動(dòng)編號(hào)的值:' + convert(varchar(10),@@identity)


use Stu
create table stuScore(
    StuID varchar(20) not null primary key,
    StuSex char(2) not null,
    Chinese int not null,
    English int not null,
    Math int not null
)
go
use Stu
insert into stuScore values('S2001','男',75,80,90),('S2002','男',76,56,54),('S2003','女',90,92,70),('S2004','女',70,72,80)
go



--統(tǒng)計(jì)分析本班男生的平均成績(jī)和女生的平均成績(jī)
--定義變量
declare @maleScore float
declare @femaleScore float

--第一步,分別統(tǒng)計(jì)男生和女生的平均成績(jī)并存入局部變量中
select @maleScore=avg((Chinese+English+Math)/3) from StuScore where StuSex='男'
select @femaleScore=avg((Chinese+English+Math)/3) from StuScore where StuSex='女'

print '男生平均成績(jī):'+convert(varchar(5),@maleScore)


print '女生平均成績(jī):'+convert(varchar(5),@femaleScore)

--第二步,用if-else結(jié)構(gòu)判斷,輸出結(jié)果
IF @maleScore > @femaleScore
    BEGIN
    PRINT '男生成績(jī)優(yōu)于女生,男生第一名是:'
    select top 1 * from stuScore where StuSex='男' order by (Chinese+English+Math) DESC
    END
ELSE
    BEGIN
    PRINT '女生成績(jī)優(yōu)于男生,女生第一名是:'
    select top 1 * from stuScore where StuSex='女' order by (Chinese+English+Math) DESC
    END

--循環(huán)語(yǔ)句案例

    --如果學(xué)生平均成績(jī)沒(méi)有達(dá)80分,便給每位學(xué)生數(shù)學(xué)成績(jī)加1分,然后再次判斷是否達(dá)80分,否則繼續(xù)加分,直到其平均成績(jī)超過(guò)80、
    declare @score float
    select @score = avg((Chinese+English+Math)/3) from StuScore
    --print @score
    WHILE(@score<80)
        BEGIN
        update StuScore set Math=Math+1 where Math<=100
        select @score = avg((Chinese+English+Math)/3) from StuScore
        END
    print @score

--CASE-END案例
    --將StuScore成績(jī)表中的學(xué)生成績(jī)用五分制顯示
    select StuID,語(yǔ)文=CASE
        WHEN Chinese <=19 THEN '1'
        WHEN Chinese <=39 THEN '2'
        WHEN Chinese <=59 THEN '3'
        WHEN Chinese <=79 THEN '4'
        ELSE '5'
        END
    from stuScore

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容