SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Enthusiast gecastill's Avatar
    Join Date
    May 2005
    Location
    Virginia US
    Posts
    42
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question How can I copy data from column A in Table A to column B in Table B?

    Using the SQL UPDATE statement how can I copy a columns data say from column A in Table A to column B in Table B?
    Note that I am using Microsoft SQL Server 2000


    I try doing the following:

    UPDATE B SET B.column_B = A.column_A
    FROM Table_A A, Table_B B
    WHERE A.numID = B.numID AND A.Line = B.Line

    but it did not work so I try this

    UPDATE Table_B
    SET column_B = (SELECT column_A FROM Table_A WHERE Table_A.numID = 233)
    WHERE Table_B.numID = 233

    When I do this one I get the following error msg:
    Server: Msg 512, Level 16, State 1, Line 1
    Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression. The statement has been terminated.


    I am trying to copy all the values of column A in Table A to column B in Table B.


    Any suggestions or hints are welcome
    Best Regards,
    Gibran E Castillo

  2. #2
    SitePoint Member riziko's Avatar
    Join Date
    Jun 2005
    Location
    Sydney, Australia
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by gecastill
    Using the SQL UPDATE statement how can I copy a columns data say from column A in Table A to column B in Table B?
    Note that I am using Microsoft SQL Server 2000
    That first one looks correct. What error did you get?
    Quote Originally Posted by gecastill
    UPDATE Table_B
    SET column_B = (SELECT column_A FROM Table_A WHERE Table_A.numID = 233)
    WHERE Table_B.numID = 233

    When I do this one I get the following error msg:
    Server: Msg 512, Level 16, State 1, Line 1
    Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression. The statement has been terminated.
    I would guess that your WHERE clause is not correct. You could cheat by using SELECT TOP 1 column_A but that is not really a good solution.

    It should look something like this (note the table alias):

    UPDATE Employees
    SET TotalSales = (SELECT sum(Orders.Amount) FROM Orders WHERE Orders.EmployeeID = e.EmployeeID)
    FROM Employees e

  3. #3
    SitePoint Enthusiast gecastill's Avatar
    Join Date
    May 2005
    Location
    Virginia US
    Posts
    42
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Hi

    riziko,

    I try your suggestion, but it did not work
    UPDATE table_B SET column_B = (SELECT column_A FROM table_A WHERE table_A.numID = B.numID) FROM table_B B

    I got the following Microsof SQL Server 2000 error:
    Server: Msg 207, Level 16, State 3, Line 1
    Invalid column name 'numID'.

    I don't think it recognizes the column B.numID in the statement.





    I may be better of trying to get this one working
    UPDATE Table_B
    SET column_B = (SELECT column_A FROM Table_A WHERE Table_A.numID = 233)
    WHERE Table_B.numID = 233

    When I do this one I get the following error msg:
    Server: Msg 512, Level 16, State 1, Line 1 Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression. The statement has been terminated.


    The problem here is that it can't process more than one value
    Best Regards,
    Gibran E Castillo


Bookmarks

Posting Permissions

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