Nebudete moci spustit ALTER VIEW
podobný příkaz ve vaší uložené proceduře. Takže k vyřešení vašeho problému musíte provést 2 akce:
1) Chcete-li opravit chybu, která se aktuálně objevuje, musíte začít CTE středníkem, jako je tento:
WITH cte as (
SELECT *,
LAG([pointsRewarded], 1) OVER (PARTITION BY [EmployeeID] ORDER BY [WeekNumber]) as prev1_points,
LAG([pointsRewarded], 2) OVER (PARTITION BY [EmployeeID] ORDER BY [WeekNumber]) as prev2_points,
LAG([pointsRewarded], 3) OVER (PARTITION BY [EmployeeID] ORDER BY [WeekNumber]) as prev3_points
FROM week1)
SELECT *,
CASE WHEN [pointsRewarded] = -10 AND prev1_points = -10 AND prev2_points = -10 AND prev3_points = -10
THEN -200
WHEN [pointsRewarded] = -10 AND prev1_points = -10 AND prev2_points = -10
THEN -100
WHEN [pointsRewarded] = -10 AND prev1_points = -10
THEN -50
ELSE 0
END penalty
FROM cte
(Nebo ještě lépe začněte ukončovat všechny své příkazy SQL středníkem, protože alternativa je zastaralá).
2) Převeďte svůj příkaz alter view do dynamického řetězce SQL a spusťte jej pomocí sp_executesql
jako ALTER VIEW
příkaz musí být první v dávce:
CREATE PROCEDURE createviewupdatepenaltypointsconsecutive
AS
BEGIN
DECLARE @STMT AS NVARCHAR(MAX) =
'
ALTER VIEW consecutive
AS
WITH cte as (
SELECT *,
LAG([pointsRewarded], 1) OVER (PARTITION BY [EmployeeID] ORDER BY [WeekNumber]) as prev1_points,
LAG([pointsRewarded], 2) OVER (PARTITION BY [EmployeeID] ORDER BY [WeekNumber]) as prev2_points,
LAG([pointsRewarded], 3) OVER (PARTITION BY [EmployeeID] ORDER BY [WeekNumber]) as prev3_points
FROM week1
)
SELECT *,
CASE WHEN [pointsRewarded] = -10 AND prev1_points = -10 AND prev2_points = -10 AND prev3_points = -10
THEN -200
WHEN [pointsRewarded] = -10 AND prev1_points = -10 AND prev2_points = -10
THEN -100
WHEN [pointsRewarded] = -10 AND prev1_points = -10
THEN -50
ELSE 0
END penalty
FROM cte
'
EXEC sp_executesql @STMT;
END