数据导出为CSV后,在SQL中更新记录

时间:2013-03-21 23:26:45

标签: ssis sql-server-2008-r2

我对SSIS仍然很陌生,而且在SQL中也是如此。我创建了一个数据流任务,它从表中提取与UNION ALL脚本匹配的记录,并将它们转储到服务器上的.CSV文件中。然后,我添加了一个FTP任务,将文件上传到收件人以便在其结束时进行处理。转储数据的标准之一是表中的TransDate字段为空,StartDate字段等于今天。因为有4个StartDates和4个TransDates,所以我使用UNION All并根据4组数据中的哪一组分配“line#”。我缺少的是一种验证转储到CSV的记录然后设置与该行对应的TransDate的方法。

SELECT
    CallLog.CallID as Ticket
    , 1 as Line
    , CallLog.CustID as Store#
    , AcctNum as SoldTo
    , CAST(BillStart01 as DATE) as BillStart
    , Cast(BillEnd01  as DATE) as BillEnd
    , CostSheet01 as BillAmount
    , SKU01 as SKU
    , Term01 as Terms
    , CAST(EffDate01 as DATE) as EffStart
    , CAST(EffDate02 as DATE) as EffEnd
FROM
    CallLog, Detail, Subset
WHERE
    (CallLog.CallID=Detail.CallID and CallLog.CallID=Subset.CallID) 
AND CallType='Contract' 
    AND TransDate01 = ''
    AND Cast(SentOps as Date) = CONVERT(date,GETDATE()) 
    AND Month(RcvDate01) <= Month(GETDATE()) 
    AND YEAR(RcvDate01) = YEAR(GetDate())

UNION ALL

SELECT CallLog.CallID as Ticket
    , 2 as Line
    , CallLog.CustID as Store#
    , AcctNum as SoldTo
    , CAST(BillStart 02 as DATE) as BillStart
    , Cast(BillEnd02 as DATE) as BillEnd
    , CostSheet02 as BillAmount
    , SKU02 as SKU
    , Term02 as Terms
    , CAST(EffDate01 as DATE) as EffStart
    , CAST(EffDate02 as DATE) as EffEnd
FROM
    CallLog, Detail, Subset
WHERE
    (CallLog.CallID=Detail.CallID and CallLog.CallID=Subset.CallID) 
AND CallType='Contract' 
    AND TransDate02 = ''
    AND Cast(SentOps as Date) = CONVERT(date,GETDATE())
    AND Month(RcvDate02) <= Month(GETDATE()) 
    AND YEAR(RcvDate02)=YEAR(GetDate())

UNION ALL

SELECT
    CallLog.CallID as Ticket
    , 3 as Line
    , CallLog.CustID as Store#
    , AcctNum as SoldTo
    , CAST(BillStart 03 as DATE) as BillStart
    , Cast(BillEnd03 as DATE) as BillEnd
    , CostSheet03 as BillAmount
    , SKU03 as SKU
    , Term03 as Terms
    , CAST(EffDate01 as DATE) as EffStart
    , CAST(EffDate02 as DATE) as EffEnd
FROM 
    CallLog, Detail, Subset
WHERE
    (CallLog.CallID=Detail.CallID and CallLog.CallID=Subset.CallID)
    AND CallType='Contract' 
    AND TransDate03 = ''
    AND Cast(SentOps as Date) = CONVERT(date,GETDATE())
    AND Month(RcvDate03) <= Month(GETDATE())
    AND YEAR(RcvDate03) = YEAR(GetDate())

UNION ALL

SELECT
    CallLog.CallID as Ticket
    , 4 as Line
    , CallLog.CustID as Store#
    , AcctNum as SoldTo
    , CAST(BillStart 04 as DATE) as BillStart
    , Cast(BillEnd04 as DATE) as BillEnd
    , CostSheet04 as BillAmount
    , SKU04 as SKU
    , Term04 as Terms
    , CAST(EffDate01 as DATE) as EffStart
    , CAST(EffDate02 as DATE) as EffEnd
FROM
    CallLog, Detail, Subset
WHERE
    (CallLog.CallID=Detail.CallID AND CallLog.CallID=Subset.CallID) 
AND CallType='Contract' 
    AND TransDate04 = ''
    AND Cast(SentOps as Date) = CONVERT(date,GETDATE()) 
    AND Month(RcvDate04)<=Month(GETDATE()) 
    AND YEAR(RcvDate04)=YEAR(GetDate())
Order BY Ticket, Line

感谢任何帮助/指示。

谢谢,

杰夫

2 个答案:

答案 0 :(得分:1)

Jeff,看起来你需要将这4个select语句中的每一个保存在不同的数据源中 - 因此需要4个OLEDB数据源。这似乎是您解决方案的关键。

然后在每个数据源之后,使用条件拆分来确定该行是否应包含在输出中。因此,每个源的4个条件分割。

在条件分割后(如果符合条件),您还需要一个派生列转换来设置TransDate。

最后,使用Union All转换将它们组合在一起,并将union all的输出放到csv文件中。

答案 1 :(得分:0)

管理导出批次的更强大的方法:

  1. 通过使用标记日期(例如2099-01-01)更新所有空白转录,将您关于的记录(在所有四个表中)标记为要导出。这将识别并“冻结”您要导出的一批记录。

  2. 现在仅导出和转移传输日期为2099-01-01

  3. 的记录
  4. 导出和FTP进程完成后没有错误,请将标记为Transdate的记录更新为今天的日期

  5. 如果没有出现错误的过程,请将2099-01-01日期设置为空白。

  6. 此过程允许您隔离要导出的记录。如果您没有先标记它们,那么当您仍在导出较早的集合时,新的(空白)记录可能会到达,然后它们会被错误地标记为“已导出”。