场景是这样的
我在程序中动态创建两张表,假设为A和B,
然后我想为A表创建触发器,当A发生变化时,触发B发生变化。
目前动态创建表好办,就是动态创建触发器没有实现
补充:
表名是动态的,作为参数
当然可以。可以写一个专门的存储过程,传递表名进去,然后实现触发器的创建操作。
至于触发器的创建操作这里就不给出具体的代码了,你可以把现有的表里的触发器的创建代码拿出来使用(对触发器按右键,在右键菜单里选择“编写触发器脚本为”,然后选择“Create到”再选择“新建查询窗口”)。
要注意的是,触发器也要,表也好,都有所有者,这里最简单的是使用dbo(一般都这样),具体的看你自己的情况而定。
我的想法就是在存储过程里面建触发器,程序调用存储过程
@Good_Luck: 对啊。我给你的方案就是这样。你创建一个专门用于创建某个触发器的存储过程,传递进表名(参数),然后在存储过程里构建触发器。
CREATE PROCEDURE dbo.spMyTriggerCreator @table varchar(100) AS BEGIN CREATE TRIGGER dbo.MyTrigger ON dbo.@table AFTER insert AS BEGIN END END
当然,这个构建过程是会失败的,因为@table这个变量不能使用于CREATETrigger这个命令。
此时,你需要把构建触发器的过程代码全部都写入到一个字符串变量中,然后再执行这个SQL语句。
CREATE PROCEDURE [dbo].[sp_AutoTrigger] @tablename varchar(50)----表名 AS declare @strSQL varchar(6000) -- 主语句 declare @table_detail varchar(50) declare @table_count varchar(50) declare @table_sum1 varchar(50) declare @table_sum2 varchar(50) declare @T_table_count varchar(50) set @table_detail = @tablename+'_detail'; set @table_count = @tablename + '_Count'; set @table_sum1 = @tablename+'_Sum1'; set @table_sum2 = @tablename+'_Sum2'; set @T_table_count = 'T_'+@tablename+'_count' set @strSQL = 'CREATE TRIGGER [dbo].['+@T_table_count+'] ON [dbo].['+@table_detail+'] AFTER INSERT,DELETE,UPDATE AS BEGIN SET NOCOUNT ON; if not exists (select * from deleted)--添加 begin /* 如果deleted表为空,那么 */ declare @scan00 date,@scan08 nvarchar(50), @scan10 nvarchar(50),@scan_type nvarchar(50), @scan02 nvarchar(50),@scan03 nvarchar(50) select @scan00=scan00, @scan_type= ( case when (Substring(scan08,1,3)=''KGH'' or Substring(scan08,1,2)=''FH'') then ''成品'' when (Substring(scan08,1,4)=''KGMS'' or Substring(scan08,1,3)=''FMS'') then ''门扇'' when (Substring(scan08,1,4)=''KGMK'' or Substring(scan08,1,3)=''FMK'') then ''门框'' else '' end ), @scan10=scan10,@scan02=scan02,@scan03=scan03 from inserted if @scan00 is not null begin /*汇总添加*/ if not exists ( select * from '+@table_count+' where scan00=@scan00 and scan_type=@scan_type and scan10=@scan10) begin insert '+@table_count+'(scan00,scan_type,scan_total,scan10,scan_sum) values(@scan00,@scan_type,1,@scan10,1) end else begin update '+@table_count+' set scan_sum=scan_sum+1 where scan00=@scan00 and scan_type=@scan_type and scan10=@scan10 end update '+@table_count+' set scan_total=(select max(scan_sum) from '+@table_count+' where scan00=@scan00 and scan_type=@scan_type) where scan00=@scan00 and scan_type=@scan_type /*分组合计1添加*/ if not exists (select * from '+@table_sum1+' where scan00=@scan00 and scan02=@scan02 and scan03=@scan03 and scan10=@scan10) begin insert '+@table_sum1+'(scan00,scan02,scan03,scan10,scan_sum) values(@scan00,@scan02,@scan03,@scan10,1) end else begin update '+@table_sum1+' set scan_sum=scan_sum+1 where scan00=@scan00 and scan02=@scan02 and scan03=@scan03 and scan10=@scan10 end /*分组合计2添加*/ if not exists (select * from '+@table_sum2+' where scan00=@scan00 and scan02=@scan02 and scan03=@scan03 and scan10=@scan10 and scan_type=@scan_type) insert '+@table_sum2+'(scan00,scan02,scan03,scan10,scan_sum,scan_type) values(@scan00,@scan02,@scan03,@scan10,1,@scan_type) else begin update '+@table_sum2+' set scan_sum=scan_sum+1 where scan00=@scan00 and scan02=@scan02 and scan03=@scan03 and scan10=@scan10 and scan_type=@scan_type end end end else if not exists (select * from inserted)--删除 begin --declare @scan00 date,@scan08 nvarchar(50),@scan10 nvarchar(50),@scan_type nvarchar(50) select @scan00=scan00, @scan_type= ( case when (Substring(scan08,1,3)=''KGH'' or Substring(scan08,1,2)=''FH'') then ''成品'' when (Substring(scan08,1,4)=''KGMS'' or Substring(scan08,1,3)=''FMS'') then ''门扇'' when (Substring(scan08,1,4)=''KGMK'' or Substring(scan08,1,3)=''FMK'') then ''门框'' else '' end ), @scan10=scan10,@scan02=scan02,@scan03=scan03 from deleted if @scan00 is not null begin /*汇总删除*/ update '+@table_count+' set scan_sum=scan_sum-1 where scan00=@scan00 and scan_type=@scan_type and scan10=@scan10 update '+@table_count+' set scan_total=(select max(scan_sum) from '+@table_count+' where scan00=@scan00 and scan_type=@scan_type) where scan00=@scan00 and scan_type=@scan_type /*分组合计1删除*/ update '+@table_sum1+' set scan_sum=scan_sum-1 where scan00=@scan00 and scan02=@scan02 and scan03=@scan03 and scan10=@scan10 /*分组合计2删除*/ update '+@table_sum2+' set scan_sum=scan_sum-1 where scan00=@scan00 and scan02=@scan02 and scan03=@scan03 and scan10=@scan10 and scan_type=@scan_type end end END ' exec (@strSQL)
@笨笨蜗牛:
你帮我看看单引号里面也有单引号的问题,存储过程是建立了,但是在调用的时候,报错了
'KGH'附近有语法错误
@Good_Luck: 有点忙,没细看。如果单引号里又有单引号,只要把两个单引号紧挨着放在一起就可以。SQL会识别连续两个单引号是字符串里的一个单引号。
可以。无论你是通过程序所谓的“动态创建”,还是自己手动在SQL SERVER里创建,一样执行了脚本。
可以的,不管什么语句,只要你在查询分析器中可以执行,那么你在程序中一样执行就可以。