如何使返回的DataSet中包含3个tableTable1
id  name  country 
1    n1      001
2    n2      001
3    n3      002
4    n4      002
5    n5      003
6    n6      003select * from Table1 where...返回的DataSet中包含3个Table (按照country来分)

解决方案 »

  1.   

    select * from Table1 where 国家=国家编号select * from Table1 where 国家=国家编号select * from Table1 where 国家=国家编号执行三次   第一个表示table[0] 第二个是table[1]类推
      

  2.   

    真实的情况不一定有多少个country值
      

  3.   

    if OBJECT_ID('tempdb..#temp', 'u') is not null   drop table #temp;
    go
    create table #temp( [id] varchar(100), [name] varchar(100), [country] varchar(100));
    insert #temp
    select '1','n1','001' union all
    select '2','n2','001' union all
    select '3','n3','002' union all
    select '4','n4','002' union all
    select '5','n5','003' union all
    select '6','n6','003' --SQL:
    DECLARE @tablename NVARCHAR(255), @sql NVARCHAR(MAX)

    DECLARE cursor_table CURSOR STATIC LOCAL FORWARD_ONLY READ_ONLY
    FOR
    SELECT DISTINCT country
    FROM #tempOPEN cursor_table
    FETCH NEXT FROM cursor_table INTO @tablenameWHILE (@@FETCH_STATUS = 0) 
    BEGIN
        SET @sql = N'select * from ' + QUOTENAME(@tablename) + ' {0}'
    SET @sql = REPLACE(@sql, '{0}', 'where 1 = 1') EXEC(@sql)
    FETCH NEXT FROM cursor_table INTO @tablename
    ENDCLOSE cursor_table
    DEALLOCATE cursor_table
      

  4.   

    SQL 语句中写几个 select 就会出来几个 table。
      

  5.   

    ++
    你想要几个table  就select几次就是了