Click here to Skip to main content
16,023,124 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
hi,

I have to use a SQL SERVER 2000 & form a query to update the record with new data if record exists, else insert it into the table.
Can it be done without using stored procedure.
Posted
Updated 1-Jun-10 9:58am
v2
Comments
radix3 2-Jun-10 0:50am    
post in what u tried first

You can't perform a merge in SQL Server 2000 without multiple queries. If you are fine with multiple queries, then here are two approaches you could take.

  • Approach #1.
    SQL
    -- First query.
    INSERT INTO SomeTable (...) VALUES (...)
    WHERE NOT EXISTS (SELECT 0 FROM SomeTable WHERE ...)
    -- Second query.
    UPDATE SomeTable SET ...

  • Approach #2.
    SQL
    -- First query.
    SELECT
        CASE
            WHEN EXISTS (SELECT 0 FROM SomeTable WHERE ...) THEN 'update'
            ELSE 'insert'
        END
    -- Second query (if first returned "insert").
    INSERT INTO SomeTable (...) VALUES (...)
    -- Second query (if first returned "update").
    UPDATE SomeTable SET ...

There is also a method of cheating which technically satisfies your requirement, but is probably not what you are looking for. You could perform an insert into a secondary table. That table would have a trigger that would then perform the conditional logic to merge that data into the primary table. While a trigger is technically not a stored procedure, I don't think that's what you are looking for. On a similar note, you could actually put the trigger on the primary table. In that case, the trigger would delete the row if it already exists, which would mean the newly inserted data should work fine... that might cause some trouble with identity fields and such though (you might be able to resolve that by turning identity insert on). That way, you would just perform an insert and the old record would get deleted before the new one gets inserted (i.e., no update necessary).


Looks like it can be done. The credit for finding that out belongs to Andrew. Here is some sample code:
C#
SqlConnection conn = new SqlConnection("...");
SqlCommand cmd = new SqlCommand(@"IF EXISTS(SELECT 0 FROM SomeTable
WHERE ID = 0) BEGIN UPDATE SomeTable SET ID = ID + 1 END
ELSE BEGIN INSERT INTO SomeTable(ID) VALUES(0) END", conn);
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
 
Share this answer
 
v2
Comments
Andrew Rissing 1-Jun-10 20:02pm    
You can just do "IF EXISTS (SELECT 1 FROM SomeTable WHERE ...) THEN BEGIN <update> END ELSE BEGIN <insert> END"

You don't need to store the value of the EXISTS, just use an "IF" to control the logic flow.
AspDotNetDev 1-Jun-10 20:13pm    
An IF statement will not work in a query you send from C#. That works fine in a stored procedure, but not in a standalone query.
AspDotNetDev 1-Jun-10 20:25pm    
Looks like I'm wrong. I just tried it and it seems you can use an IF statement in a query. Thanks for the info! That also makes things much easier for the OP. I'll update my answer.
saloni15 2-Jun-10 1:14am    
hi!! thanxs fro replying but im not able to implement it..
Actually i have a table with phone number,name,address. and want to have unique phone number list so i need to perform the function that i have written in question.Can u plz write a query in that format.. plz
AspDotNetDev 2-Jun-10 3:05am    
I trust you'll be able to figure it out. Good luck.
saloni15 wrote:
form a query to update the record with new data if record exists else insert it into table. can it be done without using stored procedure..


It totally depends on the uniqueness you define for the records.

If its a simple uniqueness(just a field or so...) then a unique constraint might do for you....
but
If its a complex uniqueness(or an entire row data needs to matchup), it might be good to verify that in Businesslogic(if in code) or a SQL function (if in DB)...

So, without using stored proc, it can be done but would depend on your requirement!
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900