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 Kornfeld Eliyahu Peter 169
1 George Jonsson 145
2 Zoltán Zörgő 139
3 PIEBALDconsult 130
4 OriginalGriff 120
0 OriginalGriff 6,165
1 DamithSL 4,658
2 Maciej Los 4,107
3 Kornfeld Eliyahu Peter 3,649
4 Sergey Alexandrovich Kryukov 3,382


Advertise | Privacy | Mobile
Web02 | 2.8.141220.1 | Last Updated 14 Feb 2013
Copyright © CodeProject, 1999-2014
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