以循环方式重用表数据

时间:2014-05-27 10:15:11

标签: tsql sql-server-2005

让我们说我有一些数据我想重复N次。一个天真的方法是:

  IF OBJECT_ID('dbo.Data', 'U') IS NOT NULL
      DROP TABLE dbo.Data

    CREATE TABLE Data
    (
        DataId INT NOT NULL PRIMARY KEY,
        DataValue NVARCHAR(MAX) NOT NULL
    )

    INSERT INTO Data (DataId, DataValue)
        SELECT 1, 'Value1' UNION ALL
        SELECT 2, 'Value2' UNION ALL
        SELECT 3, 'Value3' UNION ALL
        SELECT 4, 'Value4' UNION ALL
        SELECT 5, 'Value5' 

    DECLARE @RowsRequired INT
    DECLARE @Counter INT
    DECLARE @NumberOfRows INT

    SET @RowsRequired = 22

    IF OBJECT_ID('tempdb..#TempData') IS NOT NULL DROP TABLE #TempData
    CREATE TABLE #TempData
    (
        Id INT IDENTITY(1,1),
        DataValue NVARCHAR(MAX)
    )

    SELECT @NumberOfRows = COUNT(*) FROM Data
    SET @Counter = 1

    WHILE @RowsRequired > 0
    BEGIN

        INSERT INTO #TempData
        SELECT DataValue FROM Data WHERE DataId = @Counter

        SET @Counter = @Counter + 1
        SET @RowsRequired = @RowsRequired - 1
        IF(@Counter > @NumberOfRows)
            BEGIN
                SET @Counter = 1
            END
    END

    SELECT * FROM #TempData

这里@RowsRequired确定需要多少行。这可以用基于集合的形式重新表述吗?感谢。

以下是带有代码的SQLFiddle

2 个答案:

答案 0 :(得分:1)

请改为尝试:

DECLARE @RowsRequired INT = 22

;WITH CTE AS
(
  SELECT DataId, DataValue, ROW_NUMBER() over (PARTITION BY DataId ORDER BY DataId) sort
  FROM DATA
  CROSS JOIN
  (
    SELECT TOP (@RowsRequired) 0 d
    FROM master..spt_values
  ) d
)
SELECT TOP (@RowsRequired) ROW_NUMBER() over (order by sort),  DataValue
FROM CTE
ORDER BY sort, 1

答案 1 :(得分:1)

我试过这个并为我工作。

declare @requiredrows int

set @requiredrows = 22;

declare @foreachrow int
select @foreachrow = @requiredrows / Count(*) from Data;

select top (@requiredrows) * from
(
select *, ROW_NUMBER() over(partition by dataId order by number) rno
from Data
Cross Join master..spt_values
) A
where rno <= @foreachrow + 1

希望它会有所帮助。