By TreeDream
存储过程、触发器、数据完整性存储过程创建存储过程修改存储过程删除存储过程触发器创建触发器数据完整性约束默认值创建默认值绑定默认值解除默认规则创建规则绑定和解除规则,删除
存储过程
将需要多次调用以实现某个特定任务的代码段编成一个过程
创建存储过程
create procedure exp1 as select * from Stock Go exec exp1
带参数的存储过程
if exists (select name from sysobjects where name ='exp2' and type = 'P') drop procedure exp2 Go create procedure exp2 @mno char(8),@mname varchar(50),@mspeci varchar(20) as insert into Stock(mat_num,mat_name,speci) values(@mno,@mname,@mspeci) GO execute exp2 'm030','护套绝缘电线','BVV-35'
带默认输入参数的存储过程
if exists (select name from sysobjects where name = 'exp3' and type = 'P') drop procedure exp3 GO create procedure exp3 @mname varchar(50) = '%绝缘%',@pno char(8) = '20110005' as select mat_name,speci,prj_name,prj_status from Stock,Salvaging,Out_stock where Stock.mat_num = Out_stock.mat_num and Salvaging.prj_num = Out_stock.prj_num and mat_name like @mname and Salvaging.prj_num = @pno GO execute exp3 execute exp3 @pno = '20110002'
带输出参数的存储过程
if exists (select name from sysobjects where name = 'exp4' and type = 'P') drop procedure exp4 GO create procedure exp4 @pn char(8),@sum int output as select @sum = sum(amount) from Out_stock where prj_num = @pn GO declare @total int execute exp4 '20110002',@total output print '总量'+ cast(@total as varchar(20))
修改存储过程
修改和创建相同alter
删除存储过程
drop procedure exp4
触发器
当用户进行插入,删除,更新等数据操作的时候,自动触发所定义的SQL语句
创建触发器
insert触发器,同时将数据复制到基本表和内存中的Inserted表中
if exists (select name from sysobjects where name = 'tr1_stock' and type = 'P') drop trigger tr1_stock GO create trigger tr1_stock on Stock after insert as declare @amount int select @amount = amount from inserted if @amount < 1 Begin rollback tran raiserror ('amount must be greater than 1!',16,10) END GO
create trigger tr1_outstock on Out_stock after insert as Begin declare @m_num char(8),@m_amount int select @m_num = mat_num,@m_amount = amount from inserted update stock set amount = amount - @m_amount where mat_num = @m_num END GO
delete触发器:将删除的数据保存于deleted中,SQL语句中还可以引用
create trigger tr2_stock on Stock after delete as Begin declare @mat_num char(8) select @mat_num = mat_num from deleted delete from Out_stock where mat_num = @mat_num END
update触发器:合并了inserted deleted 触发器
create trigger tr3_stock on Stock after update as declare @amount_new int,@amount_old int,@mat_num char(10) select @amount_new = amount,@mat_num = mat_num from inserted if @amount_new < 1 Begin select @amount_old = amount from deleted update Stock set amount = @amount_old where mat_num = @mat_num END
数据完整性
约束
-
建表的时候:列级完整性约束,表级完整性约束
-
alter table Salvaging add constraint PK_salvaging primary key(prj_num) alter table Salvaging add constraint data_check check(start_date<=end_date) alter table Salvaging add constraint DF_salvaging default (0) for prj_status alter table Salvaging drop constraint data_check
默认值
默认值对象独立于表
创建默认值
create default _Getdate as getdate()
绑定默认值
sp_bindefault _Getdate, 'Out_stock.get_date'
解除默认
sp_unbindefault _Getdate
规则
check约束是在create table中建立的,规则独立于表
创建规则
create rule amount_rule as @amount>0 and @amount<=100
绑定和解除规则,删除
create rule amount_rule as @amount>0 and @amount<=100 sp_bindrule amount_rule,'Stock.amount' sp_unbindrule amount_rule,'Stock.amount' drop rule amount_rule