Domanda

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**

È stato utile?

Soluzione

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
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top