Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Simple Update from Table

Author  Topic 

trellend
Starting Member

9 Posts

Posted - 2009-02-27 : 15:34:12
I have a table variable of names and points. I have another table than contains names,points and a date. I can insert all the items into the 3 col table, but I cannot seem to be able to update the other table with the values from the temp table. Simple example:


//This has the data to be added to the other table
DECLARE @TempPoints TABLE (
UserName varchar(20),
Points int )

//Pretend this is a real table
DECLARE @Points TABLE (
UserName varchar(20),
Points int )

//This is part of a large calculation - made simple -
//can contain multiple lines
Insert into @TempPoints (UserName,Points) VALUES ('a',5)

//This is a table of 1000's of users
Insert into @Points (UserName,Points) VALUES ('a',60)

Update @Points set Points = Points + ( @TempPoints.Points ) where Points.Username in (Select UserName from @TempPoints)


When finished, I want Points to contain ('a',65)

But it get this undefined scalar error. Please help.

vijayisonly
Master Smack Fu Yak Hacker

1836 Posts

Posted - 2009-02-27 : 15:43:01
Try this

Update t set t.Points = t.Points + t1.Points from @Points t inner join @TempPoints t1
on t.UserName = t1.UserName
Go to Top of Page

trellend
Starting Member

9 Posts

Posted - 2009-02-27 : 15:45:34
Perfect - Thank you.
Go to Top of Page

vijayisonly
Master Smack Fu Yak Hacker

1836 Posts

Posted - 2009-02-27 : 16:05:33
Great. Good luck!
Go to Top of Page

trellend
Starting Member

9 Posts

Posted - 2009-02-27 : 17:42:54
Your help will make this blank page full.

[url]http://www.loneaspen.com/Tournaments/leaderboard.aspx[/url]
Go to Top of Page
   

- Advertisement -