babu
babu

Reputation: 11

How to check empty table in sql query?

I have two tables top1 and top2 .First I want to check top1 is empty or not.If top1 is not empty then truncate top2 and insert data from top1. Otherwise dont do any action .

CASE
WHEN top1 is not empty THEN
 Truncate top2
  Insert from Top1

END case

Upvotes: 1

Views: 1857

Answers (1)

AntDC
AntDC

Reputation: 1917

Something like the below should do the trick. Provided both of the tables have the same structure....

IF EXISTS(SELECT 1 FROM top1) THEN
  TRUNCATE TABLE Top2;
  INSERT INTO top2 SELECT * FROM top1;
END IF;

Upvotes: 1

Related Questions