美文网首页
Transact_SQL 编程

Transact_SQL 编程

作者: 任人渐疏_Must | 来源:发表于2021-09-27 20:26 被阅读0次

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','张无忌','男',15,2),('S2003','梅超风','女',20,3)
go

use Stu
--查找张无忌前后同桌
--定义一个表示学生座位号的变量
declare @Seat int
--使用select将查询的结果赋予变量@Seat
select @Seat = StuSeat from stuInfo where StuName = '张无忌'

--查询@Seat的值
select @Seat

--根据‘张无忌’的座位号找到前后的同桌
select * from StuInfo where (StuSeat=@Seat+1) or (StuSeat=@Seat-1)
GO

--查询表中年龄最小的学生姓名
declare @StuName varchar(10)
select @StuName = StuName from StuInfo order by StuAge Desc 
print @StuName



--最后一个T-SQL错误的错误号
print @@Error
--最后一次插入的标识值
print @@identity
--当前使用的语言的名称
print @@language
--可以创建的同时连接的最大数目
print @@max_connections
--受上一个SQL语句影响的行数
print @@rowcount
--本地服务器的名称
print @@servername
--当前连接打开的事务数
print @@TRANSCOUNT  

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


--print 变量或表达式
print '数据库服务器名:'+@@servicename

--select变量或表达式
select 15*8

--显示自动编号
insert into StuInfo(StuName,StuSex,StuBirth) values('诸葛亮','true','2002-03-14')
print '当前自动编号的值:' + 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



--统计分析本班男生的平均成绩和女生的平均成绩
--定义变量
declare @maleScore float
declare @femaleScore float

--第一步,分别统计男生和女生的平均成绩并存入局部变量中
select @maleScore=avg((Chinese+English+Math)/3) from StuScore where StuSex='男'
select @femaleScore=avg((Chinese+English+Math)/3) from StuScore where StuSex='女'

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


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

--第二步,用if-else结构判断,输出结果
IF @maleScore > @femaleScore
    BEGIN
    PRINT '男生成绩优于女生,男生第一名是:'
    select top 1 * from stuScore where StuSex='男' order by (Chinese+English+Math) DESC
    END
ELSE
    BEGIN
    PRINT '女生成绩优于男生,女生第一名是:'
    select top 1 * from stuScore where StuSex='女' order by (Chinese+English+Math) DESC
    END

--循环语句案例

    --如果学生平均成绩没有达80分,便给每位学生数学成绩加1分,然后再次判断是否达80分,否则继续加分,直到其平均成绩超过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成绩表中的学生成绩用五分制显示
    select StuID,语文=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

相关文章

  • Transact_SQL 编程

  • 编程方式

    穷举编程 ccv编程 百度编程 谷歌编程 gayhub编程 guess编程 no think 群友编程 小黄鸭调试...

  • 《Python语言程序设计》第一章.练习与作业

    编程题 1.1 编程题 1.2 编程题 1.3 编程题 1.4 编程题 1.5 编程题 1.6 编程题 1.7 编...

  • 探秘Spring AOP

    编程范式概览 面向过程编程 面向对象编程 函数式编程 事件驱动编程 面向切面编程 AOP是什么 是一种编程范式,不...

  • 探秘Spring AOP

    编程范式概览 主要有面向对象编程、面向过程编程、函数式编程、事件驱动编程、面向切面编程。面向过程编程是以过程为中心...

  • python面向对象1

    编程思想 编程思想:面向过程编程(穷人思想)、函数式编程、面向对象编程(富豪) 面向过程编程: 算法和逻辑 函数式...

  • 函数式编程-前置知识(1)

    什么是函数式编程 函数式编程是编程范式之一,我们常听说的编程范式还有面向过程编程,面向对象编程。 面向对象编程的思...

  • 面向指针编程(一)

    面向对象编程,面向设计模式编程(亦即设计模式),面向接口编程,面向模板编程(亦即泛型编程),面向函数编程(亦即函数...

  • AOP

    一、概述 编程范式概述 面向过程编程 面向对象编程 面向切面编程 函数式编程 事件驱动编程(GUI) 主要解决问题...

  • Rxjava总结

    概念 函数式编程就是一种编程范式,常见的编程范式有命令式编程 函数式编程 和逻辑式编程。。。常见的面向对象编程是一...

网友评论

      本文标题:Transact_SQL 编程

      本文链接:https://www.haomeiwen.com/subject/hseknltx.html