【问题标题】:Insert hashbytes Incorrect syntax near 'MD5'在“MD5”附近插入哈希字节语法不正确
【发布时间】:2019-09-26 09:34:36
【问题描述】:

我正在使用 SQL Server 2014。作为一项更大任务的一部分,我需要从表中检测更新等,因此我正在实现 Hashbytes。 Hasbytes 字段定义为varbinary(MAX)。

这是我的 SQL:

INSERT INTO tbl_People
  SELECT
    id,
    Name,
    Add1,
    Add2,
    Add3,
    HashValue as Hashbytes('MD5',CONCAT('|',Name, Add1, Add2, Add3))
from tbl_PeopleSource

但是,我一直收到错误消息:

“MD5”附近的语法不正确。

我哪里错了?

【问题讨论】:

  • 我是SELECT ID, .., Hashbytes('MD5',CONCAT('|',Name, Add1, Add2, Add3)) AS HashByte。

标签: sql sql-server hashbytes


【解决方案1】:

在 HashValue 别名之前缺少一个逗号,以便将 Add3 与下一列分隔开。别名也是倒过来的,应该是<expression> as AliasName。

Add3, -- Here comma
Hashbytes('MD5',CONCAT('|',Name, Add1, Add2, Add3)) as HashValue -- Inverted alias

另外,最好列出插入表的列,这样就不会错误地解释顺序:

INSERT INTO tbl_People (
    ID,
    Name,
    Col1,
    Col2,
    Col3,
    HashValue)
SELECT
    ...

在 SQL Server 上有multiple ways 可以使用列别名。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-12-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-08
    • 1970-01-01
    • 2015-12-12
    相关资源
    最近更新 更多