How to insert / update data into database in sqlserver
I have a table which contains data already.
Now i need to create some script which insert some data into the table
But befire insert the script, i have to validate the data is exist in the table or not.
If not exist thst script will insert otherwise it will update
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
If exists(select * from Translations where TNR_LANGUAGE = @TNR_LANGUAGE AND C_LABELNAME = @C_LABELNAME)
Begin
update Translations set C_TRANSLATEDSTRINGS = @C_TRANSLATEDSTRINGS where TNR_LANGUAGE = @TNR_LANGUAGE AND C_LABELNAME = @C_LABELNAME
End
Else
Begin
insert into Translations (TNR_LANGUAGE,C_LABELNAME,C_TRANSLATEDSTRINGS)values (@TNR_LANGUAGE,@C_LABELNAME,@C_TRANSLATEDSTRINGS)
End
pdd1lan
does your query work?
here are some suggestion:
-not hurt to have try... catch to capture any error might occur around your if statement.
-the way you do is fine, but might consider not to use "select *" when you just check for exist record, I suggest using "Select 1" instead.
If exists(select 1 from Translations where TNR_LANGUAGE = @TNR_LANGUAGE AND C_LABELNAME = @C_LABELNAME)
here is an article, might have better explanation why:
I used the following query.
If exists(select * from Translations where TNR_LANGUAGE = @TNR_LANGUAGE AND C_LABELNAME = @C_LABELNAME)
Begin
update Translations set C_TRANSLATEDSTRINGS = @C_TRANSLATEDSTRINGS where TNR_LANGUAGE = @TNR_LANGUAGE AND C_LABELNAME = @C_LABELNAME
End
Else
Begin
insert into Translations (TNR_LANGUAGE,C_LABELNAME,
End