From 46a892a66031ae3f4103ce7b59d8af0b5a5ce04c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BA=84=E4=BA=91?= <1487274726@qq.com> Date: Tue, 11 Oct 2022 20:41:32 +0800 Subject: [PATCH] =?UTF-8?q?=E7=AC=AC=E5=8D=81=E4=BA=8C=E6=AC=A1=E4=BD=9C?= =?UTF-8?q?=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...0\350\247\246\345\217\221\345\231\250.sql" | 59 +++++++++++++++++++ .../\347\254\224\350\256\260/SQL SERVER.md" | 38 ++++++++++++ 2 files changed, 97 insertions(+) create mode 100644 "21\345\272\204\344\272\221/\344\275\234\344\270\232/2022-10-10\350\247\246\345\217\221\345\231\250.sql" diff --git "a/21\345\272\204\344\272\221/\344\275\234\344\270\232/2022-10-10\350\247\246\345\217\221\345\231\250.sql" "b/21\345\272\204\344\272\221/\344\275\234\344\270\232/2022-10-10\350\247\246\345\217\221\345\231\250.sql" new file mode 100644 index 0000000..6282e1f --- /dev/null +++ "b/21\345\272\204\344\272\221/\344\275\234\344\270\232/2022-10-10\350\247\246\345\217\221\345\231\250.sql" @@ -0,0 +1,59 @@ +select * from Department +select * from People + +--(1)假设有部门表和员工表,在添加员工的时候,该员工的部门编号如果在部门表中找不到,则自动添加部门信息, +--部门名称为"新部门"。 +go + create trigger tri_newpeo + on People for insert + as + if not exists(select * from Department where DepartmentId=(select DepartmentId from inserted)) + begin + insert into Department values ((select DepartmentId from inserted),'新部门') + end + go + insert into People(DepartmentId,PeopleName,PeopleSex,PeopleSalary) values ('005','张三','男',5000) + +--(2)触发器实现,删除一个部门的时候将部门下所有员工全部删除。 +go + create trigger tri_deldep + on Department for delete + as + delete from People where DepartmentId=(select DepartmentId from deleted) + go + delete from Department where DepartmentId='005' +--(4)修改一个部门编号之后,将该部门下所有员工的部门编号同步进行修改 +go + create trigger tri_deldepid + on Department for update + as + update People set DepartmentId=(select DepartmentId from inserted) where DepartmentId=(select DepartmentId from deleted) + go + update Department set DepartmentId='006' where DepartmentId='005' + + -- 游标一般是放在存储过程中使用 +select * from Department +select * from People +--总经办-1000 市场部员工工资-2000 +--用存储过程和游标 +go +create proc pro_outmon +as +declare cur_addmon cursor scroll for select DepartmentName from People inner join Department on People.DepartmentId=Department.DepartmentId +open cur_addmon +declare @dename varchar(20) +fetch first from cur_addmon into @dename +while (@@FETCH_STATUS=0) +begin + if @dename='总经办' + begin + update People set PeopleSalary-=1000 where DepartmentId=(select DepartmentId from Department where DepartmentName=@dename) + end + else if @dename='市场部' + begin + update People set PeopleSalary-=2000 where DepartmentId=(select DepartmentId from Department where DepartmentName=@dename) + end + fetch next from cur_addmon into @dename +end +go +exec pro_outmon diff --git "a/21\345\272\204\344\272\221/\347\254\224\350\256\260/SQL SERVER.md" "b/21\345\272\204\344\272\221/\347\254\224\350\256\260/SQL SERVER.md" index 4e21c59..348f92b 100644 --- "a/21\345\272\204\344\272\221/\347\254\224\350\256\260/SQL SERVER.md" +++ "b/21\345\272\204\344\272\221/\347\254\224\350\256\260/SQL SERVER.md" @@ -717,3 +717,41 @@ drop procedure <瀛樺偍杩囩▼鍚嶇О> +### 鍗佷簩銆佽Е鍙戝櫒 + +#### DEL瑙﹀彂鍣 + +1. ##### after瑙﹀彂鍣 + + ###### 鍒涘缓瑙﹀彂鍣 + + ```sql + CREATE TRIGGER <瑙﹀彂鍣ㄥ悕绉> + + ON table_name + FOR [DELETE, INSERT, UPDATE] + + AS + + T-SQL璇彞 + + GO + --WITH ENCRYPTION琛ㄧず鍔犲瘑瑙﹀彂鍣ㄥ畾涔夌殑SQL鏂囨湰 + + --DELETE, INSERT, UPDATE鎸囧畾瑙﹀彂鍣ㄧ殑绫诲瀷 + + 1.鎻掑叆鎿嶄綔锛圛nsert锛 + ***\*inserted 琛╘****鏈夋暟鎹紝***\*deleted 琛╘****鏃犳暟鎹 + + 2.鍒犻櫎鎿嶄綔锛圖elete锛 + ***\*inserted 琛╘****鏃犳暟鎹紝***\*deleted 琛╘****鏈夋暟鎹 + + 3.鏇存柊鎿嶄綔锛圲pdate锛 + ***\*inserted 琛╘****鏈夋暟鎹紙鏂版暟鎹級锛***\*deleted 琛╘****鏈夋暟鎹紙鏃ф暟鎹級 + + + ``` + + + +2. ##### instead of瑙﹀彂鍣 -- Gitee