• jcelko212 32090 - Friday, October 20, 2017 5:57 PM

    soldout6000 - Wednesday, October 18, 2017 2:53 PM

    I have two tables T_V and T_C. I'm updating T_V's columns with T_C's respective columns. I'm getting an error during compiling the procedure. The error is 

    Msg 156, Level 15, State 1, Procedure proc1
    Incorrect syntax near the keyword 'INNER'.

    CREATE PROCEDURE proc1
    AS
    BEGIN
    UPDATE T_V INNER JOIN T_C ON T_V.[Unique ID] = T_C.[Unique ID]
    SET T_V.A = 1-[T_C].A, T_V.B = 1-[T_C].B, T_V.C = 1-[T_C].C, T_V.D = 1-[T_C].D;
    END
    GO

    >> I have two tables T_V and T_C. I'm updating T_V's columns with T_C's respective columns. I'm getting. Incorrect syntax near the keyword 'INNER'. <<

    When it's not technically wrong, using the prefix "T_" is a design flaw called a Tibble. It means you're putting metadata in the names. We used have to do this in the old days with one pass compilers, but that was 60 years ago.

    I like you to think about doing an update on the join and what it really means. The join produces a new table, so you update that new table which  disappears at the end of the session. In short, the syntax makes no sense whatsoever. You also need to learn how to use the new merge statement is been around for about five or six years. Why did you think "unique ID" would be a good variable name? Why did you embed spaces in it? Why the one-letter table names? Why no DDL?
    Let's use nonsense names that might actually be ISO-11179 compliant.
    MERGE INTO Voobs
    USING Cloobs
    ON Voobs.something_id = Cloobs.something_id
    WHEN MATCHED
    THEN UPDATE
    SET voobs.a = 1 - cloobs.a,
      voobs.b = 1 - cloobs.b,
      voobs.c = 1 - cloobs.c,
      voobs.d = 1 - cloobs.d;

    Why use MERGE when all you are doing is an UPDATE?  Not only that, I have seen the MERGE statement take longer and use more resources than the combination of UPDATE and INSERT (UPSERT).
    Just because you can use it doesn't mean you should.