Connecting Tech Pros Worldwide Forums | Help | Site Map

Update a table using data in another table

obs obs is offline
Newbie
 
Join Date: Oct 2007
Posts: 8
#1: Oct 21 '07
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's Avatar
Moderator
 
Join Date: Dec 2006
Location: Bangalore ,India
Posts: 7,508
#2: Oct 21 '07

re: Update a table using data in another table


Is maxPageId avariable in your code ?

Then how can u use that to update any other table .
obs obs is offline
Newbie
 
Join Date: Oct 2007
Posts: 8
#3: Oct 21 '07

re: Update a table using data in another table


Quote:

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_IDENTITY() AS int)

Thanks anyway, cheers !
Reply


Similar Microsoft SQL Server bytes