SQL Server:使用INSERT INTO

时间:2018-05-21 09:39:10

标签: sql-server-2008

我想对于那些了解SQL编程的人来说,这将是一个非常简单的...

SQLFiddle

以下是我对数据库执行的代码:

DECLARE @maxCounter int
-- Used to get the maximum bound number for my loop, basically what is the highest number of records. Tested, seems to work as expected.
SET @maxCounter = (SELECT TOP 1 COUNT(SN)
                   FROM TestResults 
                   WHERE Type = 'EX'
                   GROUP BY SN
                   ORDER BY COUNT(SN) DESC)

CREATE TABLE #Info
(
    DLoc VARCHAR(500),
    DCode VARCHAR(500),
    Dobs VARCHAR(500)
)

DECLARE @counter INT
SET @counter = 1

WHILE @counter <= @maxCounter 
BEGIN
    INSERT INTO #Info (DLoc) 
    VALUES ('Location_' + CAST(@counter AS VARCHAR(16)))

    INSERT INTO #Info (DCode) 
    VALUES ('Code_' + CAST(@counter AS VARCHAR(16)))

    INSERT INTO #Info (Dobs) 
    VALUES ('Observation_' + CAST(@counter AS VARCHAR(16)))

    SET @counter = @counter + 1
END

SELECT * FROM #Info;

DROP TABLE #Info;

如果你在代码中看到一些奇怪的东西,那就是因为我是一个初学者并且不知道更好。

while循环的预期输出:

+------------+---------+---------------+
| defLoc     | defCode | obs           |
+------------+---------+---------------+
| Location_1 | Code_1  | Observation_1 |
| Location_2 | Code_2  | Observation_2 |
| Location_3 | Code_3  | Observation_3 |
+------------+---------+---------------+

意外的输出结果:

defLoc           | defCode     | obs           |
-----------------+-------------+---------------|
Location_1       |             |               |
                 | Code_1      |               |
                 |             | Observation_1 |
Location_2       |             |               |
                 | Code_2      |               |
                 |             | Observation_2 |
Location_3       |             |               |
                 | Code_3      |               |
                 |             | Observation_3 |

我不知道空单元格来自哪里......

2 个答案:

答案 0 :(得分:1)

每次迭代都需要使用一个 INSERT,并且一次性指定所有三列及其值:

WHILE @counter <= @maxCounter 
BEGIN
    INSERT INTO #Info (DLoc, DCode, Dobs) 
    VALUES ('Location_' + CAST(@counter AS VARCHAR(16)),  
            'Code_' + CAST(@counter AS VARCHAR(16)),
            'Observation_' + CAST(@counter AS VARCHAR(16))
           )

    SET @counter = @counter + 1
END

每个INSERT都会插入整行 - 您提供的值会插入到您指定的列中,但其他列将全部保留{{ 1}}。

答案 1 :(得分:0)

您可以在循环中插入具有3个不同列值的每一行和一个insert语句:

WHILE @counter <= @maxCounter
BEGIN
    INSERT INTO #Info (DLoc,DCode,Dobs)
    VALUES (
          'Location_' + CAST(@counter AS VARCHAR(16))
        , 'Code_' + CAST(@counter AS VARCHAR(16))
        , 'Observation_' + CAST(@counter AS VARCHAR(16))
        );
    SET @counter = @counter + 1;
END;

或者,您可以使用计数表作为源,在单个INSERT...SELECT语句中一次插入所有行。如果没有计数表,则可以使用CTE生成数字集。以下示例使用CTEROW_NUMBER(),可根据需要进行扩展。

WITH 
     t10 AS (SELECT n FROM (VALUES(0),(0),(0),(0),(0),(0),(0),(0),(0),(0)) t(n))
    ,t1k AS (SELECT 0 AS n FROM t10 AS a CROSS JOIN t10 AS b CROSS JOIN t10 AS c)
    ,t1m AS (SELECT ROW_NUMBER() OVER (ORDER BY (SELECT 0)) AS num FROM t1k AS a CROSS JOIN t1k AS b CROSS JOIN t1k AS c)
INSERT INTO #Info (DLoc,DCode,Dobs)
SELECT
          'Location_' + CAST(num AS VARCHAR(16))
        , 'Code_' + CAST(num AS VARCHAR(16))
        , 'Observation_' + CAST(num AS VARCHAR(16))
FROM t1m
WHERE num <= @maxCounter;

在SQL Server中,基于集合的操作通常比数量级的程序循环结构更优越。