【问题标题】:What do the quotes mean in this SQL Server snippet? (SELECT '' column1, '' column 2)此 SQL Server 代码段中的引号是什么意思? (选择''第1列,''第2列)
【发布时间】:2020-11-18 13:47:28
【问题描述】:

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

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

【问题讨论】:

  • '' column1 表示别名为“column1”的空字符串
  • 使用 NULL 而不是 ' ' 空白字符串。 Null 不会占用任何内存

标签: sql sql-server syntax


【解决方案1】:

表达式'' 是一个空字符串。所以这是使用空字符串而不是 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';

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-03-02
    • 1970-01-01
    • 2014-06-15
    • 2011-04-02
    • 2012-02-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多