批量执行,高手来。
declare @TB (TBSQL nvarchar(4000))
INSERT INTO @TB
SELECT 'SELECT A1 FROM A' FROM Z
如 100行
declare @TB2 (A1 NVARCHAR(100))
需求:
MSSQL 2000
执行@TB 表里100行语句,并将内容写入 @TB2
有什么好写法没?
[解决办法]
将第一个变量表里的字段拼接为动态执行的字符串,执行就可以,将内容写入@tb2,这里的内容指?
[解决办法]
/********查找...有數據的表********/if object_id('TEMPDB..#temp') is not null drop table #tempif object_id('_temp','U') is not null drop table _tempgoselect 'insert into _temp/*(tb_name,row_count)*/ select '''+name+''',count(*) as a from '+name as nameinto #tempfrom sysobjects where xtype = 'U' order by namecreate table _temp (tb_name varchar(50),row_count decimal(10,0))declare @sql nchar(200)declare cur_1 cursor for select name from #tempopen cur_1 fetch from cur_1 into @sqlwhile @@fetch_status=0 begin exec sp_executesql @sql fetch from cur_1 into @sqlend close cur_1 deallocate cur_1if object_id('TEMPDB..#temp') is not null drop table #temp--if object_id('_temp','U') is not null drop table _tempselect * from _tempgo