MySql存储过程的参数与影响列的名称相同,可以吗? [英] MySql Stored Procedure's parameter with the same name as affecting's column, is it possible?

查看:74
本文介绍了MySql存储过程的参数与影响列的名称相同,可以吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含两列(名称,值)的序列表,并且我有一个存储过程,用于在提供名称的情况下递增值

I have a sequence table with two columns, name, value, and I have a stored procedure for incrementing the value provided the name

DROP PROCEDURE IF EXISTS p_generate_sequence;
delimiter |
CREATE PROCEDURE p_generate_sequence (name VARCHAR(30))
BEGIN
    START TRANSACTION;
    -- Variable "name" equal to column "name", how to reference?
    UPDATE sequences_table SET value = value + 1 WHERE name = name;
    SELECT value FROM sequences_table WHERE name = name;
    COMMIT;
END |
delimiter ;

请注意,该参数称为名称".

Note the parameter is called "name".

是否可以使用与受影响表的列名称同名的参数?

Is there any approach for using a parameter with the same name as the column name of the affected table?

注意::我对更改参数的名称甚至列的名称都不感兴趣,只是想知道是否可能以及如何进行.

NOTE: I'm not interested in change parameter's name, nor even column's name, just to know whether is it possible or not, and how.

推荐答案

是的,请使用别名将表列的范围设置为

Yes, scope your table column by an alias.

例如

delimiter //
create procedure foo( id int )
begin
 select * from users u where u.id = id;
end
//

call foo( 123 )

返回用户ID = 123

returns user id = 123

这篇关于MySql存储过程的参数与影响列的名称相同,可以吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆