MySQL 存储过程和标识符
MySQL Stored Procedure and Identifiers
我正在学习编写MySQL存储过程,遇到了一些困难。这里我有两个存储过程:
第一个存储过程:
CREATE PROCEDURE sp1 (IN `username` TEXT, OUT `user_id` INT)
BEGIN
DECLARE rowcount INT;
SELECT count(`User ID`) INTO rowcount FROM user WHERE `Username`=username;
SET user_id = rowcount;
END|
第二个存储过程:
CREATE PROCEDURE sp2 (IN `doc_id` INT, IN `content` LONGTEXT)
BEGIN
UPDATE doc SET `Content`=content WHERE `Doc ID`=doc_id;
END|
(分隔符是|
。)
问题:
我观察到第一个存储过程的结果和调用SELECT count(`User ID`) FROM user;
是一样的。但是,第二个存储过程完成了它的工作并使用新内容更新了内容。
那么为什么第一个存储过程将`Username`
和username
视为相同的标识符,而第二个存储过程将`Content`
和content
视为不同的标识符?两种情况下的两个标识符除了首字母大写外是相同的。
我在阅读关于 the scope of local variables 的官方 MySQL 文档后弄明白了。
它指出:
A local variable should not have the same name as a table column. If an SQL statement, such as a SELECT ... INTO statement, contains a reference to a column and a declared local variable with the same name, MySQL currently interprets the reference as the name of a variable.
我正在学习编写MySQL存储过程,遇到了一些困难。这里我有两个存储过程:
第一个存储过程:
CREATE PROCEDURE sp1 (IN `username` TEXT, OUT `user_id` INT)
BEGIN
DECLARE rowcount INT;
SELECT count(`User ID`) INTO rowcount FROM user WHERE `Username`=username;
SET user_id = rowcount;
END|
第二个存储过程:
CREATE PROCEDURE sp2 (IN `doc_id` INT, IN `content` LONGTEXT)
BEGIN
UPDATE doc SET `Content`=content WHERE `Doc ID`=doc_id;
END|
(分隔符是|
。)
问题:
我观察到第一个存储过程的结果和调用SELECT count(`User ID`) FROM user;
是一样的。但是,第二个存储过程完成了它的工作并使用新内容更新了内容。
那么为什么第一个存储过程将`Username`
和username
视为相同的标识符,而第二个存储过程将`Content`
和content
视为不同的标识符?两种情况下的两个标识符除了首字母大写外是相同的。
我在阅读关于 the scope of local variables 的官方 MySQL 文档后弄明白了。
它指出:
A local variable should not have the same name as a table column. If an SQL statement, such as a SELECT ... INTO statement, contains a reference to a column and a declared local variable with the same name, MySQL currently interprets the reference as the name of a variable.