?
Solved

How to insert / update data into database in sqlserver

Posted on 2011-03-04
7
Medium Priority
?
160 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

 

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

Tech or Treat!

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

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

In this article I will describe the Backup & Restore 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.
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

649 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