Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi everyone,

what i actually want to do is.. i want to do insert and update operation on single query. i tried getting the unique value from table.
if the unique value is matched update operation will be done.. other wise insert operation will in action.


But i don't know where to use the selected column from the table in If statement..

please guide me

Thanks in advance
Posted

How about having a stored procedure for it. Check the value first and based on whether the value is found or not perform insert or update accordingly in the stored procedure only.
 
Share this answer
 
Comments
__TR__ 11-Jan-13 3:16am    
My 5!
As Rahul suggested you can check if a particular row exists in a table using EXISTS (Transact-SQL)[^]
Here is a sample
SQL
IF EXISTS (SELECT 1 FROM YourTable WHERE YourColumn = ValueToCheck)
BEGIN

   --Update Operation

END
ELSE
BEGIN

   --Insert Operation

END


You write this in a stored procedure and pass the value to check as a parameter to the stored procedure.
 
Share this answer
 
CSS
i got a solution. created one public string if the string is null do insertion action.
else the string contain value do updation operation


public string new{ get; set; }

new  =  "new";

if(new == "new")
{
------do insert operation--------
}
else
{
-------- do updation operation-------
}

it works fine for me..
 
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