在存储过程中更改视图

时间:2016-11-03 10:10:45

标签: sql sql-server sql-server-2008 sql-server-2005 sql-server-2012

我们如何在存储过程中更改视图?

create procedure createviewupdatepenaltypointsconsecutive
as
begin
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

end

M收到此错误: 消息156,级别15,状态1,过程createviewupdatepenaltypointsconsecutive,第4行 关键字' VIEW'附近的语法不正确。 消息319,级别15,状态1,过程createviewupdatepenaltypointsconsecutive,第8行 关键字'与'附近的语法不正确。如果此语句是公用表表达式,xmlnamespaces子句或更改跟踪上下文子句,则必须以分号结束前一个语句。

1 个答案:

答案 0 :(得分:1)

您将无法在存储过程中运行http://uvw.mydomain.com:8091/myApp语句。因此,要解决您的问题,您必须采取以下两种措施:

1)要纠正当前出现的错误,您必须使用分号开始CTE:

ALTER VIEW

(或者甚至更好地开始用分号终止所有SQL语句,因为不推荐使用替代方法)。

2)在动态SQL字符串中转换alter view语句并使用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 执行它,因为sp_executesql语句必须是批处理中的第一个:

ALTER VIEW