我的一个查询语句存储在某一个表的varchar字段中
我想在触发器中把它取出来执行,请问如何实现?
------解决方案--------------------
--用游标实现
drop table tbtest
go
create table tbtest(id int,sql varchar(8000))
insert into tbtest
select 1, 'select id from sysobjects where xtype= ' 'u ' ' '
union all select 2, 'select name from sysobjects where xtype= ' 'u ' ' '
create table #t1(id int)
create table #t2(name varchar(255))
declare @sql varchar(8000)
declare @id int
declare cur_tmp cursor for
select id,sql from tbtest
open cur_tmp
fetch next from cur_tmp into @id,@sql
while @@fetch_status=0
begin
if @id=1
begin
insert into #t1 --存储exec的结果
exec(@sql)
select * from #t1
end
if @id=2
begin
insert into #t2 --存储exec的结果
exec(@sql)
select * from #t2
end
fetch next from cur_tmp into @id,@sql
end
close cur_tmp
deallocate cur_tmp