数据库中的 SQL 切换变量
我的数据库中有一个表的过程,但它似乎没有达到我想要的效果。有人可以检查我是否做错了吗?我对 SQL 都是新手。
这是我的程序。 当我告诉它时,它应该将管理状态从 1 更改为 0,并从 0 更改为 1。但由于某种原因它改变了它并且不会改变它回来:(
ALTER PROCEDURE [sesuser].[Login_Admin_Toggle]
(
@loginID INT
)
AS
BEGIN
DECLARE @tmpId INTEGER;
SET @tmpId = (SELECT SESAdmin FROM [ses_users].[sesuser].[SESLogin] WHERE SESLoginID = @loginID);
IF (@tmpId = 1)
BEGIN
SET @tmpId = 0
RETURN
END
ELSE
SET @tmpId = 1
UPDATE [ses_users].[sesuser].[SESLogin]
SET SESAdmin = @tmpId
WHERE SESLoginID = @loginID
END
I have a procedure for a table in my database, but it doesn't seem to do what I want it to. Could someone check if I am doing it wrong? I am new to SQL all together.
This is my procedure.
It is supposed to change admin status from 1 to 0 and from 0 to 1 when I tell it to. But for some reason it changes it and doesn't change it back :(
ALTER PROCEDURE [sesuser].[Login_Admin_Toggle]
(
@loginID INT
)
AS
BEGIN
DECLARE @tmpId INTEGER;
SET @tmpId = (SELECT SESAdmin FROM [ses_users].[sesuser].[SESLogin] WHERE SESLoginID = @loginID);
IF (@tmpId = 1)
BEGIN
SET @tmpId = 0
RETURN
END
ELSE
SET @tmpId = 1
UPDATE [ses_users].[sesuser].[SESLogin]
SET SESAdmin = @tmpId
WHERE SESLoginID = @loginID
END
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
亚历克斯·K 是完全正确的。您将变量设置为 0,然后返回,这意味着您永远不会从那里到达 UPDATE 语句。
但是,稍微偏离一下,你实际上可以在一个声明中完成所有这些......
Alex K is completely right. You set your variable to 0 then RETURN, meaning that you never get to the UPDATE statement from there.
But, on a slight tangent, you can actually do all of that in one statement...
它不会切换回来,因为您
RETURN
,因此如果@tmpId = 1
,则停止执行,因此它永远不会更新为0
。It won't toggle back because you
RETURN
and so halt execution if@tmpId = 1
, so its never updated to0
.