此 SQL 服务器代码段中的引号是什么意思? (SELECT '' column1, '' column 2)

What do the quotes mean in this SQL Server snippet? (SELECT '' column1, '' column 2)

我想知道每列前面的两个单引号的用途。这是在存储过程中。感谢您的帮助!

INSERT INTO #temptable1
SELECT DISTINCT '' column1
    ,column2
    ,'' column3
    ,'' column4     
FROM table1
WHERE column1 NOT LIKE 'string1%'
    AND column2 <> 'string2'
    AND column3 <> 'string3'

表达式''是一个空字符串。所以这是使用空字符串而不是 NULL 来表示“缺失”值。

我会把这段代码写成:

SELECT DISTINCT '' as column1, column2, '' as column3, '' as column4

这可能会更清楚。我总是使用 as 作为列别名——这样更容易发现缺少的逗号(对于列别名,应该始终有一个 as)。

更常见的是,这可能只使用默认值(通常是 NULL,但也可能是 ''):

INSERT INTO #temptable1 (column2)
    SELECT DISTINCT column2
    FROM table1
    WHERE column1 NOT LIKE 'string1%'
        AND column2 <> 'string2'
        AND column3 <> 'string3';