Solved

Check for Existing Data in a table (SQL 2008)

Posted on 2014-03-04
5
340 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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

863 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

23 Experts available now in Live!

Get 1:1 Help Now