--创建测试表 这里需要问一下你后面你筛选的name 和no 的列名有1,2,3吗?
CREATE TABLE #T
(
name VARCHAR(20) ,
no VARCHAR(10)
)
DECLARE @name VARCHAR(20)= ''
DECLARE @str NVARCHAR(100)
--创建游标
DECLARE cursor_tb CURSOR FAST_FORWARD
FOR
( SELECT tablename
FROM tablemain
)
OPEN cursor_tb
FETCH NEXT FROM cursor_tb
INTO @name
WHILE @@FETCH_STATUS = 0
BEGIN
SELECT @str = 'select name,no from '
+ QUOTENAME(@name)
INSERT INTO #T EXEC(@sql);
FETCH NEXT FROM cursor_tb
INTO @name
END
CLOSE cursor_tb
DEALLOCATE cursor_tb
SELECT *
FROM #T
--删除临时表
DROP TABLE #T
什么意思?不太明白,查询出start的表比如说有两个,程序中拼sql
select * from table union all select * from table2
这样?不过你这种设计数据库也是醉了?为什么这设计