Solved

How to insert / update data into database in sqlserver

Posted on 2011-03-04
7
153 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
Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

 

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

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Count with a subquery showing details 10 50
This query failed in sql 2014 5 33
Sql query with where clause 2 34
TSQL convert date to string 4 38
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…
SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

820 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