SQL Server:将多个insert语句合并为一个

时间:2018-03-13 10:30:55

标签: sql sql-server

我需要帮助将这些单个SQL插入语句合并为一个。这是我的代码,作为单独的陈述:

        INSERT INTO [DBO].[QUEST] ([QUEST_ID], [QUEST_TEXT], [DOM_ID], [CRT_USR], [CRT_DATE], [EXAM_ID], [RANDOMNUMBER]) SELECT TOP 24 [QUEST_ID], [QUEST_TEXT], [DOM_ID], @USR_NAME, @CRT_DATE, [EXAM_ID], ABS(CAST(CAST(NEWID() AS VARBINARY) AS INT)) AS [RANDOMNUMBER] FROM [DBO].[QUEST] WHERE [DOM_ID] = 'DOM1' AND EXAM_ID = @EXAM_ID ORDER BY [RANDOMNUMBER]

        INSERT INTO [DBO].[QUEST] ([QUEST_ID], [QUEST_TEXT], [DOM_ID], [CRT_USR], [CRT_DATE], [EXAM_ID], [RANDOMNUMBER]) SELECT TOP 10 [QUEST_ID], [QUEST_TEXT], [DOM_ID], @USR_NAME, @CRT_DATE, [EXAM_ID], ABS(CAST(CAST(NEWID() AS VARBINARY) AS INT)) AS [RANDOMNUMBER] FROM [DBO].[QUEST] WHERE [DOM_ID] = 'DOM2' AND EXAM_ID = @EXAM_ID ORDER BY [RANDOMNUMBER]

        INSERT INTO [DBO].[QUEST] ([QUEST_ID], [QUEST_TEXT], [DOM_ID], [CRT_USR], [CRT_DATE], [EXAM_ID], [RANDOMNUMBER]) SELECT TOP 15 [QUEST_ID], [QUEST_TEXT], [DOM_ID], @USR_NAME, @CRT_DATE, [EXAM_ID], ABS(CAST(CAST(NEWID() AS VARBINARY) AS INT)) AS [RANDOMNUMBER] FROM [DBO].[QUEST] WHERE [DOM_ID] = 'DOM3' AND EXAM_ID = @EXAM_ID ORDER BY [RANDOMNUMBER]

        INSERT INTO [DBO].[QUEST] ([QUEST_ID], [QUEST_TEXT], [DOM_ID], [CRT_USR], [CRT_DATE], [EXAM_ID], [RANDOMNUMBER]) SELECT TOP 8 [QUEST_ID], [QUEST_TEXT], [DOM_ID], @USR_NAME, @CRT_DATE, [EXAM_ID], ABS(CAST(CAST(NEWID() AS VARBINARY) AS INT)) AS [RANDOMNUMBER] FROM [DBO].[QUEST] WHERE [DOM_ID] = 'DOM4' AND EXAM_ID = @EXAM_ID ORDER BY [RANDOMNUMBER]

如何将这些陈述合并为一个陈述?

5 个答案:

答案 0 :(得分:2)

使用UNION ALL

    INSERT INTO [DBO].[QUEST] ([QUEST_ID], [QUEST_TEXT], [DOM_ID], [CRT_USR], [CRT_DATE], [EXAM_ID], [RANDOMNUMBER])
    SELECT TOP 24 [QUEST_ID], [QUEST_TEXT], [DOM_ID], @USR_NAME, @CRT_DATE, [EXAM_ID], ABS(CAST(CAST(NEWID() AS VARBINARY) AS INT)) AS [RANDOMNUMBER] FROM [DBO].[QUEST] WHERE [DOM_ID] = 'DOM1' AND EXAM_ID = @EXAM_ID 
    UNION ALL
    SELECT TOP 24 [QUEST_ID], [QUEST_TEXT], [DOM_ID], @USR_NAME, @CRT_DATE, [EXAM_ID], ABS(CAST(CAST(NEWID() AS VARBINARY) AS INT)) AS [RANDOMNUMBER] FROM [DBO].[QUEST] WHERE [DOM_ID] = 'DOM2' AND EXAM_ID = @EXAM_ID 
    UNION ALL 
    SELECT TOP 24 [QUEST_ID], [QUEST_TEXT], [DOM_ID], @USR_NAME, @CRT_DATE, [EXAM_ID], ABS(CAST(CAST(NEWID() AS VARBINARY) AS INT)) AS [RANDOMNUMBER] FROM [DBO].[QUEST] WHERE [DOM_ID] = 'DOM3' AND EXAM_ID = @EXAM_ID 
    UNION ALL
    SELECT TOP 24 [QUEST_ID], [QUEST_TEXT], [DOM_ID], @USR_NAME, @CRT_DATE, [EXAM_ID], ABS(CAST(CAST(NEWID() AS VARBINARY) AS INT)) AS [RANDOMNUMBER] FROM [DBO].[QUEST] WHERE [DOM_ID] = 'DOM4' AND EXAM_ID = @EXAM_ID 

答案 1 :(得分:2)

我迟到了,但我可以提出另一种方法来实现这一目标。

;WITH MT AS 
(
 SELECT * FROM (VALUES 
    ('DOM1', 24),('DOM2', 10),('DOM3', 15),('DOM4', 8)
    )T(DOM_ID ,[Count])
)

INSERT INTO [DBO].[QUEST] (COLUME1, COLUM2...)
SELECT COLUME1, COLUM2... FROM
(
  SELECT *, ROW_NUMBER() OVER (PARTITION BY DOM_ID ORDER BY [RANDOMNUMBER]) RN
  FROM [DBO].[QUEST] 
) TBL
INNER JOIN MT MT ON TBL.DOM_ID = MT.DOM_ID 
WHERE TBL.RN <=MT.[Count]

或者简单地跟随。

INSERT INTO [DBO].[QUEST] (COLUME1, COLUM2...)
SELECT COLUME1, COLUM2... FROM
(
  SELECT *, ROW_NUMBER() OVER (PARTITION BY DOM_ID ORDER BY [RANDOMNUMBER]) RN  FROM [DBO].[QUEST] 
) TBL
INNER JOIN
(
  SELECT * FROM (VALUES ('DOM1',24),('DOM2',10),('DOM3',15),('DOM4',8)) T(DOM_ID,[Count])
) MT ON TBL.DOM_ID = MT.DOM_ID 
WHERE TBL.RN <=MT.[Count]

您可以在http://rextester.com/CECHM83488

上查看演示实施

答案 2 :(得分:1)

以下是一种方法,使用ROW_NUMBER()TOP WITH TIES

INSERT INTO [DBO].[QUEST] ([QUEST_ID], [QUEST_TEXT], [DOM_ID], [CRT_USR], [CRT_DATE], [EXAM_ID], [RANDOMNUMBER])
    SELECT TOP (24) WITH TIES [QUEST_ID], [QUEST_TEXT], [DOM_ID], @USR_NAME, @CRT_DATE, [EXAM_ID], ABS(CAST(CAST(NEWID() AS VARBINARY) AS INT)) AS [RANDOMNUMBER]
    FROM [DBO].[QUEST]
    WHERE [DOM_ID] IN ('DOM1', 'DOM2', 'DOM3', 'DOM4') AND
          EXAM_ID = @EXAM_ID
    ORDER BY ROW_NUMBER() OVER (PARTITION BY DOM_ID, RANDOMNUMBER);

QUEST中包含随机数似乎很奇怪。没有数字,这也可以。

另请注意,WHERE上的DOM_ID条件不是必需的,因此这可以适用于任意数量,但不会明确列出。

答案 3 :(得分:0)

您可以使用WHILE循环动态地将DOM从1调整到n。它应包括以下内容。 (显然,这需要加强)

DECLARE @Var1 VARCHAR (10)
SET @Var1 = 'DOM'

DECLARE @Var2 INT 
SET @Var2 = 1

SELECT @Var1 + CAST (@Var2 AS VARCHAR)  -- that would be your DOM_ID

答案 4 :(得分:0)

按Dom_ID排序,Randomnumber:

INSERT INTO [DBO].[QUEST] ([QUEST_ID], [QUEST_TEXT], [DOM_ID], [CRT_USR], [CRT_DATE], [EXAM_ID], [RANDOMNUMBER]) 
SELECT TOP 24 [QUEST_ID], [QUEST_TEXT], [DOM_ID], @USR_NAME, @CRT_DATE, [EXAM_ID], ABS(CAST(CAST(NEWID() AS VARBINARY) AS INT)) AS [RANDOMNUMBER] 
FROM [DBO].[QUEST] 
WHERE [DOM_ID] = 'DOM1' AND EXAM_ID = @EXAM_ID 
UNION ALL
SELECT TOP 10 [QUEST_ID], [QUEST_TEXT], [DOM_ID], @USR_NAME, @CRT_DATE, [EXAM_ID], ABS(CAST(CAST(NEWID() AS VARBINARY) AS INT)) AS [RANDOMNUMBER] 
FROM [DBO].[QUEST] 
WHERE [DOM_ID] = 'DOM2' AND EXAM_ID = @EXAM_ID 
UNION ALL
SELECT TOP 15 [QUEST_ID], [QUEST_TEXT], [DOM_ID], @USR_NAME, @CRT_DATE, [EXAM_ID], ABS(CAST(CAST(NEWID() AS VARBINARY) AS INT)) AS [RANDOMNUMBER] 
FROM [DBO].[QUEST] 
WHERE [DOM_ID] = 'DOM3' AND EXAM_ID = @EXAM_ID 
UNION ALL
SELECT TOP 8 [QUEST_ID], [QUEST_TEXT], [DOM_ID], @USR_NAME, @CRT_DATE, [EXAM_ID], ABS(CAST(CAST(NEWID() AS VARBINARY) AS INT)) AS [RANDOMNUMBER] 
FROM [DBO].[QUEST] 
WHERE [DOM_ID] = 'DOM4' AND EXAM_ID = @EXAM_ID 
ORDER BY DOM_ID, [RANDOMNUMBER]