当前位置: 代码迷 >> Sql Server >> 批量执行,高手来。解决思路
  详细解决方案

批量执行,高手来。解决思路

热度:73   发布时间:2016-04-27 13:08:29.0
批量执行,高手来。
declare @TB (TBSQL nvarchar(4000))

INSERT INTO @TB
SELECT 'SELECT A1 FROM A' FROM Z

如 100行



declare @TB2 (A1 NVARCHAR(100))

需求:
MSSQL 2000 
[email protected] 表里100行语句,并将内容写入 @TB2 

有什么好写法没?



------解决方案--------------------
将第一个变量表里的字段拼接为动态执行的字符串,执行就可以,[email protected],这里的内容指?
------解决方案--------------------
探讨
语句长度 过长,所以需要 循环 执行,分开写入临时表。

这里用的是 MSSQL2000。

问题所在,如何简写 这个循环执行 ?

------解决方案--------------------
探讨

declare @C NVARCHAR(4000)
SELECT @C = @C +'SELECT A1 FROM A' FROM Z

4000长度不够

------解决方案--------------------
楼上的乱回复 误人子弟
人都说了SQL2000 还整一2005跟人说

楼主我这以前有做过一个类似的功能(查询数据库中有数据的表.并把表名及数据量记录下来).改改应该可以用
SQL code
/********查找...有數據的表********/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
  相关解决方案