alter procedure usp_ReplaceSourceText
(
@t1 nvarchar(max)
)
as
declare @sql nvarchar(max)
begin
set @sql='select ''update a
set a.source_text=b.new
from ''+name+'' a,'+@t1+' b
where a.source_text=b.old;'' from sys.tables where name like ''%_transdb'''
exec(@sql)
end执行结果是:
update a   set a.source_text=b.new   from aa_transdb a,t1 b   where a.source_text=b.old;
update a   set a.source_text=b.new   from ab_transdb a,t1 b   where a.source_text=b.old;
...想要的结果是把上面的结果在存储过程里直接执行了。
不知道我有没有说明白,就是上面的存储过程里动态生成了SQL语句,然后想把生成的SQL语句也一起执行了。这样存储过程该怎么写?返回值什么的应该怎么写才能看出执行的效果?谢谢

解决方案 »

  1.   

    把@sql的内容复制出来不就ok了?
      

  2.   


    --一起执行
    exec usp_ReplaceSourceText
    update a set a.source_text=b.new from aa_transdb a,t1 b where a.source_text=b.old;
    update a set a.source_text=b.new from ab_transdb a,t1 b where a.source_text=b.old;
      

  3.   


    alter procedure usp_ReplaceSourceText
    (
        @t1 nvarchar(max)
    )
    as
    declare @sql nvarchar(max), @t2 nvarchar(max)
    set @t2 = N''begin
        set @sql='select @t2 = @t2 + ''update a
        set a.source_text=b.new
        from ''+name+'' a,'+@t1+' b
        where a.source_text=b.old;'' from sys.tables where name like ''%_transdb'''
       exec sp_executesql @sql, N'@t2 nvarchar(max) output', @t2 output
        exec (@t2)
    end