2008年编写的SQL查询在2005年不起作用

时间:2013-11-18 20:49:29

标签: sql sql-server syntax

我在运行SQL Server 2008的开发环境中编写了一个触发器。然后我尝试在生产中执行它,它运行SQL Server 2005

我收到以下错误:

Msg 102, Level 15, State 1, Procedure trg_issues_update, Line 97
Incorrect syntax near ','.

查询如下所示:

INSERT INTO users (username, firstname, surname, pwd, emailaddress, resetpwd, comment,  apikey, active)
VALUES (@username, @first_name, @last_name, @password, @email, NULL, 'Comment: ' + CAST(@issueid AS VARCHAR), 'None', '1');

UPDATE :这是触发器中声明/设置大多数变量的部分:

IF (SELECT projectid FROM inserted) = 55 AND (SELECT issuestatusid FROM inserted) = 28 BEGIN
    DECLARE @full_name nvarchar (255)
    SET @full_name = (SELECT fielddata FROM customfielddata WHERE issueid = @issueid AND customfieldid = 84)

    DECLARE @first_name nvarchar (255)
    SET @first_name = SUBSTRING(@full_name, 1, CHARINDEX(' ', @full_name) - 1)

    DECLARE @last_name nvarchar (255)
    SET @last_name = SUBSTRING(@full_name, CHARINDEX(' ', @full_name) + 1, LEN(@full_name) + 1)

    DECLARE @email nvarchar (255)
    SET @email = (SELECT fielddata FROM customfielddata WHERE issueid = @issueid AND customfieldid = 85)

    DECLARE @location nvarchar (255)
    DECLARE @domain nvarchar (255)
    DECLARE @username nvarchar (255)
    SET @location = (SELECT fielddata FROM customfielddata WHERE issueid = @issueid AND customfieldid = 86)
    SET @domain = (SELECT fielddata FROM customfielddata WHERE issueid = @issueid AND customfieldid = 87)
    IF @location = 'Canada' BEGIN SET @username = 'ORG1\' + UPPER(@domain) END
    ELSE SET @username = 'ORG2\' + UPPER(@domain)

    DECLARE @password binary (16)
    SET @password = CONVERT(BINARY(16), HashBytes('MD5', ''), 2)

    DECLARE @comment nvarchar (500)
    SET @comment = 'APPR-' + CAST(@issueid AS VARCHAR)

    INSERT INTO users (username, firstname, surname, pwd, emailaddress, resetpwd, comment, apikey, active)
    VALUES (@username, @first_name, @last_name, @password, @email, NULL, @comment, 'None', '1');

    DECLARE @userid NUMERIC(10,0)
    SET @userid = (SELECT userid FROM users WHERE username = @username)

    INSERT INTO globalgroupmembership (globalgroupid, userid, created)
    VALUES (1, @userid, CURRENT_TIMESTAMP), (3, @userid, CURRENT_TIMESTAMP)

END

我认为这是因为2005年与2008年的差异,但无法弄清楚到底出了什么问题。

2 个答案:

答案 0 :(得分:4)

问题在于:

INSERT INTO globalgroupmembership (globalgroupid, userid, created)
VALUES (1, @userid, CURRENT_TIMESTAMP), (3, @userid, CURRENT_TIMESTAMP)

来自MSDN

  

SQL Server 2008引入了Transact-SQL行构造函数(也称为表值构造函数),以在单个INSERT语句中指定多个行。行构造函数由单个VALUES子句组成,其中多个值列表括在括号中并用逗号分隔。

为了解决这个问题,它可以在SQL 2008和SQL Server 2005中运行,您需要将查询重写为:

INSERT INTO globalgroupmembership (globalgroupid, userid, created)
VALUES (1, @userid, CURRENT_TIMESTAMP);

INSERT INTO globalgroupmembership (globalgroupid, userid, created)
VALUES (3, @userid, CURRENT_TIMESTAMP)

或者您可以使用INSERT INTO... SELECT...UNION ALL

INSERT INTO globalgroupmembership (globalgroupid, userid, created)
SELECT 1, @userid, CURRENT_TIMESTAMP
UNION ALL
SELECT 3, @userid, CURRENT_TIMESTAMP;

答案 1 :(得分:-2)

尝试在值列表之前连接注释:

declare @comment varchar
set @comment = 'Comment: ' + cast(@issueid as varchar)

INSERT INTO users (username, firstname, surname, pwd, emailaddress, resetpwd, comment,  apikey, active)
select @username, @first_name, @last_name, @password, @email, NULL, @comment, 'None', '1'