locked
UPDATE Statement RRS feed

  • Question

  • Hi all when i'm trying to execute the following statement i'm getting an error "incorrect syntax near GO" can anyone please tell me what is wrong?

    UPDATE       dbo.STG_SPACE
    SET                DateID = ID
    FROM            dbo.DIM_TIME
    WHERE        Date = PK_Date GO
                                 UPDATE       STG_SPACE
                                  SET                 DateID = - 1
                                  WHERE        DateID IS NULL
     GO

    Thanks

    Monday, January 28, 2013 6:45 PM

Answers

  • UPDATE dbo.STG_SPACE
    SET DateID = ID
    FROM dbo.DIM_TIME
    WHERE [DATE] = PK_Date 
    GO
    UPDATE STG_SPACE
    SET DateID = - 1
    WHERE DateID IS NULL

    Looks like you needed a carriage return after your first update.  See if this works, or give you a more accurate error message

    Chuck Pedretti | Magenic – North Region | magenic.com

    • Proposed as answer by sambeet Monday, January 28, 2013 7:02 PM
    • Marked as answer by Iric Wen Wednesday, February 6, 2013 7:57 AM
    Monday, January 28, 2013 6:50 PM
  • Here is the explaination for why your code didn't work:

    http://msdn.microsoft.com/en-us/library/ms188037.aspx

    A Transact-SQL statement cannot occupy the same line as a GO command. However, the line can contain comments.


    Chuck Pedretti | Magenic – North Region | magenic.com

    • Proposed as answer by sambeet Monday, January 28, 2013 7:02 PM
    • Marked as answer by Iric Wen Wednesday, February 6, 2013 7:57 AM
    Monday, January 28, 2013 6:58 PM

All replies

  • UPDATE dbo.STG_SPACE
    SET DateID = ID
    FROM dbo.DIM_TIME
    WHERE [DATE] = PK_Date 
    GO
    UPDATE STG_SPACE
    SET DateID = - 1
    WHERE DateID IS NULL

    Looks like you needed a carriage return after your first update.  See if this works, or give you a more accurate error message

    Chuck Pedretti | Magenic – North Region | magenic.com

    • Proposed as answer by sambeet Monday, January 28, 2013 7:02 PM
    • Marked as answer by Iric Wen Wednesday, February 6, 2013 7:57 AM
    Monday, January 28, 2013 6:50 PM
  • Here is the explaination for why your code didn't work:

    http://msdn.microsoft.com/en-us/library/ms188037.aspx

    A Transact-SQL statement cannot occupy the same line as a GO command. However, the line can contain comments.


    Chuck Pedretti | Magenic – North Region | magenic.com

    • Proposed as answer by sambeet Monday, January 28, 2013 7:02 PM
    • Marked as answer by Iric Wen Wednesday, February 6, 2013 7:57 AM
    Monday, January 28, 2013 6:58 PM
  • ;WITH mycte
         AS (SELECT  SS.DateID, dt.ID
             FROM   dbo.STG_SPACE ss
                    LEFT JOIN dbo.DIM_TIME dt
                           ON DT.PK_Date = SS.Date)
    UPDATE mycte
    SET    DateID = CASE WHEN DateID IS NULL THEN -1 ELSE ID END

    Monday, January 28, 2013 10:21 PM