MySQL:在存储过程中将多个字段选择为多个变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2450529/
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: Selecting multiple fields into multiple variables in a stored procedure
提问by aHunter
Can I SELECT multiple columns into multiple variables within the same select query in MySQL?
我可以在 MySQL 的同一个选择查询中将多个列选择到多个变量中吗?
For example:
例如:
DECLARE iId INT(20);
DECLARE dCreate DATETIME;
SELECT Id INTO iId, dateCreated INTO dCreate
FROM products
WHERE pName=iName;
What is the correct syntax for this?
什么是正确的语法?
回答by martin clayton
回答by sijo vijayan
==========Advise==========
==========建议==========
@martin clayton Answer is correct, But this is an advise only.
@martin Clayton 答案是正确的,但这只是一个建议。
Please avoid the use of ambiguous variable in the stored procedure.
请避免在存储过程中使用不明确的变量。
Example :
例子 :
SELECT Id, dateCreated
INTO id, datecreated
FROM products
WHERE pName = iName
The above example will cause an error (null value error)
上面的例子会导致错误(空值错误)
Example give below is correct. I hope this make sense.
下面给出的例子是正确的。我希望这是有道理的。
Example :
例子 :
SELECT Id, dateCreated
INTO val_id, val_datecreated
FROM products
WHERE pName = iName
You can also make them unambiguous by referencing the table, like:
您还可以通过引用表使它们明确,例如:
[ Credit : maganap]
[信用:maganap]
SELECT p.Id, p.dateCreated INTO id, datecreated FROM products p
WHERE pName = iName
回答by ibai
Alternatively to Martin's answer, you could also add the INTO part at the end of the query to make the query more readable:
除了 Martin 的回答,您还可以在查询的末尾添加 INTO 部分以使查询更具可读性:
SELECT Id, dateCreated FROM products INTO iId, dCreate