Update a table using data in another table

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • obs
    New Member
    • Oct 2007
    • 8

    Update a table using data in another table

    Hi all.
    I have two tables:

    Pages consists of an id field: PageId, and some other fields
    NewsTemplate consists of another id field: PageId , and some other fields

    I want to update PageId in NewsTemplate to get the value of the largest PageId in Pages, so I use the following code :

    SELECT MAX(PageId) AS maxPageId FROM Pages

    UPDATE NewsTemplate SET PageId = maxPageId WHERE (PageId IS NULL)


    I get the following error: Invalid column name 'maxPageId'.

    What's wrong with my code ?
  • debasisdas
    Recognized Expert Expert
    • Dec 2006
    • 8119

    #2
    Is maxPageId avariable in your code ?

    Then how can u use that to update any other table .

    Comment

    • obs
      New Member
      • Oct 2007
      • 8

      #3
      Originally posted by debasisdas
      Is maxPageId avariable in your code ?

      Then how can u use that to update any other table .
      I got it eventually. I did it using :

      SELECT @pID = cast(SCOPE_IDEN TITY() AS int)

      Thanks anyway, cheers !

      Comment

      Working...