MySQL MySQL删除会话变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24897504/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
MySQL Delete Session Variable
提问by Zon
SITUATION: MySQL query uses value, delivered from outside. This is done by declaring and initializing a session variable, that remains valid until the end of session:
情况:MySQL 查询使用从外部传递的值。这是通过声明和初始化会话变量来完成的,该变量在会话结束之前一直有效:
SET @id = 0;
SELECT * FROM my_table
WHERE id = @id;
QUESTION: Is it a good practice to delete session variable after the query has finished (for safety reasons)? What is the most adequate way to do this?
问题:在查询完成后(出于安全原因)删除会话变量是一个好习惯吗?做到这一点最合适的方法是什么?
STUDIES: I found the following suggestion, but have some doubts as it looks like "uninitialise", not like "undeclare":
研究:我发现了以下建议,但有一些疑问,因为它看起来像“未初始化”,而不是“未声明”:
SET @id = NULL;
回答by RandomSeed
Using session variables to pass parameters is a bad practice (using global variables often indicates code smell). Therefore there is no adequate or recommended way to deal with your situation(1).
使用会话变量传递参数是一种不好的做法(使用全局变量通常表示代码异味)。因此,没有足够或推荐的方法来处理您的情况(1)。
You commented:
你评论说:
Using name parameters is more flexible than place parameters using "?", because adding/removing a variable needs no order changes in code.
使用名称参数比使用“?”放置参数更灵活,因为添加/删除变量不需要更改代码顺序。
This is an illusion. Using this approach, you will still need to document "somewhere" that your procedure requires some variables be declared before calling. This also introduce the very problem you are trying to address.
这是一种错觉。使用这种方法,您仍然需要记录您的过程需要在调用之前声明一些变量的“某处”。这也引入了您试图解决的问题。
On the other hand, a stored procedure (or prepared statement) is self-documenting and parameters have a well-known scope and life span.
另一方面,存储过程(或准备好的语句)是自记录的,参数具有众所周知的范围和生命周期。
Now, to answer the specific question:
现在,回答具体问题:
How to delete/unset a session variable?
如何删除/取消设置会话变量?
SET @id = NULL;
is the only way to go. You cannot "undeclare" a session variable, since you cannot "declare" a session variable (try SELECT @dummy_variable_never_declared_before;
). MySQL session variables are very similar to shell environment variables.(2)
SET @id = NULL;
是唯一的出路。您不能“取消声明”会话变量,因为您不能“声明”会话变量(try SELECT @dummy_variable_never_declared_before;
)。MySQL 会话变量与 shell 环境变量非常相似。(2)
(1)... except if you make sure the session is closed after you are done. Closing the session will surely clear all session variables.
(1)...除非您确保会话在完成后关闭。关闭会话肯定会清除所有会话变量。
(2)I have learnt that bash session variables can indeed be unset.
(2)我了解到 bash 会话变量确实可以 unset。
回答by user1904991
I haven't read the manual or anything. But what I've found when using @-variables is that they disappear with the database connection. I guess that's what you call a session. In any case, reconnecting to the database seems to remove all variables.
If you use MySQL with a web server, this means that all your variables get deleted as soon as a page has been delivered. That is actually how I found this out. You cannot keep an SQL @-variable from one http call to another.
我没有阅读手册或任何东西。但是我在使用 @-variables 时发现它们随着数据库连接而消失。我想这就是你所说的会话。无论如何,重新连接到数据库似乎会删除所有变量。
如果您将 MySQL 与 Web 服务器一起使用,这意味着一旦交付页面,您的所有变量都会被删除。我就是这样发现的。您不能将 SQL @ 变量从一个 http 调用保留到另一个。