简体   繁体   English

MySQL:无法从SELECT语句中调用函数

[英]MySQL: Unable to call function from within SELECT statement

I have created a function called removeYear in SQL. 我在SQL中创建了一个称为removeYear的函数。 The function is defined as: 该函数定义为:

CREATE FUNCTION removeYear(passedTitle CHAR(255), passedYear CHAR(100))
RETURNS CHAR(255) DETERMINISTIC

The function works properly. 该功能正常工作。 The problem is that I am unable to call it from within a SELECT statement using one of my column names. 问题是我无法使用我的列名之一在SELECT语句中调用它。 For example: 例如:

SELECT movieid, title
FROM movies
WHERE movieid = 40;

Works. 作品。

SELECT movieid, title, removeYear("bluh", movies.year)
FROM movies
WHERE movieid = 40;

Works. 作品。

SELECT movieid, title, removeYear(movies.title, movies.year)
FROM movies
WHERE movieid = 40;

Gives the following error: Error Code: 1054 Unknown column 'title' in 'field list' . 给出以下错误: 错误代码:1054“字段列表”中的未知列“标题”

I have simply no idea what is causing this error. 我根本知道是什么导致了此错误。 movies.title is a valid column. films.title是有效的列。 I have tried calling the function with every other column in my table, and those all work fine. 我尝试用表中的所有其他列调用该函数,所有这些都可以正常工作。 For example: 例如:

SELECT movieid, title, removeYear(movies.movieid, movies.year)
FROM movies
WHERE movieid = 40;

Executes just fine. 执行得很好。

EDIT: In my frustration, I decided to go over the 100+ lines of code comprising the function for the umpteenth time, finally spotting the single word in the code that was causing the error. 编辑:令我沮丧的是,我决定无数次遍历包含该函数的代码的100多行,最后在代码中发现引起错误的单个单词。

Just a single time in the entire block of code, I had accidentally written 在整个代码块中,只有一次,我不小心编写了

title

Instead of 代替

passedTitle

Whilst my problem is fixed, this still begs the question: 虽然我的问题已解决,但这仍然是一个问题:

Does an SQL function share a namespace with the SELECT statement calling it? SQL函数是否与调用它的SELECT语句共享一个名称空间?

Does an SQL function share a namespace with the SELECT statement calling it? SQL函数是否与调用它的SELECT语句共享一个名称空间?

As per your function signature definition 根据您的功能签名定义

CREATE FUNCTION removeYear(passedTitle CHAR(255), passedYear CHAR(100))
RETURNS CHAR(255) DETERMINISTIC

Value of first parameter is always identified within the function body using its name passedTitle only. 第一个参数的值始终仅在函数体内使用其名称passedTitle

And this is applicable to any parameter or variable declared defined within any stored procedure. 这适用于在任何存储过程中声明的任何参数或变量。

And hence if you use title as name for the first parameter over its defined name passedTitle in the signature, engine won't recognise it runtime and throws an error. 因此,如果您使用title作为第一个参数的名称,而不是其签名中已定义的名称passedTitle ,则引擎将无法识别它的运行时并抛出错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM