Wrox Home  
Search P2P Archive for: Go

  Return to Index  

ado_dotnet thread: Adding to existing or new records


Message #1 by "Frode" <fstroemm@o...> on Mon, 29 Apr 2002 14:43:20
I have a couple of variables I would like to add to a database.
Say I have:

Name     Score
John     5
Peter    12
Adam     7

How could I check a database (dataset/datatable?) If the names exists,
if so add the score to their existing score. If not, create a new ID and 
then add name and score as a new row.

I'm using C#, and examples using access or sql server would be fine.
(I guess adding a new record with SQL server you use @@identity when you 
make a new record to get the ID created. Not sure how this works in access)

thanks

- Frode
Message #2 by "Paul Johnson" <pe_johnson46@h...> on Tue, 30 Apr 2002 02:17:41
In T-SQL you can use the EXISTS operator to check for an existing record:

   IF EXISTS(SELECT * FROM dbo.Scores WHERE Name LIKE('John'))
   BEGIN
         -- record found, run the update...
   END
   ELSE
   BEGIN
         -- insert a new record...
   END
   
 

> I have a couple of variables I would like to add to a database.
S> ay I have:

> Name     Score
J> ohn     5
P> eter    12
A> dam     7

> How could I check a database (dataset/datatable?) If the names exists,
i> f so add the score to their existing score. If not, create a new ID and 
t> hen add name and score as a new row.

> I'm using C#, and examples using access or sql server would be fine.
(> I guess adding a new record with SQL server you use @@identity when you 
m> ake a new record to get the ID created. Not sure how this works in 
access)

> thanks

> - Frode

  Return to Index