这不是一个非常复杂的问题,我只是在遇到其他问题时遇到了同样的问题。
我需要一个存储过程,该过程将更新表的“状态”,而不是删除它。
CREATE PROCEDURE sp_delete_user
@id INTEGER
AS
UPDATE USER
IF (state = 0)
SET state = 1
ELSE
SET state = 0
WHERE id = @id
GO
但是,这会引发错误...
我也尝试过:
CREATE PROCEDURE sp_delete_user
@id INTEGER
AS
IF state = 1
BEGIN
UPDATE user
SET state = 0
WHERE id = @id
END
ELSE
BEGIN
UPDATE user
SET state = 1
WHERE id = @id
END
GO
但是我在“状态”上看到一个错误,说列名无效...
答案 0 :(得分:3)
尝试一下:
CREATE PROCEDURE flip_user_state
@user_id int
AS
UPDATE Users
SET state = CASE WHEN state = 1 THEN 0 ELSE 1 END
WHERE user_id = @user_id