Results 1 to 6 of 6

Thread: Problem with update query (RESOLVED)

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2001
    Location
    Europe
    Posts
    289

    Problem with update query (RESOLVED)

    Hi

    I am trying to build an update query but keep getting an error that says the column TableB.FirstName does not exist:

    update TableA set TableA.emailaddress = TableB.emailaddress where
    TableA.firstname = TableB.FirstName and TableA.lastname = Tableb.lastname

    Any ideas? Many thanks.

    Mike
    Last edited by MikeGarvin; Dec 10th, 2008 at 02:25 PM.

  2. #2
    Addicted Member
    Join Date
    Dec 2006
    Location
    Between Try & Catch
    Posts
    249

    Re: Problem with update query

    Why do you have two tables with the same data? Is one an archive/backup of some sort?

    In any case, would this work?

    Code:
    Update TableA set emailaddress =
    (Select TableB.emailaddress
     From TableA, TableB
     Where TableA.firstname = TableB.firstname
     And TableA.lastname = TableB.lastname)
    If not, someone more experienced may be able to help.
    If my post helped you, please rate it!

    Languages: VB/ASP.NET 2005, C# 2008,VB6
    Databases: Oracle (knowledge not currently in use), DB2

    FROM Customers
    WHERE We_Know_What_We_Want <> DB.Null
    SELECT *
    0 rows returned

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2001
    Location
    Europe
    Posts
    289

    Re: Problem with update query

    Thanks. I get this error:

    You can't specify target table 'TableA' for update in FROM clause

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2001
    Location
    Europe
    Posts
    289

    Re: Problem with update query

    Here is the solution:

    UPDATE tablea INNER JOIN tableb ON (tablea.lastname = tableb.lastname) AND (tablea.firstname = tableb.firstname) SET tablea.emailaddress = tableb.emailaddress

  5. #5
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: Problem with update query (RESOLVED)

    I would normally write like this:

    sql Code:
    1. Update TableA Set
    2.   TableA.EmailAddress = TableB.EmailAddress
    3. Inner Join TableB On
    4.   TableA.FirstName = TableB.FirstName And
    5.   TableA.LastName = TableB.LastName
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  6. #6
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Re: Problem with update query (RESOLVED)

    You could also try this.

    Code:
    UPDATE TableA SET 
      TableA.EmailAddress = TableB.EmailAddress 
    FROM TableA INNER JOIN TableB ON
      TableA.FirstName = TableB.FirstName AND 
      TableA.LastName = TableB.LastName
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width