Solved

Check for Existing Data in a table (SQL 2008)

Posted on 2014-03-04
5
339 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
  • 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 500 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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Join & Write a Comment

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 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.
Via a live example, show how to shrink a transaction log file down to a reasonable size.
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.

747 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

13 Experts available now in Live!

Get 1:1 Help Now