我正在尝试编写一个存储过程来检查某个值并返回错误或继续更新语句。我已经尝试编写 proc 并且一切似乎在语法上都是正确的,但是它似乎只检查某个值,当没有找到它时,它似乎只是跳过了需要发生的更新。我当前的代码如下:
declare @newID varchar = 099393
declare @oldID varchar = 260260
Declare @pMsg varchar(255)
if exists (select * from req where dept_id=@oldID)
begin
SET @pMsg = 'The department ID ' + @oldID + ' cannot be changed at this time.'
return
end
Begin Try
Begin TRAN
update dbo.dept
set dept_id = @newID
where dept_id = @oldID
COMMIT TRAN
END TRY
BEGIN CATCH
ROLLBACK TRAN
update dbo.dept
set dept_id = @oldID
where dept_id = @oldID
END CATCH
select dept_id=@newID
return