आप ALTER VIEW
नहीं चला पाएंगे आपकी संग्रहीत प्रक्रिया में उस तरह का बयान। इसलिए अपनी समस्या का समाधान करने के लिए आपको 2 कदम उठाने होंगे:
1) वर्तमान में दिखाई देने वाली त्रुटि को ठीक करने के लिए आपको सीटीई को इस तरह के अर्धविराम से शुरू करना होगा:
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
(या इससे भी बेहतर अपने सभी SQL कथनों को अर्धविराम के साथ समाप्त करना शुरू करें क्योंकि विकल्प बहिष्कृत है)।
2) अपने परिवर्तन दृश्य कथन को एक गतिशील SQL स्ट्रिंग में बदलें और sp_executesql
. का उपयोग करके इसे निष्पादित करें ALTER VIEW
. के रूप में कथन बैच में पहला होना चाहिए:
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