SQLTeam.com | Weblogs | Forums

I got a Stored Procedures that truncate the table TBL1 before insert data from TBL2 table, but before I truncate the table I want to make sure the table TBL2 rowcount <> 0, if rowcount = 0 then PRINT '* Source empty *' and go to end_of_sp ELSE Insert


#1

BEGIN
SELECT COUNT(*) FROM TBL2
RETURN @@ROWCOUNT

Heading

if @@ROWCOUNT = 0

PRINT '*** Source file empty ***'  

GOTO
END


#2

Use NOT EXISTS to check for existance of rows

IF NOT EXISTS (SELECT * FROM TBL2)
BEGIN
    TRUNCATE TABLE TBL1

    INSERT INTO TBL1 ( . .  . )
    SELECT  . . . 
    FROM    TBL2
END
ELSE
BEGIN
    PRINT '*** Source file empty ***'
END