Check for Existing Data in a table (SQL 2008)

SeTech
SeTech used Ask the Experts™
on
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
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
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

SeTechAnalyst

Author

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?
A typo. Truncate tableB.

Obviously you will want to test this first.
SeTechAnalyst

Author

Commented:
Thanks worked PERFECTLY
Splendid!

Glad to be of help :)

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial