MySQL:在存储过程中选择多个字段到多个变量中
我可以在 MySQL 的同一个选择查询中将多个列选择到多个变量中吗?
例如:
DECLARE iId INT(20);
DECLARE dCreate DATETIME;
SELECT Id INTO iId, dateCreated INTO dCreate
FROM products
WHERE pName=iName;
正确的语法是什么?
Can I SELECT multiple columns into multiple variables within the same select query in 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的语法不太正确:您需要按顺序列出 INTO 前面的字段,后面列出相应的目标变量:
Your syntax isn't quite right: you need to list the fields in order before the INTO, and the corresponding target variables after:
==========建议==========
@martinclayton 答案是正确的,但这只是一个建议。
请避免在存储过程中使用不明确的变量。
示例:
上面的示例将导致错误(空值错误)
下面给出的示例是正确的。我希望这是有道理的。
示例:
您还可以通过引用表格来使它们明确,例如:
[ Credit : maganap ]
==========Advise==========
@martin clayton Answer is correct, But this is an advise only.
Please avoid the use of ambiguous variable in the stored procedure.
Example :
The above example will cause an error (null value error)
Example give below is correct. I hope this make sense.
Example :
You can also make them unambiguous by referencing the table, like:
[ Credit : maganap ]
除了 Martin 的答案之外,您还可以在查询末尾添加 INTO 部分以使查询更具可读性:
Alternatively to Martin's answer, you could also add the INTO part at the end of the query to make the query more readable: