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
 General SQL Server Forums
 New to SQL Server Programming
 Update vs Insert Into Table

Author  Topic 

swims01
Yak Posting Veteran

59 Posts

Posted - 2009-05-29 : 11:18:17
I have a temporary table which I'm using individual queries to insert data into.

How can I use an INSERT statement to insert data into an existing row based on existing data matches?

DECLARE @tblResults TABLE (
[Club] CHAR(20),
[First] DECIMAL (9,2),
[Second] DECIMAL (9,2))

INSERT INTO @tblResults (club, First)
SELECT stuff
FROM stuff
GROUP BY club

INSERT INTO @tblResults (club, Second)
SELECT stuff
FROM stuff
GROUP BY club


If there are 3 clubs then the above will create 6 rows within the temporary table. I'd like the second query to insert it's results into the appropriate existing rows based on the club.

I've done a search and people reference "WHERE EXISTS (Select * from TABLE WHERE @tblResults.club = query.club)" but that doesn't seem to work.

As always - many thanks!

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2009-05-29 : 12:24:10
First step do "update where exists" and second step do "insert where not exists".
Maybe you can show what you have tried so far because "but that doesn't seem to work" is not very clear.

Webfred



No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

swims01
Yak Posting Veteran

59 Posts

Posted - 2009-05-29 : 12:34:50
edit**

I inserted the results from the second table into a second temporary table and then updated the first on the second.

All good =)
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-05-29 : 14:09:29
are you try to cross tab values onto columns based on another field value?
Go to Top of Page
   

- Advertisement -