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 有什么好写法没?

解决方案 »

  1.   

    语句长度 过长,所以需要 循环 执行,分开写入临时表。这里用的是 MSSQL2000。问题所在,如何简写 这个循环执行 ?
      

  2.   

    可以拼接SQL
    例如
    declare @Str1 varchar(1000)
    declare @Str2 varchar(5000)
    exec (@Str1 + @Str2)
      

  3.   

    declare @C NVARCHAR(4000)
    SELECT @C = @C +'SELECT A1 FROM A' FROM Z4000长度不够
      

  4.   

    2005
    declare @c nvarchar(max)
      

  5.   

    楼上的乱回复 误人子弟
    人都说了SQL2000  还整一2005跟人说楼主我这以前有做过一个类似的功能(查询数据库中有数据的表.并把表名及数据量记录下来).改改应该可以用
    /********查找...有數據的表********/
    if object_id('TEMPDB..#temp') is not null drop table #temp
    if object_id('_temp','U') is not null drop table _temp
    go
    select 'insert into _temp/*(tb_name,row_count)*/ select '''+name+''',count(*) as a from '+name as name
    into #temp
    from 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 #temp
    open cur_1 fetch from cur_1 into @sql
    while @@fetch_status=0 
    begin
    exec sp_executesql @sql 
    fetch from cur_1 into @sql
    end 
    close cur_1 deallocate cur_1
    if object_id('TEMPDB..#temp') is not null drop table #temp
    --if object_id('_temp','U') is not null drop table _temp
    select * from _temp
    go