I have a stored procedure in which I have a table shows all working days starting from tomorrow to the last date in my records, it works fine, but the problem is I can't have access to it in my SP, I need to make it global and just call it, here is my table contains all the working days:
DECLARE @StartDate AS DATE = current_timestamp
, @EndDate AS DATE = (select max(ASSYDAY.ASSYDATE) from ASSYDAY)
;WITH dateCTE AS
(
SELECT @StartDate StartDate, DATEPART(WEEKDAY,@StartDate) wkday
UNION ALL
SELECT DATEADD(day,1,StartDate) , DATEPART(WEEKDAY, DATEADD(day,1,StartDate)) wkday
FROM dateCTE
WHERE DATEADD(day,1,StartDate) <= @EndDate
)
SELECT StartDate as workingDays
FROM dateCTE
WHERE wkday NOT IN(1,7) -- Execluding Sat, Sun
option (maxrecursion 0)
and here I need just to say if the dates are in WorkingDays:
select wrkTransport.WORKSHOPID from WORKSHOPTRANSPORT wrkTransport
join AddCapacityToCalenders addCapa
on wrkTransport.FACTORYID=addCapa.FACTORYID ) and wrk.WORKSHOPGROUPID is not null)
and wrkCalender.DAY>=(select MIN(ASSYDAY) from ASSYDAY where ASSYDATE in (**workingDays**))
But I get invalid column name, how can I make it global and have access from all parts of my SP? Thanks a lot in advance
1条答案
按热度按时间dxxyhpgq1#
When you make a CTE it is completed, with the code below you get an temporary table that you can use
or you make another CTE and use that like