?
Solved

Check for Existing Data in a table (SQL 2008)

Posted on 2014-03-04
5
Medium Priority
?
359 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 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

Fill in the form and get your FREE NFR key NOW!

Veeam is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

Question has a verified solution.

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

An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
Microsoft Access has a limit of 255 columns in a single table; SQL Server allows tables with over 255 columns, but reading that data is not necessarily simple.  The final solution for this task involved creating a custom text parser and then reading…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Suggested Courses

840 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