问个关于sql2000中关于创建触发器的问题
因为在创建触发器的时候,触发器的名称事先不能确定,就是说触发器的名称不断的变化,我要用一个变量把名称临时存储起来
如 declare @mytablename varchar(20)
.......
.......
create trigger @mytablename+ '_insert ' on @mytablename for insert as
begin
insert into tb_record(mytablename,information,controldate)
values(@mytablename,@infor_insert,left(convert(varchar(20),getdate(),120),10))
end
提示在关键字 'trigger ' 附近有语法错误。
就是说应该怎么实现 创建出发器时候名称用变量存储??????
------解决方案--------------------select @sql= 'create trigger '+@mytablename+ '_insert on @mytablename for insert as
begin
insert into tb_record(mytablename,information,controldate)
values(@mytablename,@infor_insert,left(convert(varchar(20),getdate(),120),10))
end '
exec @sql
------解决方案--------------------declare @mytablename varchar(20)
declare @sql as varchar(1000)
set @sql = '
create trigger ' ' + @mytablename + ' '_insert on ' ' + @mytablename + ' ' for insert as
begin
insert into tb_record(mytablename,information,controldate)
values( ' ' + @mytablename + ' ', ' ' + @infor_insert + ' ',left(convert(varchar(20),getdate(),120),10))
end '
exec(@sql)
------解决方案--------------------你的 WHERE name =tb_book_insert
这里缺了引号.
我上面写的也缺引号,但我要表达的就是那个意思.
还是老乌龟比较负责严谨.
------解决方案-------------------- declare @sql varchar(500)
declare @mytablename varchar(100)
declare @infor_insert varchar(100)
declare @i varchar(100)
select @mytablename= 'chen '
select @infor_insert= 'haha '
select @i= '1 '
select @sql= 'IF EXISTS (SELECT name FROM sysobjects WHERE name = ' ' '+@mytablename+@i+ ' ' ' AND type = ' 'TR ' ' DROP trigger '
+@mytablename+@i + ' go create trigger '+@mytablename+@i+ ' on '+@mytablename
+ ' for insert as '
+ ' begin '
+ ' insert into tb_record(mytablename,information,controldate) '
+ ' values( ' ' '+@mytablename+ ' ' ', ' ' '+@infor_insert+ ' ' ',left(convert(varchar(20),getdate(),120),10)) end go '
select @sql
------解决方案--------------------select @sql 改成 exec @sql