Click here to Skip to main content
11,479,486 members (46,745 online)
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 OriginalGriff 255
1 Sergey Alexandrovich Kryukov 215
2 F-ES Sitecore 175
3 Richard MacCutchan 120
4 Frankie-C 104
0 Sergey Alexandrovich Kryukov 7,890
1 OriginalGriff 7,441
2 Sascha Lefèvre 3,064
3 Maciej Los 2,491
4 Richard Deeming 2,335


Advertise | Privacy | Mobile
Web04 | 2.8.150520.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