Check for Existing Data in a table (SQL 2008)

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
Who is Participating?
Lee SavidgeConnect With a Mentor Commented:
A typo. Truncate tableB.

Obviously you will want to test this first.
Lee SavidgeCommented:
if exists (select top 1 * from tableA)
  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

Open in new window

SeTechAuthor Commented:
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?
SeTechAuthor Commented:
Thanks worked PERFECTLY
Lee SavidgeCommented:

Glad to be of help :)
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.