0

I wrote a trigger in the development environment which runs SQL Server 2008. I then tried to execute it in production, which runs SQL Server 2005.

I'm getting the following error:

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

The query looks like this:

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: This is the part of the trigger where most of the variables are declared/set:

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

I assume it's because of the 2005 vs 2008 differences, but can't figure out what exactly is wrong.

4
  • 8
    Can you share the whole trg_issues_update ? Commented Nov 18, 2013 at 20:51
  • 1
    Ya - we'll need to see the trigger to help you, since that's where the error is Commented Nov 18, 2013 at 21:00
  • 3
    The issue is with this line VALUES (1, @userid, CURRENT_TIMESTAMP), (3, @userid, CURRENT_TIMESTAMP) 2005 doesn't support the VALUES(etc), (etc) For that line, use either multiple INSERT statements or SELECT with UNION ALL Commented Nov 18, 2013 at 21:14
  • Thanks @bluefeet - I re-wrote that part and the query executed successfully. Feel free to post an answer, I will accept it. Line 97 from the error message was that INSERT query I posted originally, oh well. Commented Nov 18, 2013 at 21:17

2 Answers 2

4

The issue is with the line:

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

From MSDN:

SQL Server 2008 introduces the Transact-SQL row constructor (also called a table value constructor) to specify multiple rows in a single INSERT statement. The row constructor consists of a single VALUES clause with multiple value lists enclosed in parentheses and separated by a comma.

In order to fix this so it will work in both SQL 2008 and SQL Server 2005, you will want to rewrite the query to:

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

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

Or you can use INSERT INTO... SELECT...UNION ALL:

INSERT INTO globalgroupmembership (globalgroupid, userid, created)
SELECT 1, @userid, CURRENT_TIMESTAMP
UNION ALL
SELECT 3, @userid, CURRENT_TIMESTAMP;
Sign up to request clarification or add additional context in comments.

Comments

-2

try to concatinate the comment before the value list:

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'

2 Comments

It wasn't me, but I guess it's because 2005 supports expressions in the values part of an insert.
I tried this approach, but it didn't work unfortunately (it wasn't me who downvoted).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.