如何使返回的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来分)
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来分)
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
你想要几个table 就select几次就是了