我测试了这个查询,它是在另一个线程的帮助下构建的,它运行得很好,将它移动到一个存储过程中,我需要为它提供一个参数(EID的值)。因此,我对完美测试的查询所做的唯一更改是在顶部和底部添加@param1参数,在顶部添加创建过程行。
我无法保存它,因为它在第4行阻塞了'n'说明 - SQL80001:'n'不是公认的选项。
要走到这一步是漫长的道路。有人可以帮我把终点线推到这里吗?
CREATE PROCEDURE [dbo].[UpdateSubIdx]
@param1 varchar(30)
WITH n AS
(
SELECT DID AS DID,
sub_idx AS current_id,
ROW_NUMBER() OVER (PARTITION BY EID ORDER BY alt_sub_idx) AS new_id
FROM GETT_Documents
)
--SELECT * FROM N
UPDATE GETT_Documents
SET sub_idx = n.new_id
FROM GETT_Documents G
JOIN n ON N.DID = G.DID
WHERE EID = 'AC-1.1.i';
---Repeat for the alt sub idx column renumberation
WITH n AS
(
SELECT DID AS DID,
alt_sub_idx AS current_id,
ROW_NUMBER() OVER (PARTITION BY EID ORDER BY sub_idx)*10 AS new_id
FROM GETT_Documents
)
UPDATE GETT_Documents
SET alt_sub_idx = n.new_id
FROM GETT_Documents G
JOIN n ON N.DID = G.DID
WHERE EID = @param1;
此致 肯...
答案 0 :(得分:3)
您需要在;
关键字前面放置一个分号(WITH
)来终止前一个语句。
公用表格表达式必须以;
开头 - 有关详细信息,请参阅此处:Why should a CTE start with a semi-colon?
您还需要使用AS BEGIN ... END
来标记过程的正文。以下是应该起作用的变化:
CREATE PROCEDURE [dbo].[UpdateSubIdx] @param1 varchar(30)
As Begin
;WITH n AS
(
SELECT DID AS DID,
sub_idx AS current_id,
ROW_NUMBER() OVER (PARTITION BY EID ORDER BY alt_sub_idx) AS new_id
FROM GETT_Documents
)
--SELECT * FROM N
UPDATE GETT_Documents
SET sub_idx = n.new_id
FROM GETT_Documents G
JOIN n ON N.DID = G.DID
WHERE EID = 'AC-1.1.i';
---Repeat for the alt sub idx column renumberation
;WITH n AS
(
SELECT DID AS DID,
alt_sub_idx AS current_id,
ROW_NUMBER() OVER (PARTITION BY EID ORDER BY sub_idx)*10 AS new_id
FROM GETT_Documents
)
UPDATE GETT_Documents
SET alt_sub_idx = n.new_id
FROM GETT_Documents G
JOIN n ON N.DID = G.DID
WHERE EID = @param1;
End