在SSIS中多次执行SQL任务中使用变量参数

时间:2018-11-09 18:29:53

标签: sql sql-server ssis ssis-2012

伙计们,所以我创建了两个变量:startdate和todate,我试图通过SSIS中的SQL查询传递它们,并在参数映射中添加了它们,但是SSIS在看到第三个变量后如何使用该变量问号?

例如:那么SSIS如何在插入#multileg中而不使用todate变量时知道使用startdate?

e 
--Declare @StartDate date
--declare @ToDate date

--set @startdate = dateadd(dd,-10, cast(getdate() as date))
--set @ToDate =  dateadd(dd,-9,cast(getdate() as date))





---SSR Table with passenger info, both APAY and PET
create table #SSRData

([ssrfl] int, [ssrcode] char(4), [ssrsequenceid] smallint, [ssrstatuscode] 
char(2), [servicestartcitycode] varchar(5), 
[ssrstartdate] date, [databasetimestamp] datetime, [pnrlocator] char(8), 
[pnrcreatedate] date, [passengersequenceid] smallint, 
[namefirst] varchar(250), [namelast] varchar(250), [frequenttravelernumber] 
varchar(25)


  )


  insert into #ssrdata
  select distinct ssrfl,
  s.ssrcode,s.ssrsequenceid,s.ssrstatuscode,s.servicestartcitycode,
  s.ssrstartdate, s.databasetimestamp, s.pnrlocator, s.pnrcreatedate
 ,s.passengersequenceid, namefirst, namelast,frequenttravelernumber
  --into #SSRData
  from 
  (select cast(ssrflightnumber as int)ssrfl,
  ssrcode,ssrsequenceid,ssrstatuscode,servicestartcitycode,
  ssrstartdate, pnrlocator, pnrcreatedate
 ,passengersequenceid,databasetimestamp from dwjetblue2.dw.resssr 
  where SSRCode in ('APAY', 'PETC') and PNRLocator <>'purged'
   and ssrstartdate  >= ? 
  and ssrstartdate <  ?)s
   inner join dw.dw.ResPassenger p 
   on p.pnrcreatedate=s.pnrcreatedate
 and p.pnrlocator=s.pnrlocator
 and p.passengersequenceid=s.passengersequenceid
 inner join dwjetblue2.dw.ResPassengerFT ft
 on ft.pnrcreatedate=s.pnrcreatedate
 and ft.pnrlocator=s.pnrlocator
 and ft.passengersequenceid=s.passengersequenceid

 --MultiLeg
  create table #multi
   (
  [pnrlocator] char(8), [pnrcreatedate] date 
  ,[segmentnumber] tinyint, [marketingflightnumber] char(5)
  ,[servicestartcity] char(3), [serviceendcity] char(3)
  ,[servicestartdate] date

   )
    insert into #multi

   select distinct
  pnrlocator p, pnrcreatedate d ,segmentnumber s,
  marketingflightnumber fl,
  servicestartcity sc, serviceendcity ec, servicestartdate sd
  --into #multi
  from  dw2.dw.resflight
  where servicestartdate >= ?

2 个答案:

答案 0 :(得分:1)

您不会在代码的顶部声明传递日期的变量,并从参数/中设置值?仔细了解这些内容,然后在下面的代码中使用变量即可

DECLARE @StartDate as Date
DECLARE @ToDate as DATE

SET @StartDate = ?
SET @ToDate = ?

-- so do it like this:
Select * from your table where MyDateColumn Between @StartDate and @ToDate

然后在您需要的代码下方使用它们。

这使您可以多次使用变量,并且更干净,更容易看到您的使用参数以及操作方式/位置。

***更新为使用您的代码:

DECLARE @StartDate as Date
DECLARE @ToDate as DATE

SET @StartDate = ?
SET @ToDate = ?

---SSR Table with passenger info, both APAY and PET
create table #SSRData

([ssrfl] int, [ssrcode] char(4), [ssrsequenceid] smallint, [ssrstatuscode] 
char(2), [servicestartcitycode] varchar(5), 
[ssrstartdate] date, [databasetimestamp] datetime, [pnrlocator] char(8), 
[pnrcreatedate] date, [passengersequenceid] smallint, 
[namefirst] varchar(250), [namelast] varchar(250), [frequenttravelernumber] 
varchar(25)


  )


  insert into #ssrdata
  select distinct ssrfl,
  s.ssrcode,s.ssrsequenceid,s.ssrstatuscode,s.servicestartcitycode,
  s.ssrstartdate, s.databasetimestamp, s.pnrlocator, s.pnrcreatedate
 ,s.passengersequenceid, namefirst, namelast,frequenttravelernumber
  --into #SSRData
  from 
  (select cast(ssrflightnumber as int)ssrfl,
  ssrcode,ssrsequenceid,ssrstatuscode,servicestartcitycode,
  ssrstartdate, pnrlocator, pnrcreatedate
 ,passengersequenceid,databasetimestamp from dwjetblue2.dw.resssr 
  where SSRCode in ('APAY', 'PETC') and PNRLocator <>'purged'
   and ssrstartdate  >= @StartDate --? 
  and ssrstartdate <  @ToDate)s --?
   inner join dw.dw.ResPassenger p 
   on p.pnrcreatedate=s.pnrcreatedate
 and p.pnrlocator=s.pnrlocator
 and p.passengersequenceid=s.passengersequenceid
 inner join dwjetblue2.dw.ResPassengerFT ft
 on ft.pnrcreatedate=s.pnrcreatedate
 and ft.pnrlocator=s.pnrlocator
 and ft.passengersequenceid=s.passengersequenceid

 --MultiLeg
  create table #multi
   (
  [pnrlocator] char(8), [pnrcreatedate] date 
  ,[segmentnumber] tinyint, [marketingflightnumber] char(5)
  ,[servicestartcity] char(3), [serviceendcity] char(3)
  ,[servicestartdate] date

   )
    insert into #multi

   select distinct
  pnrlocator p, pnrcreatedate d ,segmentnumber s,
  marketingflightnumber fl,
  servicestartcity sc, serviceendcity ec, servicestartdate sd
  --into #multi
  from  dw2.dw.resflight
  where servicestartdate >= @StartDate

答案 1 :(得分:0)

布拉德的答案是做到这一点的好方法。另一种方法是再次将参数简单地添加到“参数映射”中。

parameter mapping

第三种方法是在带有表达式的变量中构建SQL语句。然后在“执行SQL任务”中,SQLSourceType将为“变量”,然后选择包含查询的变量。这可能是一种简单的方法,因此可以避免为参数选择正确的数据类型而造成麻烦。