Solved

How to insert / update data into database in sqlserver

Posted on 2011-03-04
7
150 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:Manikannan
Comment Utility
pdd1lan,

Its working .

Thanks for ur kind of informations.
0
 
LVL 8

Expert Comment

by:pdd1lan
Comment Utility
glad to hear
0
 
LVL 8

Expert Comment

by:pdd1lan
Comment Utility
can you give points ?
0
 
LVL 8

Expert Comment

by:pdd1lan
Comment Utility
??
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Long way back, we had to take help from third party tools in order to encrypt and decrypt data.  Gradually Microsoft understood the need for this feature and started to implement it by building functionality into SQL Server. Finally, with SQL 2008, …
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.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

744 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now