From 2d11ecf19b5acb6d0215f3d2532214f700dc7874 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=90=B4=E6=98=8E=E6=9D=B0?= <3090356592@qq.com> Date: Mon, 5 Sep 2022 21:53:45 +0800 Subject: [PATCH 1/3] 9.5 --- ...47\345\210\266\350\257\255\345\217\245.md" | 304 ++++++++++++++++++ ...41\344\270\216\345\217\230\351\207\217.md" | 100 ++++++ 2 files changed, 404 insertions(+) create mode 100644 "15\345\220\264\346\230\216\346\235\260/\347\254\224\350\256\260/\344\272\214\343\200\201\345\243\260\346\230\216\345\217\230\351\207\217\344\270\216\351\200\273\350\276\221\346\216\247\345\210\266\350\257\255\345\217\245.md" create mode 100644 "15\345\220\264\346\230\216\346\235\260/\347\273\203\344\271\240/220905-\350\256\276\350\256\241\344\270\216\345\217\230\351\207\217.md" diff --git "a/15\345\220\264\346\230\216\346\235\260/\347\254\224\350\256\260/\344\272\214\343\200\201\345\243\260\346\230\216\345\217\230\351\207\217\344\270\216\351\200\273\350\276\221\346\216\247\345\210\266\350\257\255\345\217\245.md" "b/15\345\220\264\346\230\216\346\235\260/\347\254\224\350\256\260/\344\272\214\343\200\201\345\243\260\346\230\216\345\217\230\351\207\217\344\270\216\351\200\273\350\276\221\346\216\247\345\210\266\350\257\255\345\217\245.md" new file mode 100644 index 0000000..6d04f0c --- /dev/null +++ "b/15\345\220\264\346\230\216\346\235\260/\347\254\224\350\256\260/\344\272\214\343\200\201\345\243\260\346\230\216\345\217\230\351\207\217\344\270\216\351\200\273\350\276\221\346\216\247\345\210\266\350\257\255\345\217\245.md" @@ -0,0 +1,304 @@ +# 二、Transact-SQL编程 + +**SQL语句** + +- 数据查询语句:DQL select +- 数据库操纵语句:DML insert update delete +- 数据库定义语句:DDL 建库 建表 加约束 +- 数据库控制语句:DCL 权限管理 + +****** + +## (一)变量 + +**局部变量** + +- 局部变量必须以标记==@==作为前缀,如@age +- 局部变量的使用也是==先声明,再赋值== + +**全局变量** + +- 全局变量必须以标记==@@==作为前缀,如@@versionا +- 全局变量由系统定义和维护,我们==只能读取,不能修改全局变量的值== + +***** + +### 1.局部变量 + +#### (1)声明局部变量 + +语法: ==declare==[声明] + +```sql +declare @变量名 数据类型 +--变量名可以自定义 +``` + +示例: + +```sql +declare @name varchar(8) +declare @seat int +``` + +*********** + +#### (2)局部变量的赋值 + +语法: set[置] + +``` +set @变量名 = 值 +或 +select @变量名 = 值 (直接赋值或查询数据赋值) +``` + +示例: + +```sql +select @变量名 = ‘111’ 或 ( set @变量名= ‘111’ ) +select @变量名 = 字段名 from 表明 [where 条件] +``` + +例题: + +```sql +--查询出与脑白金相邻id的药名 +declare @MeMid int --声明局部变量 +select @MeMid=mId from Medicine where MedName='脑白金' --把脑白金的id赋值给局部变量 +select MedName from Medicine where mId=(@MeMid+1) or mId=(@MeMid-1) --条件符合mid=变量(脑白金的id)+1或变量-1 +``` + +****** + +| | set | select | +| ------------------ | -------------- | ---------------------- | +| 同时对多个变量赋值 | 不支持 | 支持 | +| 表达式返回多个值 | 出错 | 将返回的最后一个值赋值 | +| 表达式未返回值时 | 变量被赋NULL值 | 变量保持原值 | + +******* + +### 2.全局变量 + +- 全局变量是SQL Server系统内部使用的变量,其作用范围并不局限于某一程序而是任何程序均可随时调用 +- 全局变量不是有用户的程序定义它们是在服务器级定义的,只能使用预先说明及定义的全局变量 +- 引用全局变量是==必须以@@开头==,==局部变量的名称不能与全局变量的名称相同==,否则会在应用中出错 + +****** + +| | 翻译 | **含 义** | +| ----------------- | ---------- | ------------------------------------------------------------ | +| @@connections | 连接 | 返回SQL Server自上次启动以来尝试的连接次数,无论链接是成功还是失败 | +| @@cursor_rows | 光标 | 返回连接上打开的上一个游标中的当前限定行的数目 | +| **@@error** | 错误 | 返回执行上的一个Transact-SQL语句,0为正确 | +| **@@identity** | 同一性 | 返回最后插入的标识值的系统函数 | +| @@language | 语言 | 返回当前所用语言的名称 | +| @@max_connections | 最大连接 | 返回SQL Server实例允许同时进行的最大用户连接数 | +| @@procid | 虚拟目录id | 返回Transact-SQL当前模块的对象标识符(ID)。Transact-SQL模块可以是存储过程、用户定义函数或触发器 | +| @@rowcount | 行数 | 返回受上一句影响的行数 | +| **@@servername** | 服务器名称 | 返回运行在SQL Server的本地服务器的名称 | +| @servicena | 服务器名称 | 返回SQL Server正在其下运行的注册表项的名称。若当前实例为默认实例,则@@servicename | +| **@@version** | 版本 | 返回当前的SQL Server的安装版本、处理器体系结构、生成日期和操作系统 | + +******* + +## (二)数据库输出语句 + +**T-SQL支持输出语句,用于输出处理的数据结果** +常用输出语句有两种:**print[打印]** + +1. print变量或表达式 +2. select 变量或表达式 + +print以文本的方式输出,select以结果集输出 + +==要注意的是SQLserver里的+ 两边只能是相同的数据类型== + +列如: + +``` +--输出服务器的名称 +print '服务器的名称为:'+@@servername +``` + +******* + +### 1.强制类型转换 + +如果说我们要输出的+两边类型不相等会报错,可以通过强制类型转换来解决 + +类型转换有两种方式:**convert[转换] cast[投射]** + +```sql +convert(数据类型,表达式,样式) +print '成绩'+convert(varchar(10),@Result) +--把int类型转换成varchar类型 +``` + +或 + +```sql +cast(表达式 as 数据类型) +print '成绩'+cast(@Result as varchar(10)) +``` + +****** + +### 2.视频提及的函数 + +#### (1)datediff()[两日期差] + +``` +datediff(year,borndate,getdate()) +两日期差(年,要求的年份,获取当前日期) +--截取当期日期和出生日期的差(今年跟出生日期差多少年求出生日) +``` + +****** + +## (三)逻辑控制语句 + +### 1.if-else语句 + +语法结构:begin[开始] end[结束] + +```sql +--语法结构: +if( 条件表达式 ) + begin + 命令行或程序块1 + 命令行或程序块2 + end +else + begin + 命令行或程序块 + end +``` + +==else是可选部分,如果有多条语句才需要begin-end语句块,判断是否为空用is 或 is not== + +例题: + +统计并显示2009-2-17的Java Logic考试平均分如果平均分在70以上,显示“考试成绩优秀”,并显示前三名学生的考试信息如果在70以下,显示“考试成绩较差”,并显示后三名学生的考试信息 + +```sql +declare @subjectno int --存储科目编号 +declare @avg numeric(5,2)--存储平均分 +--查询科目编号赋值给@subjecton +--查询平均分赋值给@avg +if(@avg>70) + begin + print '成绩优秀' + select语句显示前3名考生信息 + end +else + begin + print '成绩较差' + select语句显示后3名考生信息 + end +``` + +输出的时候会发现只有select语句输出了print没有输出 + +**** + +#### ==(1)如何同时显示print和select输出语句== + +工具->选项->查询结果->SQL Server->以文本格式显示->重新启动(保险起见操作前保存代码) + +******** + +### 2.**while-continue-break**语句 + +语法: continue[继续] + +```sql +while(条件表达式) + begin + 命令行或程序块 + [break] →→ 跳出循环 + [continue] →→ 可以让程序跳过continue命令之后的语句 + 命令行或程序块 + end +``` + +==如果有多条语句才需要begin-end语句块== + +例题: + +检擦student表中是否有==成绩不及格==的学生。如果有,每个人加两分,==高于95分的学生不再加分==,直到所有学生这次考试成绩均及格 + +```sql +declare @scount int; --声明人数变量 +while(1=1) + begin + select @scount=count(*) from student where score<60 --统计不及格人数 + if(@sum>0)--如果有不及格的人 + begin + update student set score=score+2 where score<95 --成绩小于95的全部加2 + end + else + begin + break + end + end +``` + +****** + +### 3.case语句 + +```sql +select 条件表达式, case [compare_value] + when 条件1 then 结果1 + when 条件2 then 结果2 + [else 其他结果] + end [列名] +from 表名 + +--批处理可以提高语句的执行效率 +``` + +==else:表示case中所有when条件均不为true时返回的结果,如果省略else且when条件都为false时, case语句返回null== + +示例: + +按等级给student表中显示学生成绩,A:90以上;B:80-89分;C:70-79分;D:60-69分;E:60分以下 + +```sql +select *, case + when score>90 then 'A' + when score>=80 and score<90 then 'B' + when score>=70 and score<80 then 'C' + when score>=60 and score<70 then 'D' + when score<60 then 'E' + end +from student +``` + +****** + +CASE句法返回第一个value = year(PeopleBirth) % 12比较结果为真的结果。 如果没有比较结果符合,则返回ELSE后的结果,如果没有ELSE部分,则返回NULL: + +```csharp +select PeopleName 姓名,PeopleSex 性别,PeopleSalary 工资,PeoplePhone 电话,PEOPLEBIRTH 生日, +case year(PeopleBirth) % 12 + when 4 then '鼠' + when 5 then '牛' + when 6 then '虎' + when 7 then '兔' + when 8 then '龙' + when 9 then '蛇' + when 10 then '马' + when 11 then '羊' + when 0 then '猴' + when 1 then '鸡' + when 2 then '狗' + when 3 then '猪' + ELSE '' +end 生肖 +from People +``` + +# \ No newline at end of file diff --git "a/15\345\220\264\346\230\216\346\235\260/\347\273\203\344\271\240/220905-\350\256\276\350\256\241\344\270\216\345\217\230\351\207\217.md" "b/15\345\220\264\346\230\216\346\235\260/\347\273\203\344\271\240/220905-\350\256\276\350\256\241\344\270\216\345\217\230\351\207\217.md" new file mode 100644 index 0000000..98d9d05 --- /dev/null +++ "b/15\345\220\264\346\230\216\346\235\260/\347\273\203\344\271\240/220905-\350\256\276\350\256\241\344\270\216\345\217\230\351\207\217.md" @@ -0,0 +1,100 @@ +--2. 练习:要求根据座位号找出张无忌的前后同学? + +``` +declare @seat int +select @seat=StuSeat from StuInfo where StuName='张无忌' +select StuName from StuInfo where StuSeat in (@seat+1,@seat-1) +``` + +--2.练习:查询年龄最大的学生信息 + +``` +declare @age int +select @age = max(StuAge) from StuInfo +select * from StuInfo where StuAge= @age +``` + +***** + +```sql +create database Projects + +use Projects + + +--工程表:工程号 工程名称 +create table Project( +pno varchar(20) primary key , +pname varchar(20) +) +go + +insert Project values +('A1','花园大厦'), +('A2','立交桥'), +('A3','临江饭店') + + + +--职务表:职务号 职务 小时工资 +create table functin( +fno int primary key identity(1,1), +fname varchar(20), +ftime int +) +go + +insert functin values +('工程师',65), +('技术员',60), +('工人',55) + + + +--职工表: 职工号 姓名 +create table staff( +sno int primary key identity(1001,1), +sname varchar(20), +fno int references functin(fno) +) +go +insert staff values +('齐光明',1), +('李思岐',2), +('鞠明亮',3), +('葛宇洪',2) + + + + + +--工时表:工时号 工程+职工号 工时 +create table manhour( +mno int primary key identity(1,1), +pno varchar(20) references Project(pno) , +sno int references staff(sno), +mtime int +) + +go + +insert manhour values +('A1',1001,13), +('A1',1002,16), +('A2',1001,15), +('A2',1003,17), +('A3',1002,18), +('A3',1004,14) + +select * from Project +select * from functin +select * from staff +select * from manhour + + +select p.pno,p.pname,s.sno,s.sname,fname,ftime,mtime from manhour m +left join Project p on m.pno=p.pno +left join staff s on s.sno=m.sno +join functin f on s.fno=f.fno +``` + -- Gitee From 71ee16401e37ee1a46d6ba9cae8fcc94d55e2549 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=90=B4=E6=98=8E=E6=9D=B0?= <3090356592@qq.com> Date: Tue, 6 Sep 2022 23:37:21 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E6=8F=90=E4=BA=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../220829sql\345\244\215\344\271\240.md" | 0 ...56\345\272\223\350\256\276\350\256\241.md" | 0 .../220905\345\217\230\351\207\217.md" | 166 +----------------- ...73\350\276\221\350\257\255\345\217\245.md" | 136 ++++++++++++++ ...55\345\217\245\347\273\203\344\271\240.md" | 106 +++++++++++ 5 files changed, 246 insertions(+), 162 deletions(-) rename "15\345\220\264\346\230\216\346\235\260/\347\254\224\350\256\260/sql\345\244\215\344\271\240.md" => "15\345\220\264\346\230\216\346\235\260/\347\254\224\350\256\260/220829sql\345\244\215\344\271\240.md" (100%) rename "15\345\220\264\346\230\216\346\235\260/\347\254\224\350\256\260/\344\270\200\343\200\201\346\225\260\346\215\256\345\272\223\350\256\276\350\256\241.md" => "15\345\220\264\346\230\216\346\235\260/\347\254\224\350\256\260/220901\346\225\260\346\215\256\345\272\223\350\256\276\350\256\241.md" (100%) rename "15\345\220\264\346\230\216\346\235\260/\347\254\224\350\256\260/\344\272\214\343\200\201\345\243\260\346\230\216\345\217\230\351\207\217\344\270\216\351\200\273\350\276\221\346\216\247\345\210\266\350\257\255\345\217\245.md" => "15\345\220\264\346\230\216\346\235\260/\347\254\224\350\256\260/220905\345\217\230\351\207\217.md" (55%) create mode 100644 "15\345\220\264\346\230\216\346\235\260/\347\254\224\350\256\260/220906\351\200\273\350\276\221\350\257\255\345\217\245.md" create mode 100644 "15\345\220\264\346\230\216\346\235\260/\347\273\203\344\271\240/220906-\351\200\273\350\276\221\350\257\255\345\217\245\347\273\203\344\271\240.md" diff --git "a/15\345\220\264\346\230\216\346\235\260/\347\254\224\350\256\260/sql\345\244\215\344\271\240.md" "b/15\345\220\264\346\230\216\346\235\260/\347\254\224\350\256\260/220829sql\345\244\215\344\271\240.md" similarity index 100% rename from "15\345\220\264\346\230\216\346\235\260/\347\254\224\350\256\260/sql\345\244\215\344\271\240.md" rename to "15\345\220\264\346\230\216\346\235\260/\347\254\224\350\256\260/220829sql\345\244\215\344\271\240.md" diff --git "a/15\345\220\264\346\230\216\346\235\260/\347\254\224\350\256\260/\344\270\200\343\200\201\346\225\260\346\215\256\345\272\223\350\256\276\350\256\241.md" "b/15\345\220\264\346\230\216\346\235\260/\347\254\224\350\256\260/220901\346\225\260\346\215\256\345\272\223\350\256\276\350\256\241.md" similarity index 100% rename from "15\345\220\264\346\230\216\346\235\260/\347\254\224\350\256\260/\344\270\200\343\200\201\346\225\260\346\215\256\345\272\223\350\256\276\350\256\241.md" rename to "15\345\220\264\346\230\216\346\235\260/\347\254\224\350\256\260/220901\346\225\260\346\215\256\345\272\223\350\256\276\350\256\241.md" diff --git "a/15\345\220\264\346\230\216\346\235\260/\347\254\224\350\256\260/\344\272\214\343\200\201\345\243\260\346\230\216\345\217\230\351\207\217\344\270\216\351\200\273\350\276\221\346\216\247\345\210\266\350\257\255\345\217\245.md" "b/15\345\220\264\346\230\216\346\235\260/\347\254\224\350\256\260/220905\345\217\230\351\207\217.md" similarity index 55% rename from "15\345\220\264\346\230\216\346\235\260/\347\254\224\350\256\260/\344\272\214\343\200\201\345\243\260\346\230\216\345\217\230\351\207\217\344\270\216\351\200\273\350\276\221\346\216\247\345\210\266\350\257\255\345\217\245.md" rename to "15\345\220\264\346\230\216\346\235\260/\347\254\224\350\256\260/220905\345\217\230\351\207\217.md" index 6d04f0c..73e655b 100644 --- "a/15\345\220\264\346\230\216\346\235\260/\347\254\224\350\256\260/\344\272\214\343\200\201\345\243\260\346\230\216\345\217\230\351\207\217\344\270\216\351\200\273\350\276\221\346\216\247\345\210\266\350\257\255\345\217\245.md" +++ "b/15\345\220\264\346\230\216\346\235\260/\347\254\224\350\256\260/220905\345\217\230\351\207\217.md" @@ -47,10 +47,10 @@ declare @seat int 语法: set[置] -``` -set @变量名 = 值 +```js +set @变量名 = 值 //一般直接赋值时使用 或 -select @变量名 = 值 (直接赋值或查询数据赋值) +select @变量名 = 值 (直接赋值或查询数据赋值)//一般从数据库里赋值时使用 ``` 示例: @@ -143,162 +143,4 @@ cast(表达式 as 数据类型) print '成绩'+cast(@Result as varchar(10)) ``` -****** - -### 2.视频提及的函数 - -#### (1)datediff()[两日期差] - -``` -datediff(year,borndate,getdate()) -两日期差(年,要求的年份,获取当前日期) ---截取当期日期和出生日期的差(今年跟出生日期差多少年求出生日) -``` - -****** - -## (三)逻辑控制语句 - -### 1.if-else语句 - -语法结构:begin[开始] end[结束] - -```sql ---语法结构: -if( 条件表达式 ) - begin - 命令行或程序块1 - 命令行或程序块2 - end -else - begin - 命令行或程序块 - end -``` - -==else是可选部分,如果有多条语句才需要begin-end语句块,判断是否为空用is 或 is not== - -例题: - -统计并显示2009-2-17的Java Logic考试平均分如果平均分在70以上,显示“考试成绩优秀”,并显示前三名学生的考试信息如果在70以下,显示“考试成绩较差”,并显示后三名学生的考试信息 - -```sql -declare @subjectno int --存储科目编号 -declare @avg numeric(5,2)--存储平均分 ---查询科目编号赋值给@subjecton ---查询平均分赋值给@avg -if(@avg>70) - begin - print '成绩优秀' - select语句显示前3名考生信息 - end -else - begin - print '成绩较差' - select语句显示后3名考生信息 - end -``` - -输出的时候会发现只有select语句输出了print没有输出 - -**** - -#### ==(1)如何同时显示print和select输出语句== - -工具->选项->查询结果->SQL Server->以文本格式显示->重新启动(保险起见操作前保存代码) - -******** - -### 2.**while-continue-break**语句 - -语法: continue[继续] - -```sql -while(条件表达式) - begin - 命令行或程序块 - [break] →→ 跳出循环 - [continue] →→ 可以让程序跳过continue命令之后的语句 - 命令行或程序块 - end -``` - -==如果有多条语句才需要begin-end语句块== - -例题: - -检擦student表中是否有==成绩不及格==的学生。如果有,每个人加两分,==高于95分的学生不再加分==,直到所有学生这次考试成绩均及格 - -```sql -declare @scount int; --声明人数变量 -while(1=1) - begin - select @scount=count(*) from student where score<60 --统计不及格人数 - if(@sum>0)--如果有不及格的人 - begin - update student set score=score+2 where score<95 --成绩小于95的全部加2 - end - else - begin - break - end - end -``` - -****** - -### 3.case语句 - -```sql -select 条件表达式, case [compare_value] - when 条件1 then 结果1 - when 条件2 then 结果2 - [else 其他结果] - end [列名] -from 表名 - ---批处理可以提高语句的执行效率 -``` - -==else:表示case中所有when条件均不为true时返回的结果,如果省略else且when条件都为false时, case语句返回null== - -示例: - -按等级给student表中显示学生成绩,A:90以上;B:80-89分;C:70-79分;D:60-69分;E:60分以下 - -```sql -select *, case - when score>90 then 'A' - when score>=80 and score<90 then 'B' - when score>=70 and score<80 then 'C' - when score>=60 and score<70 then 'D' - when score<60 then 'E' - end -from student -``` - -****** - -CASE句法返回第一个value = year(PeopleBirth) % 12比较结果为真的结果。 如果没有比较结果符合,则返回ELSE后的结果,如果没有ELSE部分,则返回NULL: - -```csharp -select PeopleName 姓名,PeopleSex 性别,PeopleSalary 工资,PeoplePhone 电话,PEOPLEBIRTH 生日, -case year(PeopleBirth) % 12 - when 4 then '鼠' - when 5 then '牛' - when 6 then '虎' - when 7 then '兔' - when 8 then '龙' - when 9 then '蛇' - when 10 then '马' - when 11 then '羊' - when 0 then '猴' - when 1 then '鸡' - when 2 then '狗' - when 3 then '猪' - ELSE '' -end 生肖 -from People -``` - -# \ No newline at end of file +## \ No newline at end of file diff --git "a/15\345\220\264\346\230\216\346\235\260/\347\254\224\350\256\260/220906\351\200\273\350\276\221\350\257\255\345\217\245.md" "b/15\345\220\264\346\230\216\346\235\260/\347\254\224\350\256\260/220906\351\200\273\350\276\221\350\257\255\345\217\245.md" new file mode 100644 index 0000000..3defd85 --- /dev/null +++ "b/15\345\220\264\346\230\216\346\235\260/\347\254\224\350\256\260/220906\351\200\273\350\276\221\350\257\255\345\217\245.md" @@ -0,0 +1,136 @@ + + +## (三)逻辑控制语句 + +### 1.if-else语句 + +语法结构:begin[开始] end[结束] + +```sql +--语法结构: +if( 条件表达式 ) + begin + 命令行或程序块1 + 命令行或程序块2 + end +else + begin + 命令行或程序块 + end +``` + +==else是可选部分,如果有多条语句才需要begin-end语句块,判断是否为空用is 或 is not== + +例题: + +统计并显示2009-2-17的Java Logic考试平均分如果平均分在70以上,显示“考试成绩优秀”,并显示前三名学生的考试信息如果在70以下,显示“考试成绩较差”,并显示后三名学生的考试信息 + +```sql +declare @subjectno int --存储科目编号 +declare @avg numeric(5,2)--存储平均分 +--查询科目编号赋值给@subjecton +--查询平均分赋值给@avg +if(@avg>70) + begin + print '成绩优秀' + select语句显示前3名考生信息 + end +else + begin + print '成绩较差' + select语句显示后3名考生信息 + end +``` + +输出的时候会发现只有select语句输出了print没有输出 + +**** + +#### ==(1)如何同时显示print和select输出语句== + +工具->选项->查询结果->SQL Server->以文本格式显示->重新启动(保险起见操作前保存代码) + +******** + +### 2.**while-continue-break**语句 + +语法: continue[继续] + +```sql +while(条件表达式) + begin + 命令行或程序块 + [break] →→ 跳出循环 + [continue] →→ 可以让程序跳过continue命令之后的语句 + 命令行或程序块 + end +``` + +==如果有多条语句才需要begin-end语句块== + +****** + +### 3.case语句 + +```sql +select 条件表达式, case [compare_value] + when 条件1 then 结果1 + when 条件2 then 结果2 + [else 其他结果] + end [列名] +from 表名 + +--批处理可以提高语句的执行效率 +``` + +==else:表示case中所有when条件均不为true时返回的结果,如果省略else且when条件都为false时, case语句返回null== + +示例:根据生日添加一个生肖列 + +CASE句法返回第一个value = year(PeopleBirth) % 12比较结果为真的结果。 如果没有比较结果符合,则返回ELSE后的结果,如果没有ELSE部分,则返回NULL: + +```sql +select PeopleName 姓名,PeopleSex 性别,PeopleSalary 工资,PeoplePhone 电话,PEOPLEBIRTH 生日, +case year(PeopleBirth) % 12 + when 4 then '鼠' + when 5 then '牛' + when 6 then '虎' + when 7 then '兔' + when 8 then '龙' + when 9 then '蛇' + when 10 then '马' + when 11 then '羊' + when 0 then '猴' + when 1 then '鸡' + when 2 then '狗' + when 3 then '猪' + ELSE '' +end 生肖 +from People +``` + +****** + +查询银行卡信息,将银行卡状态1,2,3,4分别转换为汉字“正常,挂失,冻结,注销”,并且根据银行卡余额显示银行卡等级 30万以下为“普通用户”,30万及以上为"VIP用户", + +```csharp +select CardNo ,AccountCode ,RealName ,CardMoney ,CardState, +case +when CardState='1' then '正常' +when CardState='2' then '挂失' +when CardState='3' then '冻结' +when CardState='4' then '注销' +end 银行卡状态, +case +when CardMoney >=300000 then 'VIP用户' +when CardMoney <300000 then '普通用户' +end 用户等级 +from BankCard b +join AccountInfo a on b.AccountId=a.AccountId +``` + +******** + +### 4.特殊字符 + +制表符 CHAR(9);换行符 CHAR(10);回车 CHAR(13); \ No newline at end of file diff --git "a/15\345\220\264\346\230\216\346\235\260/\347\273\203\344\271\240/220906-\351\200\273\350\276\221\350\257\255\345\217\245\347\273\203\344\271\240.md" "b/15\345\220\264\346\230\216\346\235\260/\347\273\203\344\271\240/220906-\351\200\273\350\276\221\350\257\255\345\217\245\347\273\203\344\271\240.md" new file mode 100644 index 0000000..89395f8 --- /dev/null +++ "b/15\345\220\264\346\230\216\346\235\260/\347\273\203\344\271\240/220906-\351\200\273\350\276\221\350\257\255\345\217\245\347\273\203\344\271\240.md" @@ -0,0 +1,106 @@ +--练习:在 StuScore 表中如果学生的平均成绩没有达到80分,便给每位同学的数学成绩加1分,然后再次判断平均成绩是否达到80分, +--否则继续加分,这样反复加分,直到其平均成绩超过80分 + +```sql +while(1=1) +begin + declare @j int + select @j=AVG(Chinese+English+Math) from StuScore + print @j //226 + if(@j<80) + update StuScore set Math=Math+1 + else + break +end +``` + + + +--1. 为赵云此人进行开户开卡操作,赵云身份证:420107199904054233 + +``` +declare @cno varchar(30)='420107199904054233' +insert AccountInfo values(@cno,'13542265123','赵云',getdate()) +insert BankCard values('6223695231569457',4,123456,0.00,1,getdate()) +``` + + + +--2. 需要求出张飞的银行卡卡号和余额,张飞身份证: (1.使用连接查询,2.使用变量) + +``` +declare @ano varchar(20)='420107199602034138 ' +select CardNo,CardMoney from AccountInfo a +join BankCard b on a.AccountId=b.AccountId +where AccountCode= @ano +``` + +-- 3.某用户银行卡号为“6225547854125656”,该用户执行取钱操作,取钱5000元,余额充足则进行取钱操作,并提示"取钱成功",否则提示“余额不足”。 + +``` +declare @acno varchar(30)='6225547854125656' +declare @cmoney money +select @cmoney=CardMoney from BankCard where CardNo=@acno +if @cmoney>=5000 +print '取钱成功' +else +print '余额不足' +``` + + +-- 条件分支:case-when + +-- 4.查询银行卡信息,将银行卡状态1,2,3,4分别转换为汉字“正常,挂失,冻结,注销”,并且根据银行卡余额显示银行卡等级 30万以下为“普通用户”,30万及以上为"VIP用户", +----显示列分别为卡号,身份证,姓名,余额,用户等级,银行卡状态。 + +``` +select CardNo ,AccountCode ,RealName ,CardMoney ,CardState, +case +when CardState='1' then '正常' +when CardState='2' then '挂失' +when CardState='3' then '冻结' +when CardState='4' then '注销' +end 银行卡状态, +case +when CardMoney >=300000 then 'VIP用户' +when CardMoney <300000 then '普通用户' +end 用户等级 +from BankCard b +join AccountInfo a on b.AccountId=a.AccountId +``` + + +--**while** + +-- 5.循环打印1-10。 + +``` +declare @n int=1 +while @n<=10 +begin +print @n +set @n=@n+1 +end +``` + +-- 6.打印99乘法表 + +``` +declare @i int =1 +while @i<=9 +begin + declare @j int =1 + declare @ret varchar(200)=' ' + +while @j<=@i +begin +set @ret=@ret + cast(@j as varchar(1))+'*'+cast(@i as varchar(1))+'='+cast(@i*@j as varchar(10))+char(9) +set @j=@j+1 +end + +print @ret +set @i=@i+1 + +end +``` + -- Gitee From 479c555e3f8ee00e5d627b51ad5aeb27032c4659 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=90=B4=E6=98=8E=E6=9D=B0?= <3090356592@qq.com> Date: Tue, 6 Sep 2022 23:46:00 +0800 Subject: [PATCH 3/3] 9.6 --- ...\276\221\350\257\255\345\217\245\347\273\203\344\271\240.md" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/15\345\220\264\346\230\216\346\235\260/\347\273\203\344\271\240/220906-\351\200\273\350\276\221\350\257\255\345\217\245\347\273\203\344\271\240.md" "b/15\345\220\264\346\230\216\346\235\260/\347\273\203\344\271\240/220906-\351\200\273\350\276\221\350\257\255\345\217\245\347\273\203\344\271\240.md" index 89395f8..d90e80f 100644 --- "a/15\345\220\264\346\230\216\346\235\260/\347\273\203\344\271\240/220906-\351\200\273\350\276\221\350\257\255\345\217\245\347\273\203\344\271\240.md" +++ "b/15\345\220\264\346\230\216\346\235\260/\347\273\203\344\271\240/220906-\351\200\273\350\276\221\350\257\255\345\217\245\347\273\203\344\271\240.md" @@ -7,7 +7,7 @@ begin declare @j int select @j=AVG(Chinese+English+Math) from StuScore print @j //226 - if(@j<80) + if(@j<=80) update StuScore set Math=Math+1 else break -- Gitee