Solved

How to insert / update data into database in sqlserver

Posted on 2011-03-04
7
152 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 500 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

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

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

Suggested Solutions

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
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.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

773 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