简体   繁体   中英

How would I write this IF THEN ELSE query in SSIS/SQL?

IF 
SELECT COUNT(*)<2 FROM dbo.FACT_TPG_INVENTORY where STAGING_TABLE_NAME='Staging_SPM_Inventory'

THEN
SELECT        staging_SPM_Inventory_id, DATE_ID, DIM_DATA_SOURCE_ID, site, operation_pk, lookup_product_id, quantity, product_group
FROM            dbo.Staging_SPM_Inventory

ELSE 
SELECT        staging_SPM_Inventory_id, DATE_ID, DIM_DATA_SOURCE_ID, site, operation_pk, lookup_product_id, quantity, product_group
FROM            dbo.Staging_SPM_Inventory WHERE dbo.staging_SPM_Inventory.staging_inserted_time > (SELECT MAX(dbo.fact_tpg_inventory.dw_load_dt) FROM dbo.fact_tpg_inventory WHERE STAGING_TABLE_NAME='Staging_SPM_Inventory')

I'm trying to write to a FACT Table in SSIS but I want to only pull records from the Source Table(Staging) which are new records hence the last statement. The first statement is to deal with the FACT Table being empty**

Something like this:

IF 2 > (SELECT COUNT(*) FROM dbo.FACT_TPG_INVENTORY 
        where STAGING_TABLE_NAME='Staging_SPM_Inventory')
BEGIN
    SELECT  staging_SPM_Inventory_id, DATE_ID, DIM_DATA_SOURCE_ID, site, 
            operation_pk, lookup_product_id, quantity, product_group
    FROM    dbo.Staging_SPM_Inventory
END
ELSE 
BEGIN
    SELECT  staging_SPM_Inventory_id, DATE_ID, DIM_DATA_SOURCE_ID, site, 
            operation_pk, lookup_product_id, quantity, product_group
    FROM    dbo.Staging_SPM_Inventory 
    WHERE   dbo.staging_SPM_Inventory.staging_inserted_time > 
               (SELECT MAX(dbo.fact_tpg_inventory.dw_load_dt) 
                FROM dbo.fact_tpg_inventory 
                WHERE STAGING_TABLE_NAME='Staging_SPM_Inventory')
END

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