显示的代码没有递增1,所有记录都显示相同的字符串值。
DECLARE @q_id NVARCHAR(50)
begin
SET @q_id='JEE_PHY_1'
UPDATE oems_question7 SET q_id = SUBSTRING (@q_id ,0,CHARINDEX('_', @q_id,5) + 1 ) + ' ' + CONVERT(NVARCHAR,CONVERT(INT, SUBSTRING ( @q_id ,CHARINDEX('_', @q_id,5) + 1, LEN(@q_id) )) +1)
WHERE id between 1 and 40
end
答案 0 :(得分:1)
使用您的代码,您将使用完全相同的值更新所有行。您应该应用ROW_NUMBER为每一行提供一个不同的增加数字,然后更新行。
DECLARE @q_id NVARCHAR(50)
begin
SET @q_id='JEE_PHY_1'
;WITH UpdateCTE AS
(
SELECT q_id,
ROW_NUMBER() OVER(ORDER BY q_id) AS RowNum
FROM oems_question7
WHERE id between 1 and 40
)
UPDATE UpdateCTE
SET q_id = SUBSTRING (@q_id ,0,CHARINDEX('_', @q_id,5) + 1 ) + ' ' + CONVERT(NVARCHAR,RowNum)
end