Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Check for Existing Data in a table (SQL 2008)

Posted on 2014-03-04
5
Medium Priority
?
356 Views
Last Modified: 2014-03-04
I first want to check if data exists in a table A, then if yes I want to truncate data in table B and replace with the data in table A. If No I do not want to do anything.
I believe there is some sort of row function or exist statement that is needed, however my attempts are causing errors. Please advise. Thanks
0
Comment
Question by:SeTech
[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
  • 3
  • 2
5 Comments
 
LVL 25

Expert Comment

by:Lee Savidge
ID: 39902960
if exists (select top 1 * from tableA)
begin
  truncate table tableA
  insert into tableB
  select * from tableA -- this assumes your table structures are the same. If not, select only the columns you want
end

Open in new window

0
 

Author Comment

by:SeTech
ID: 39902986
Thanks and one additional quesiton on your post. Should the truncate table tableA really be truncate table tableB as that is the table I want to update. So most likley that was just a typo?
0
 
LVL 25

Accepted Solution

by:
Lee Savidge earned 2000 total points
ID: 39903023
A typo. Truncate tableB.

Obviously you will want to test this first.
0
 

Author Closing Comment

by:SeTech
ID: 39903089
Thanks worked PERFECTLY
0
 
LVL 25

Expert Comment

by:Lee Savidge
ID: 39903245
Splendid!

Glad to be of help :)
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Windocks is an independent port of Docker's open source to Windows.   This article introduces the use of SQL Server in containers, with integrated support of SQL Server database cloning.
When trying to connect from SSMS v17.x to a SQL Server Integration Services 2016 instance or previous version, you get the error “Connecting to the Integration Services service on the computer failed with the following error: 'The specified service …
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

609 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