我们可以使用SSIS在表中的每一列中插入具有默认值的行吗?

时间:2019-05-30 22:10:23

标签: sql sql-server ssis

我不确定我的问题是否会找到解决方案,搜索后我找不到解决方法。

场景: 我正在使用SSIS开发批处理解决方案。读取数据流任务中的查询。查询会产生一些结果行,我只需要将该结果插入目标表(在MS SQL Server数据库中)。

挑战

我在这里看到的挑战是,即使SQL查询不产生任何行,目标表也应填充默认行。但是,在下面的查询和SSIS中生成CTE似乎并不那么容易。

SQL查询已插入SSIS OLE DB SOURCE EDITOR。

DECLARE @date DATE
SET @date = GETDATE();

with cte as
(
select
   T.mot_fleettypedescription AS 'FleetType'
   ,(CASE WHEN F.mot_averagefleetsize = 0 THEN COUNT(C.mot_nscweight) ELSE 0 END) AS 'Fleet_Range_0'
   ,(CASE WHEN F.mot_averagefleetsize = 1 THEN COUNT(C.mot_nscweight) ELSE 0 END) AS 'Fleet_Range_1'
   ,(CASE WHEN F.mot_averagefleetsize BETWEEN 2 AND 4 THEN COUNT(C.mot_nscweight) ELSE 0 END) AS 'Fleet_Range_2-4'
   ,(CASE WHEN F.mot_averagefleetsize BETWEEN 5 AND 10 THEN COUNT(C.mot_nscweight) ELSE 0 END) AS 'Fleet_Range_5-10'
   ,CONVERT(DATE, C.mot_monthenddate) AS 'MED'
from Filteredmot_carrierfleettypes F
left outer join Filteredmot_carrierfleettype T on F.mot_fleettype = T.mot_carrierfleettypeid
left outer join Filteredmot_fleetcount C on F.mot_carriernumber = C.mot_carriernumber
where F.statecode = 0 AND C.mot_monthenddate = CONVERT(DATE, DATEADD(D,-(DAY(@date)), @date))
group by F.mot_averagefleetsize, T.mot_fleettypedescription, C.mot_monthenddate
)

select 
    CONVERT(NVARCHAR(50), 'by Weight') AS 'Caption', cte.MED AS 'Date', CONVERT(NVARCHAR(10), cte.FleetType) AS 'FleetType',
    CONVERT(NVARCHAR(25), 'ALL') AS 'OperatingStatus',
    SUM(cte.Fleet_Range_0) As 'Fleet_Range_0', SUM(cte.Fleet_Range_1) As 'Fleet_Range_1', SUM(cte.[Fleet_Range_2-4]) 'Fleet_Range_2-4', 
    SUM(cte.[Fleet_Range_5-10]) 'Fleet_Range_5-10', 
    (SUM(cte.Fleet_Range_0) + SUM(cte.Fleet_Range_1) + SUM(cte.[Fleet_Range_2-4]) + SUM(cte.[Fleet_Range_5-10])) 'TOTAL'
from cte
where cte.FleetType = 'Bus'
group by cte.FleetType, cte.MED

重新搜索 如果它是纯SQL Server环境,我将使用以下查询段检查使用COUNT(*)@@ROWCOUNT返回的行数。但我不确定如何将这部分与SSIS中的上述实际查询混合。

DECLARE @CntOfRow INT;
select @CntOfRow = @@ROWCOUNT
--select @CntOfRow as 'Numrow'
if(@CntOfRow = 0)
begin
    if object_id ('tempdb..#Bus') is not null
    begin
        drop table #Bus
    end
    if object_id ('tempdb..#Bus') is null
    begin
        create table #Bus(
            Caption varchar(20), [Date] date, FleetType varchar(10), OperatingStatus varchar(25),
            Fleet_Range_0 int null, Fleet_Range_1 int null, [Fleet_Range_2-4] int null, [Fleet_Range_5-10] int null, TOTAL int null
        )
        insert into #Bus(Caption,[Date],FleetType,OperatingStatus, Fleet_Range_0,Fleet_Range_1,[Fleet_Range_2-4],[Fleet_Range_5-10],TOTAL)
        values('by Weight', CONVERT(DATE, DATEADD(D,-(DAY(@date)), @date)), 'Bus', 'ALL',0,0,0,0,0)
    end
    select * from #Bus
end

调查

我研究了Row CountConditional Split之类的SSIS组件,以便首先获取源查询之后的行数,然后应用条件拆分将检索到的结果进一步插入目标表中。但是在条件拆分之后,我不确定如何生成临时表并插入具有默认值的行以将其进一步插入到目标表中。

我还在探索控制流中的Execute SQL Task,以查看是否可以转移逻辑以在那里生成默认行,并将该行传递到DFT中以插入到目标表中。

非常感谢您对此情况的见识。

1 个答案:

答案 0 :(得分:0)

您可以在OLEDB源代码中使用复杂的SQL命令,尝试将数据插入到临时表中,然后检查临时表中是否不包含行,请插入默认值。最后,从临时表中选择所有行。

DECLARE @date DATE
SET @date = GETDATE();

with cte as
(
select
   T.mot_fleettypedescription AS 'FleetType'
   ,(CASE WHEN F.mot_averagefleetsize = 0 THEN COUNT(C.mot_nscweight) ELSE 0 END) AS 'Fleet_Range_0'
   ,(CASE WHEN F.mot_averagefleetsize = 1 THEN COUNT(C.mot_nscweight) ELSE 0 END) AS 'Fleet_Range_1'
   ,(CASE WHEN F.mot_averagefleetsize BETWEEN 2 AND 4 THEN COUNT(C.mot_nscweight) ELSE 0 END) AS 'Fleet_Range_2-4'
   ,(CASE WHEN F.mot_averagefleetsize BETWEEN 5 AND 10 THEN COUNT(C.mot_nscweight) ELSE 0 END) AS 'Fleet_Range_5-10'
   ,CONVERT(DATE, C.mot_monthenddate) AS 'MED'
from Filteredmot_carrierfleettypes F
left outer join Filteredmot_carrierfleettype T on F.mot_fleettype = T.mot_carrierfleettypeid
left outer join Filteredmot_fleetcount C on F.mot_carriernumber = C.mot_carriernumber
where F.statecode = 0 AND C.mot_monthenddate = CONVERT(DATE, DATEADD(D,-(DAY(@date)), @date))
group by F.mot_averagefleetsize, T.mot_fleettypedescription, C.mot_monthenddate
)
insert into #Bus(Caption,[Date],FleetType,OperatingStatus, Fleet_Range_0,Fleet_Range_1,[Fleet_Range_2-4],[Fleet_Range_5-10],TOTAL)
select 
    CONVERT(NVARCHAR(50), 'by Weight') AS 'Caption', cte.MED AS 'Date', CONVERT(NVARCHAR(10), cte.FleetType) AS 'FleetType',
    CONVERT(NVARCHAR(25), 'ALL') AS 'OperatingStatus',
    SUM(cte.Fleet_Range_0) As 'Fleet_Range_0', SUM(cte.Fleet_Range_1) As 'Fleet_Range_1', SUM(cte.[Fleet_Range_2-4]) 'Fleet_Range_2-4', 
    SUM(cte.[Fleet_Range_5-10]) 'Fleet_Range_5-10', 
    (SUM(cte.Fleet_Range_0) + SUM(cte.Fleet_Range_1) + SUM(cte.[Fleet_Range_2-4]) + SUM(cte.[Fleet_Range_5-10])) 'TOTAL'
from cte
where cte.FleetType = 'Bus'
group by cte.FleetType, cte.MED

DECLARE @Count INT
SELECT @Count = Count(*) FROM #Bus

If @Count = 0 
insert into #Bus(Caption,[Date],FleetType,OperatingStatus, Fleet_Range_0,Fleet_Range_1,[Fleet_Range_2-4],[Fleet_Range_5-10],TOTAL)
values('by Weight', CONVERT(DATE, DATEADD(D,-(DAY(@date)), @date)), 'Bus', 'ALL',0,0,0,0,0)

SELECT * FROM #Bus

如果它不起作用,请尝试在存储过程中实现它