UPDATE with subqueries in WHERE clause?

  • Hi,

    Is this possible, and if so what is the correct syntax.

    I have one table called Contacts...

    pkContactID
    ContactName
    ContactAddress, etc.

    And one table called Projects

    pkProjectID
    fkClientLocationID
    fkContactID
    Project Name, etc.

    I want to pull the fkClientLocation field out of Projects and put it into Contacts.  So I added an empty field called fkClientLocationID to Contacts and I want to update it with an UPDATE query, something like

    UPDATE Contacts AS C
    Set fkClientLocationID =
    (SELECT fkClientLocationID FROM Projects AS P WHERE P.fkContactID=C.pkContactID)

    I know this is not the correct syntax.  Basically, how do I reference a field in the main query, from within the subquery?

    Thanks.

  • Can't test the query from here, but it should be pretty close :

    UPDATE C

    Set C.fkClientLocationID = P.fkClientLocationID

    FROM dbo.Contacts inner join dbo.Project P on P.fkContactID=C.pkContactID

    Why exactly do you want to have the same information in 2 different tables?

  • Hi Remi,

    I get a Syntax error (missing operator)

    Any ideas?

    Why is the same info in 2 tables?  Because it's what my client gave me, and I'm trying to get it normalized. 

    Thanks for your help.

    Aaron

  • UPDATE C

    Set C.fkClientLocationID = P.fkClientLocationID

    FROM dbo.Contacts C inner join dbo.Project P on P.fkContactID=C.pkContactID

  • I know this is not the correct syntax. Basically, how do I reference a field in the main query, from within the subquery?

    UPDATE Contacts AS C

    Set fkClientLocationID =

    (SELECT fkClientLocationID FROM Projects AS P WHERE P.fkContactID=C.pkContactID)

    Simply remove the red items (the alias for the table to update) and the query is correct. Although Remi's query would win the performance race this time, this query is ANSI standard instead of the proprietary SQL Server syntax in the other one.

  • Can this be adjusted to work with JET SQL, or just SQL Server?

    Aaron

  • No idea. Does not any of them work in Jet?

  • This syntaxe is just forbidden : UPDATE Contacts AS C

    You must use the as in the from clause. And yes my query will run much faster .

  • Yes, it can be adjusted for Jet, which is really uglier (is there such a word )

    UPDATE Contacts  inner join Project on Project.fkContactID = Contacts.pkContactID Set Contacts.fkClientLocationID = Project.fkClientLocationID;

     

     


    * Noel

  • Uglier is the right word here .

  • Thanks guys,

    I ran it in SQL Server and Imported the tables back into Access (I hate Jet SQL anyway).

    While it's currently in Access it's going to get centralized back into SQL Server at some point.

    The query worked beautifully.  Thank you.

    Aaron

  • HTH


    * Noel

  • I should send the link to sushila... proves I'm not the only one who HTHs guys to add to the posts count .

  • If you look at my posts I don't do this very often and I am not concerned at all with my post count  it was just a courtesy (so I thought :cool. But if it makes you happier I am not doing it any more

     

     


    * Noel

  • I don't mind... as long as you keep agreeing with my solutions .

Viewing 15 posts - 1 through 15 (of 17 total)

You must be logged in to reply to this topic. Login to reply