From 19d2dad1af9cb8ef1aa6c064adfaa630aff20e2d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E4=B8=96=E8=B4=A2?= <10033750+wang-shicaishicai@user.noreply.gitee.com> Date: Tue, 11 Oct 2022 15:28:41 +0000 Subject: [PATCH 1/7] =?UTF-8?q?=E6=96=B0=E5=BB=BA=202022-10-7=E7=AC=AC?= =?UTF-8?q?=E5=8D=81=E4=B8=80=E6=AC=A1=E4=BD=9C=E4=B8=9A+=E7=AC=94?= =?UTF-8?q?=E8=AE=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../.keep" | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 "20\347\216\213\344\270\226\350\264\242/2022-10-7\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232+\347\254\224\350\256\260/.keep" diff --git "a/20\347\216\213\344\270\226\350\264\242/2022-10-7\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232+\347\254\224\350\256\260/.keep" "b/20\347\216\213\344\270\226\350\264\242/2022-10-7\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232+\347\254\224\350\256\260/.keep" new file mode 100644 index 0000000..e69de29 -- Gitee From 2fd67ee41db7ec50f8826ea8b24d1000a2335fd4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E4=B8=96=E8=B4=A2?= <10033750+wang-shicaishicai@user.noreply.gitee.com> Date: Tue, 11 Oct 2022 15:30:38 +0000 Subject: [PATCH 2/7] =?UTF-8?q?add=2020=E7=8E=8B=E4=B8=96=E8=B4=A2/2022-10?= =?UTF-8?q?-7=E7=AC=AC=E5=8D=81=E4=B8=80=E6=AC=A1=E4=BD=9C=E4=B8=9A+?= =?UTF-8?q?=E7=AC=94=E8=AE=B0/=E7=AC=AC=E5=8D=81=E4=B8=80=E6=AC=A1?= =?UTF-8?q?=E4=BD=9C=E4=B8=9A.sql.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 王世财 <10033750+wang-shicaishicai@user.noreply.gitee.com> --- ...0\346\254\241\344\275\234\344\270\232.sql" | 81 +++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 "20\347\216\213\344\270\226\350\264\242/2022-10-7\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232+\347\254\224\350\256\260/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232.sql" diff --git "a/20\347\216\213\344\270\226\350\264\242/2022-10-7\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232+\347\254\224\350\256\260/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232.sql" "b/20\347\216\213\344\270\226\350\264\242/2022-10-7\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232+\347\254\224\350\256\260/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232.sql" new file mode 100644 index 0000000..d21730c --- /dev/null +++ "b/20\347\216\213\344\270\226\350\264\242/2022-10-7\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232+\347\254\224\350\256\260/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232.sql" @@ -0,0 +1,81 @@ +--定义存储过程实现查询出账户余额最低的银行卡账户信息,显示银行卡号,姓名,账户余额 +create proc proc_GetBankMessage +as +select a.RealName,b.CardNo,b.CardMoney from AccountInfo a inner join BankCard b on a.AccountId = b.AccountId +where b.CardMoney = (select min(CardMoney) from BankCard) +go +exec proc_GetBankMessage +go +--模拟银行卡存钱操作,传入银行卡号,存钱金额,实现存钱操作 +create proc proc_InMoney +@cardNo varchar(20), +@InMoney money +as +begin + update BankCard set CardMoney += @InMoney where CardNo = @cardNo + insert into CardExchange values(@cardNo,@InMoney,0,getdate()) + +end +go +exec proc_InMoney '6225547854125656',5000 +go + +--模拟银行卡取钱操作,传入银行卡号,取钱金额,实现取钱操作,取钱成功,返回1,取钱失败返回-1 +create proc proc_OutMoney +@cardNo varchar(20), +@OutMoney money +as +begin + declare @code int + declare @CardMoney money = (select CardMoney from BankCard where CardNo = @cardNo) + if(@CardMoney - @OutMoney > 0) + begin + update BankCard set CardMoney -= @CardMoney where CardNo = @cardNo + insert into CardExchange values (@cardNo,0,@OutMoney,getdate()) + return 1 + end + return -1 +end +go +declare @code int + exec @code = proc_OutMoney '6225547858741263',7000 + print @code + go + --**查询出某时间段的银行存取款信息以及存款总金额**,取款总金额,传入开始时间,结束时间, + --显示存取款交易信息的同时,返回存款总金额,取款总金额。 + create proc proc_GetChargeMessage + @Start_time smalldatetime, + @End_time smalldatetime, + @SumInMoney money output, + @SumOutMoney money output + as + begin + select @SumInMoney = sum(MoneyInBank),@SumOutMoney = sum(MoneyOutBank) from CardExchange + where ExchangeTime between @Start_time and @End_time + end + go +declare @Start_time smalldatetime = '2022-09-05 ', @End_time smalldatetime = '2022-09-19' +declare @SumInMoney money , @SumOutMoney money +exec proc_GetChargeMessage @Start_time,@End_time,@SumInMoney output,@SumOutMoney output +select @SumInMoney 存款总金额,@SumOutMoney 取款总金额 +go +--**密码升级**,传入用户名和密码,如果用户名密码正确,并且密码长度<8,自动升级成8位密码 +--(提示:随机生成 0-9 的整数: floor(rand()*10)) rand():随机生成0.0-1.0的小数 floor:向下取整) +create proc proc_PassWordUp +@userName varchar(10),@passWord varchar(10) output +as +begin + if(@userName = '呼呼' and @passWord = '123456' and len(@passWord)<8) + begin + set @passWord = Convert(decimal(8,0),floor(rand()*100000000)) + end +end +go +drop proc proc_PassWordUp +declare @userName varchar(10) = '呼呼' +declare @passWord varchar(10) = '123456' + +exec proc_PassWordUp @userName,@passWord output +select @passWord + + -- Gitee From fdfa96c2e7fa2642309311f5ee6884b85f8e477d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E4=B8=96=E8=B4=A2?= <10033750+wang-shicaishicai@user.noreply.gitee.com> Date: Tue, 11 Oct 2022 15:31:45 +0000 Subject: [PATCH 3/7] =?UTF-8?q?add=2020=E7=8E=8B=E4=B8=96=E8=B4=A2/2022-10?= =?UTF-8?q?-7=E7=AC=AC=E5=8D=81=E4=B8=80=E6=AC=A1=E4=BD=9C=E4=B8=9A+?= =?UTF-8?q?=E7=AC=94=E8=AE=B0/=E7=AC=AC=E5=8D=81=E4=B8=80=E6=AC=A1?= =?UTF-8?q?=E7=AC=94=E8=AE=B0.md.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 王世财 <10033750+wang-shicaishicai@user.noreply.gitee.com> --- ...00\346\254\241\347\254\224\350\256\260.md" | 103 ++++++++++++++++++ 1 file changed, 103 insertions(+) create mode 100644 "20\347\216\213\344\270\226\350\264\242/2022-10-7\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232+\347\254\224\350\256\260/\347\254\254\345\215\201\344\270\200\346\254\241\347\254\224\350\256\260.md" diff --git "a/20\347\216\213\344\270\226\350\264\242/2022-10-7\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232+\347\254\224\350\256\260/\347\254\254\345\215\201\344\270\200\346\254\241\347\254\224\350\256\260.md" "b/20\347\216\213\344\270\226\350\264\242/2022-10-7\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232+\347\254\224\350\256\260/\347\254\254\345\215\201\344\270\200\346\254\241\347\254\224\350\256\260.md" new file mode 100644 index 0000000..67f2d75 --- /dev/null +++ "b/20\347\216\213\344\270\226\350\264\242/2022-10-7\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232+\347\254\224\350\256\260/\347\254\254\345\215\201\344\270\200\346\254\241\347\254\224\350\256\260.md" @@ -0,0 +1,103 @@ +存储过程(Stored Procedure) +什么是存储过程 +存储过程是预编译SQL语句集合,这些语句存储在一个名称(存储过程的名称)下并作为单元来处理。存储过程代替了传统的逐条执行SQL语句的方式,一个存储过程中可以包含查询、插入、删除、更新等操纵的一系列SQL语句,当这个存储过程被调用执行时,这些操作也会同时执行。 + +封装好 --> 调用 + +存储过程的分类 +系统存储过程 +系统存储过程是用来管理SQL Server与显示有关数据库和用户的信息的存储过程。 + +常见的系统存储过程有 + +sp_databases 列出服务上的所有数据库 +sp_helpdb --报告有关指定数据库或所有数据库的信息 +sp_renamedb 更改数据库的名称 +sp_tables --返回当前环境下可查询的对象的列表 +sp_columns 返回某个表列的信息 +sp_help --返回某个表的所有信息 +sp_helpconstraint 查看某个表的约束 +sp_helpindex --查看某个表的索引 +sp_stored_procedures 列出当前环境中的所有存储过程 +sp_password --添加或修改登录账户的密码 +sp_rename 重命名存储过程 +sp_helptext 显示默认值,未加密的存储过程、用户定义的存储过程、触发器或视图的实际文本。 +自定义存储过程 +创建存储过程 + +1.没有输入参数,没有输出参数的存储过程。 + +create proc <存储过程名称> +as + +go +练习: 定义存储过程查询 年龄最小的学生信息 + +2.有输入参数,没有输出参数的存储过程 + +create proc <存储过程名称> +<变量1> <数据类型> +<变量2> <数据类型> +... +as + +go +3.有输入参数,没有输出参数,但是有返回值的存储过程(返回值必须整数)。 + +create proc <存储过程名称> +<变量1> <数据类型> +<变量2> <数据类型> +... +as + +return 整数 +go +4.有输入参数,有输出参数的存储过程 & 一个变量具备同时输入输出参数的存储过程 + +create proc <存储过程名称> +<变量1> <数据类型> output +<变量2> <数据类型> output +... +as + +return 整数 +go +执行存储过程 + +--无参 +exec <存储过程名称> +--带参 +exec <存储过程名称> <形参1>,<形参2>,... +--带参带返回值 +declare @变量 +exec @变量 = <存储过程名称> <形参1>,<形参2>,... +--有输入参数,有输出参数的存储过程 +declare @变量 +exec <存储过程名称> <形参1>,<形参2>,@变量 output +--一个变量同时具备输入输出功能 +declare @变量 <数据类型> = 值 +exec <存储过程名称> <形参1>,<形参2>,@变量 output +删除存储过程 + +drop procedure <存储过程名称> +存储过程的优点 +允许模块化程序设计 + +执行速度更快 + +减少网络流通量 + +提高系统安全性 + +存储过程与函数的区别 +1.储存过程可以有返回值也可以无返回值。函数必须有返回值。 + +2.存储过程的实现比较复杂,而函数的实现比较有针对性。 + +3.储存过程可以输入输出参数,而函数只可以输入参数。 + +4.过程允许在其中选择以及DML语句,而函数只可以在其中select语句。 + +5.可以在存储过程中调用函数,不可以在函数中调用存储过程。 + +6.函数可以作为条件在语句中使用,但是存储过程不可以。 \ No newline at end of file -- Gitee From ed84b2dc8621a2c370bb6a6833af8f3ec57c1f03 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E4=B8=96=E8=B4=A2?= <10033750+wang-shicaishicai@user.noreply.gitee.com> Date: Tue, 11 Oct 2022 15:32:05 +0000 Subject: [PATCH 4/7] =?UTF-8?q?=E6=96=B0=E5=BB=BA=202022-10-10?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- "20\347\216\213\344\270\226\350\264\242/2022-10-10/.keep" | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 "20\347\216\213\344\270\226\350\264\242/2022-10-10/.keep" diff --git "a/20\347\216\213\344\270\226\350\264\242/2022-10-10/.keep" "b/20\347\216\213\344\270\226\350\264\242/2022-10-10/.keep" new file mode 100644 index 0000000..e69de29 -- Gitee From 615e42a8c5c37fa4df6f1d1d025c16f32859021f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E4=B8=96=E8=B4=A2?= <10033750+wang-shicaishicai@user.noreply.gitee.com> Date: Tue, 11 Oct 2022 15:32:38 +0000 Subject: [PATCH 5/7] =?UTF-8?q?=E9=87=8D=E5=91=BD=E5=90=8D=2020=E7=8E=8B?= =?UTF-8?q?=E4=B8=96=E8=B4=A2/2022-10-10=20=E4=B8=BA=2020=E7=8E=8B?= =?UTF-8?q?=E4=B8=96=E8=B4=A2/2022-10-10=E7=AC=AC=E5=8D=81=E4=BA=8C?= =?UTF-8?q?=E6=AC=A1=E4=BD=9C=E4=B8=9A+=E7=AC=94=E8=AE=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../.keep" | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename "20\347\216\213\344\270\226\350\264\242/2022-10-10/.keep" => "20\347\216\213\344\270\226\350\264\242/2022-10-10\347\254\254\345\215\201\344\272\214\346\254\241\344\275\234\344\270\232+\347\254\224\350\256\260/.keep" (100%) diff --git "a/20\347\216\213\344\270\226\350\264\242/2022-10-10/.keep" "b/20\347\216\213\344\270\226\350\264\242/2022-10-10\347\254\254\345\215\201\344\272\214\346\254\241\344\275\234\344\270\232+\347\254\224\350\256\260/.keep" similarity index 100% rename from "20\347\216\213\344\270\226\350\264\242/2022-10-10/.keep" rename to "20\347\216\213\344\270\226\350\264\242/2022-10-10\347\254\254\345\215\201\344\272\214\346\254\241\344\275\234\344\270\232+\347\254\224\350\256\260/.keep" -- Gitee From f2a08ec7b4da6e11bd867d8c3dfd61422ee2f76b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E4=B8=96=E8=B4=A2?= <10033750+wang-shicaishicai@user.noreply.gitee.com> Date: Tue, 11 Oct 2022 15:33:07 +0000 Subject: [PATCH 6/7] =?UTF-8?q?add=2020=E7=8E=8B=E4=B8=96=E8=B4=A2/2022-10?= =?UTF-8?q?-10=E7=AC=AC=E5=8D=81=E4=BA=8C=E6=AC=A1=E4=BD=9C=E4=B8=9A+?= =?UTF-8?q?=E7=AC=94=E8=AE=B0/=E7=AC=AC=E5=8D=81=E4=BA=8C=E6=AC=A1?= =?UTF-8?q?=E4=BD=9C=E4=B8=9A.sql.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 王世财 <10033750+wang-shicaishicai@user.noreply.gitee.com> --- ...4\346\254\241\344\275\234\344\270\232.sql" | 54 +++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 "20\347\216\213\344\270\226\350\264\242/2022-10-10\347\254\254\345\215\201\344\272\214\346\254\241\344\275\234\344\270\232+\347\254\224\350\256\260/\347\254\254\345\215\201\344\272\214\346\254\241\344\275\234\344\270\232.sql" diff --git "a/20\347\216\213\344\270\226\350\264\242/2022-10-10\347\254\254\345\215\201\344\272\214\346\254\241\344\275\234\344\270\232+\347\254\224\350\256\260/\347\254\254\345\215\201\344\272\214\346\254\241\344\275\234\344\270\232.sql" "b/20\347\216\213\344\270\226\350\264\242/2022-10-10\347\254\254\345\215\201\344\272\214\346\254\241\344\275\234\344\270\232+\347\254\224\350\256\260/\347\254\254\345\215\201\344\272\214\346\254\241\344\275\234\344\270\232.sql" new file mode 100644 index 0000000..0286da5 --- /dev/null +++ "b/20\347\216\213\344\270\226\350\264\242/2022-10-10\347\254\254\345\215\201\344\272\214\346\254\241\344\275\234\344\270\232+\347\254\224\350\256\260/\347\254\254\345\215\201\344\272\214\346\254\241\344\275\234\344\270\232.sql" @@ -0,0 +1,54 @@ +-(1)假设有部门表和员工表,在添加员工的时候,该员工的部门编号如果在部门表中找不到, +--则自动添加部门信息,部门名称为"新部门"。 +create trigger tri_AddPeople +on People for insert +as +declare @DepartmentId int +select @DepartmentId = DepartmentId from People +if not exists(select * from Department where DepartmentId = @DepartmentId) + insert into Department values(@DepartmentId,'新部门') + +insert into People values('007','呼呼','男',15000) +go +--(2)触发器实现,删除一个部门的时候将部门下所有员工全部删除。 +create trigger tri_Delete +on Department for delete +as +delete People where DepartmentId = (select DepartmentId from deleted) +go +delete Department where DepartmentId = 1 +go + +--(4)修改一个部门编号之后,将该部门下所有员工的部门编号同步进行修改 +create trigger tri_UpdateDepartment +on Department for update +as +declare @Old_DepartmentId int ,@New_DepartmentId int +select @Old_DepartmentId = (select DepartmentId from deleted) +select @New_DepartmentId = (select DepartmentId from inserted) +update People set DepartmentId = @New_DepartmentId where DepartmentId = @Old_DepartmentId + +update Department set DepartmentId = '003' where DepartmentId = '001' + +--总经办-1000 市场部员工工资-2000 +--用存储过程和游标 +declare cur_PayCut cursor scroll +for select DepartmentId from Department + +go +create proc proc_PayCut +as +begin + open cur_PayCut + declare @DepartmentId int,@money money = 1000 + fetch first from cur_PayCut into @DepartmentId + while(@@FETCH_STATUS = 0) + begin + update People set PeopleSalary -= @money where DepartmentId = @DepartmentId + set @money += 1000 + fetch next from cur_PayCut into @DepartmentId + end + close cur_PayCut +end + +exec proc_PayCut \ No newline at end of file -- Gitee From a9808ededea9f077e04a8d487eff16852c0a2c8e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E4=B8=96=E8=B4=A2?= <10033750+wang-shicaishicai@user.noreply.gitee.com> Date: Tue, 11 Oct 2022 15:34:00 +0000 Subject: [PATCH 7/7] =?UTF-8?q?add=2020=E7=8E=8B=E4=B8=96=E8=B4=A2/2022-10?= =?UTF-8?q?-10=E7=AC=AC=E5=8D=81=E4=BA=8C=E6=AC=A1=E4=BD=9C=E4=B8=9A+?= =?UTF-8?q?=E7=AC=94=E8=AE=B0/=E7=AC=AC=E5=8D=81=E4=BA=8C=E6=AC=A1?= =?UTF-8?q?=E7=AC=94=E8=AE=B0.md.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 王世财 <10033750+wang-shicaishicai@user.noreply.gitee.com> --- ...14\346\254\241\347\254\224\350\256\260.md" | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 "20\347\216\213\344\270\226\350\264\242/2022-10-10\347\254\254\345\215\201\344\272\214\346\254\241\344\275\234\344\270\232+\347\254\224\350\256\260/\347\254\254\345\215\201\344\272\214\346\254\241\347\254\224\350\256\260.md" diff --git "a/20\347\216\213\344\270\226\350\264\242/2022-10-10\347\254\254\345\215\201\344\272\214\346\254\241\344\275\234\344\270\232+\347\254\224\350\256\260/\347\254\254\345\215\201\344\272\214\346\254\241\347\254\224\350\256\260.md" "b/20\347\216\213\344\270\226\350\264\242/2022-10-10\347\254\254\345\215\201\344\272\214\346\254\241\344\275\234\344\270\232+\347\254\224\350\256\260/\347\254\254\345\215\201\344\272\214\346\254\241\347\254\224\350\256\260.md" new file mode 100644 index 0000000..2635bd1 --- /dev/null +++ "b/20\347\216\213\344\270\226\350\264\242/2022-10-10\347\254\254\345\215\201\344\272\214\346\254\241\344\275\234\344\270\232+\347\254\224\350\256\260/\347\254\254\345\215\201\344\272\214\346\254\241\347\254\224\350\256\260.md" @@ -0,0 +1,34 @@ +触发器(trigger) +什么是触发器: insert --》 一系列随着插入操作自动执行的sql语句集合 +触发器( trigger )是作为对数据库修改的连带效果而由系统自动执行的一条语句。它是一种特殊的存储过程。也是一个事务(可以回滚)。为了定义一个触发器,我们必须: + +指明什么时候执行触发器。这被拆分为引起触发器被检测的一个*事件*和触发器继续执行所必须满足的一个条件。 +指明当触发器执行时所采取的动作。 +触发器分类 +分为 DML触发器 + +DML触发器 : 增删改 +after触发器: 增删改 之后执行的 +after触发器主要用于:insert,update,delete + +instead of 触发器 : 增删改 之前检查的 +创建触发器的语句 +CREATE TRIGGER <触发器名称> + +ON table_name +FOR [DELETE, INSERT, UPDATE] + +AS + + T-SQL语句 + +GO +--WITH ENCRYPTION表示加密触发器定义的SQL文本 + +--DELETE, INSERT, UPDATE指定触发器的类型 +总结 +1.插入操作(Insert) ****inserted 表*有数据,*deleted 表****无数据 + +2.删除操作(Delete) ****inserted 表*无数据,*deleted 表****有数据 + +3.更新操作(Update) ****inserted 表*有数据(新数据),*deleted 表****有数据(旧数据) \ No newline at end of file -- Gitee