I want to use parameters in the below cte but I get the below error messages
- Msg 102, Level 15, State 1, Procedure X82 7 Days/by Day, Line 5 Incorrect syntax near '('.
- Msg 137, Level 15, State 2, Procedure X82 7 Days/by Day, Line 21 Must declare the scalar variable "@startDate".
Once I can get the perameters to work I will turn the static pivot into a dynamic one
ALTER Procedure [dbo].[X82 7 Days/by Day]
(
@StartDate As datetime = DATEADD(DAY, DATEDIFF(DAY, 0, getdate()-8), 0)
,@EndDate As datetime = DATEADD(DAY, DATEDIFF(DAY, 0, @startdate -1), 0)
)
As
Begin
;with cte as(
select [CAB Part No.]
,[RFT/Scrap/Rework]
,[Process No.]
,[Process]
,[Date & Time]
,[Quantity]
,cast([Date & Time] as date) UtcDay
from [SFDLOG]
where [Date & Time] between @startDate and @EndDate
)
select [CAB Part No.]
,[RFT/Scrap/Rework]
,[Process No.]
,[Process]
,[Quantity]
,UtcDay
into #TempDates
from cte
select *
from
(
select [Quantity]
,[utcday]
,[CAB Part No.]
,[Process No.]
,[Process]
,[RFT/Scrap/Rework]
from #tempDates
) x
pivot
(
count([Quantity])
for [utcDay]
In ([2015-02-01],[2015-02-02],[2015-02-03],[2015-02-04],[2015-02-05],[2015-02-06],[2015-02-07])
)p
Order By [CAB Part No.],[Process No.]
drop table #tempdates
End