Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: SQL-Server
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 10-Jan-13 21:58pm
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  
Comments
__TR__ at 11-Jan-13 3:16am
   
My 5!
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

As Rahul suggested you can check if a particular row exists in a table using EXISTS (Transact-SQL)[^]
Here is a sample
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.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

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..
  Permalink  
v2

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 240
1 CHill60 205
2 Zoltán Zörgő 175
3 Richard Deeming 170
4 Richard MacCutchan 150
0 Sergey Alexandrovich Kryukov 9,328
1 OriginalGriff 6,806
2 Peter Leow 4,442
3 Zoltán Zörgő 3,984
4 Richard MacCutchan 2,807


Advertise | Privacy | Mobile
Web01 | 2.8.150129.1 | Last Updated 14 Feb 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100