I want to Add a if .. else statement to make sure if the Data is exist in Table
if (exist )
{ do a update query}
else
{
do a Insert Query}
I Know how to update and how to insert but how to check if the data is exist in Table and how to add a if ..else to query?
thank you very much
Try this
IFEXISTS(SELECT CustomerIDfrom CustomersWhere CustomerName='Scott')
BEGIN
-- Put Update Query Here
END
ELSE
BEGIN
-- Put Insert Query Here
END
Or, if your EXISTS(SELECT is verifying the same coditions as UPDATE, you can write:
{Your UPDATE query}
if @.@.ROWCOUNT = 0 -- nothing was updated
{Your INSERT query}
|||May be this can help you:
-------------------------------------------------------
If(Select Count(*) From Tabel1 Where Data1 = 'your matching Condition')>0--or Somthing like this
Begin
Insert Tabel1
(--------)
Values
(----)
End
Else If(Some Other Condition)
Begin
Insert Tabel2
(--------)
Values
(----)
End
Else
Begin
'Your update Query'
End
Note that IF EXISTS() is more efficient then IF (SELECT COUNT(*) ...)>0
|||Create procedure InsertUpdate
@. recordid int
as
Begin
Declare @.count int;
Select @.Count = * from TABLE1 Where recordid= @. recordid
if @.Count > 0
Update set value1= @.value1 , value2 = @.value2 where recordid= @. recordid
else
Insert Into Table1 values....
End
|||
Thanks dear i was also having same problem .Thanks for help
No comments:
Post a Comment