如何在SSIS / SQL中编写此THCH ELSE查询?

时间:2014-05-08 16:44:57

标签: mysql sql sql-server sql-server-2008 ssis

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')

我正在尝试写入SSIS中的FACT表,但我只想从源表(分段)中提取记录,这些记录是新记录,因此是最后一个语句。第一个声明是处理FACT表为空**

1 个答案:

答案 0 :(得分:1)

这样的事情:

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