我有3个CTE的查询。我很好奇如何将参数传递给它们。如果我在查询的顶部声明某些效果
声明@prioryear varchar(5) 设置@prioryear ='2014'
声明@currentyear varchar(5) 设置@prioryear ='2015'
然后我怎样才能将这些变量传递给CTE呢?我一直试图在CTE名称之后加入paren,但似乎没有什么想要通过。
谢谢, 乔
`
with paidPrior as (
SELECT DISTINCT des_heapplicationSet.des_PeopleId
FROM des_heapplicationSet INNER JOIN
des_zyearSet ON des_heapplicationSet.des_YearId = des_zyearSet.des_zyearId INNER JOIN
des_zapplicationstatusSet ON des_heapplicationSet.des_ApplicationStatusId = des_zapplicationstatusSet.des_zapplicationstatusId
WHERE (des_zyearSet.des_YearText = '2014') AND (des_zapplicationstatusSet.des_applicationstatustext = 'paid') and des_IsMaster =0
),
allCurrent as (
SELECT DISTINCT des_heapplicationSet.des_PeopleId
FROM des_heapplicationSet INNER JOIN
des_zyearSet ON des_heapplicationSet.des_YearId = des_zyearSet.des_zyearId
WHERE (des_zyearSet.des_YearText = '2015') and des_IsMaster =0
),
peopleSet as (
select distinct des_PeopleId, MAX(des_ApplicationNumber) as appNum
from des_heapplicationSet
where des_PeopleId in (select * from paidPrior) and des_PeopleId not in (select * from allCurrent)
group by des_PeopleId
)
SELECT des_zterritorySet.des_TerritoryText, des_hepeopleSet.des_PeopleNumber, des_hepeoplenameSet.des_FirstName, des_hepeoplenameSet.des_LastName, des_headdressmasterSet.des_AddressLine,
des_headdressmasterSet.des_FinalCity, des_headdressmasterSet.des_Zip5, des_headdressmasterSet.des_CountyName, 'WI' as stateOfWi, des_hepeoplephoneSet.des_PhoneNumber,
des_heapplicationSet.des_EmailAddress
FROM des_heapplicationSet INNER JOIN
des_hepeopleSet ON des_heapplicationSet.des_PeopleId = des_hepeopleSet.des_hepeopleId INNER JOIN
des_headdressmasterSet ON des_heapplicationSet.des_MailingAddressId = des_headdressmasterSet.des_headdressmasterId INNER JOIN
des_zterritorySet ON des_heapplicationSet.des_TerritoryId = des_zterritorySet.des_zterritoryId INNER JOIN
des_hepeoplenameSet ON des_heapplicationSet.des_PeopleNameId = des_hepeoplenameSet.des_hepeoplenameId LEFT OUTER JOIN
des_hepeoplephoneSet ON des_heapplicationSet.des_PeoplePrimaryPhoneId = des_hepeoplephoneSet.des_hepeoplephoneId
where des_ApplicationNumber in (select appnum from peopleSet) and des_TerritoryText = 'Adams'
order by des_TerritoryText
`
答案 0 :(得分:1)
你不能直接引用变量吗?将'2015'替换为CTE中的@currentyear