我有一个复杂的视图,它返回一组记录。我需要做的就是“人为地”膨胀这个数字(调查只出现高数据量的问题),我认为我可以简单地UNION ALL查询自己。这在理论上有效:
SELECT column1, column2, column3, .... FROM <Table> WHERE <Some Condition>
UNION ALL
SELECT column1, column2, column3, .... FROM <Table> WHERE <Some Condition>
UNION ALL
SELECT column1, column2, column3, .... FROM <Table> WHERE <Some Condition>
UNION ALL ... ...
我不想重复这个联合1000次,有没有更好的方法让我递归地执行此操作,或者在while循环中使用所有union语句?不幸的是我无法创建存储过程,它可以通过直接在SSMS中运行查询来完成。
我确实尝试过这样的事情:
Declare @ctr as integer;
Set @Ctr = 0;
While @Ctr < 10
BEGIN
With cte as (Select ......)
select * from cte
UNION ALL
select * from cte
set @Ctr = @ctr + 1
这对工会做了,但它确实给了我10个不同的结果,每个工会完成了10次。
我正在使用SQL Server 2012。
先谢谢
答案 0 :(得分:1)
如果我理解正确,你只需要重复数据1000次,就可以这样做
SELECT t.*
FROM
(SELECT column1, column2, column3, .... FROM <Table> WHERE <Some Condition>) t
cross apply (select top 1000 * from master..spt_values) a -- you could use any table with 1000+ records, or any number tables
答案 1 :(得分:0)
以下是我试图解决的问题。
DECLARE @Tables TABLE(TableName NVARCHAR(100))
INSERT INTO @Tables SELECT 'MyHardCodedTable1'
INSERT INTO @Tables SELECT 'MyHardCodedTable2'
INSERT INTO @Tables SELECT SO.Name FROM SYSOBJECTS SO WHERE SO.XType = 'U'
DECLARE @TableName NVARCHAR(100)
DECLARE X CURSOR FOR SELECT TableName FROM @Tables
OPEN X
FETCH NEXT FROM X INTO @TableName
WHILE(@@FETCH_STATUS=0) BEGIN
PRINT 'SELECT column1, column2, column3, .... FROM '+@TableName+' WHERE <Some Condition>' +CHAR(10)+CHAR(13) + 'UNION ALL '+CHAR(10)
FETCH NEXT FROM X INTO @TableName
END
CLOSE X
DEALLOCATE X