简体   繁体   中英

Counting the rows in a table before performing a delete and insert

Sorry if the answer to this is very simple, I have goggled and cannot find it.

I have a job that runs overnight and performs the following actions

delete from [CLOUD].[SAP-Data].TABLE_A

INSERT INTO [CLOUD].[SAP-Data].TABLE_A
select *
    from ACTA.TABLE_B
    where
    EXPIRY_DATE > CONVERT(VARchar(30), GetDate(),102) 

However on occasions the information in ACTA.TABLE_B has not refreshed and it does not contain the correct number of records. I need to stop the

delete and insert into [CLOUD].[SAP-Data].TABLE_A 

occurring on these occasions.

When the issue occurs the row count of the records in ACTA.TABLE_B will always be below 100,000.

Any help would be really appreciated.

Thank you Lyng

you can use if to control the flow of the program

IF(select count(*) from from ACTA.TABLE_B where
EXPIRY_DATE > CONVERT(VARchar(30), GetDate(),102) ) > 100000
 BEGIN 
delete from [CLOUD].[SAP-Data].TABLE_A
INSERT INTO [CLOUD].[SAP-Data].TABLE_A
select *
from ACTA.TABLE_B
where
EXPIRY_DATE > CONVERT(VARchar(30), GetDate(),102) 
 END 
ELSE 
 BEGIN
PRINT 'BAD DATA Skipping delete and Insert'
 END 

Below code may helpful to you

IF ((SELECT COUNT(*) FROM [CLOUD].[SAP-Data].TABLE_A WHERE
EXPIRY_DATE > CONVERT(VARCHAR(30), GETDATE(),102)) > 100000)
BEGIN
    DELETE FROM [CLOUD].[SAP-Data].TABLE_A
    INSERT INTO [CLOUD].[SAP-Data].TABLE_A
    SELECT *
    FROM ACTA.TABLE_B
    WHERE
    EXPIRY_DATE > CONVERT(VARCHAR(30), GETDATE(),102)
END
ELSE
BEGIN
    PRINT 'Data Refreshing Problem'
END

Thank You

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM