?
Solved

How to insert / update data into database in sqlserver

Posted on 2011-03-04
7
Medium Priority
?
162 Views
Last Modified: 2013-03-21
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
0
Comment
Question by:Manikannan
  • 5
  • 2
7 Comments
 
LVL 8

Accepted Solution

by:
pdd1lan earned 1500 total points
ID: 35034807
here is simple example.

IF EXISTS (SELECT 1 FROM table1)
   UPDATE table1 set field1 = 'tosomething'
ELSE
   INSERT INTO table1 (field1) VALUES ('tosomething')
0
 

Author Comment

by:Manikannan
ID: 35034909
Ya, thanks  pdd1lan.

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,C_TRANSLATEDSTRINGS)values (@TNR_LANGUAGE,@C_LABELNAME,@C_TRANSLATEDSTRINGS)

End
0
 
LVL 8

Expert Comment

by:pdd1lan
ID: 35035070
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:

http://blog.sqlauthority.com/2008/02/26/sql-server-select-1-vs-select-an-interesting-observation/
0
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 

Author Comment

by:Manikannan
ID: 35035971
pdd1lan,

Its working .

Thanks for ur kind of informations.
0
 
LVL 8

Expert Comment

by:pdd1lan
ID: 35036600
glad to hear
0
 
LVL 8

Expert Comment

by:pdd1lan
ID: 35040153
can you give points ?
0
 
LVL 8

Expert Comment

by:pdd1lan
ID: 35097710
??
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question