#1327 - 未声明的变量:table_name

时间:2014-03-21 15:31:33

标签: mysql mysql-error-1327

我正在将一些查询滚动到存储过程中并且遇到了#1327 - Undeclared variable错误......但奇怪的是,它声称未声明的变量实际上是表名


解决问题

所以我提取了它崩溃的程序位,并尝试通过PHPMyAdmin直接在数据库上运行它作为正常的SQL查询......同样的事情。经过多次修修补补后,我似乎正在加入另一张桌子。

如果我在一个表上运行查询,那很好,就像这样:

SET @i_channel_id = 3;
SET @i_product_id = 90;

SELECT 
`product_status_to_channel`.`status_code` INTO @s_status_code

FROM `product_status_to_channel`

WHERE `product_status_to_channel`.`channel_id` = @i_channel_id
AND `product_status_to_channel`.`product_id` = @i_product_id

ORDER BY IF(`product_status_to_channel`.`date` IS NULL, 1, 0) ASC, 
    `product_status_to_channel`.`date` DESC

LIMIT 0, 1;

SELECT @s_status_code AS status_code;


哪个输出'LIVE'作为PHPMyAdmin中的status_code - 这很好。


但是,当我尝试JOIN到消息表以查找关联的状态消息时,我收到错误:#1327 - Undeclared variable: product_status_to_channel_lang ...但是product_status_to_channel_lang是一个表?!< / p>

SET @i_channel_id = 3;
SET @i_language_id = 3;
SET @i_product_id = 90;

SELECT 
`product_status_to_channel`.`status_code` INTO @s_status_code,
`product_status_to_channel_lang`.`string` INTO @s_status_message

FROM `product_status_to_channel`

LEFT JOIN `product_status_to_channel_lang`
    ON `product_status_to_channel`.`product_status_to_channel_id` = `product_status_to_channel_lang`.`product_status_to_channel_id`
    AND `product_status_to_channel_lang`.`language_id` = @i_language_id

WHERE `product_status_to_channel`.`channel_id` = @i_channel_id
AND `product_status_to_channel`.`product_id` = @i_product_id

ORDER BY IF(`product_status_to_channel`.`date` IS NULL, 1, 0) ASC, `product_status_to_channel`.`date` DESC

LIMIT 0, 1;

SELECT @s_status_code AS status_code, @s_status_message AS status_message;


它是否试图将product_status_to_channel_lang.product_status_to_channel_id评估为JOIN上的变量?

LEFT JOIN `product_status_to_channel_lang`
    ON `product_status_to_channel`.`product_status_to_channel_id` = `product_status_to_channel_lang`.`product_status_to_channel_id`


我想我忽略了一些明显的东西?

我在两个方面都试过这个:

  • 使用MySQL 5.5.27运行xampp的Win7盒--MySQL社区服务器(GPL)
  • 运行MySQL的Debian盒子5.1.73-1-log - (Debian)

1 个答案:

答案 0 :(得分:8)

没关系 - 这是显而易见的事情:

SELECT 
`product_status_to_channel`.`status_code` INTO @s_status_code,
`product_status_to_channel_lang`.`string` INTO @s_status_message

应该是:

SELECT 
`product_status_to_channel`.`status_code`, 
`product_status_to_channel_lang`.`string` 

INTO 
@s_status_code, 
@s_status_message

......一定是星期五,花了几个小时就看到了。


... INTO @s_status_code,
`product_status_to_channel_lang`.`string` ...

^这就是它试图将product_status_to_channel_lang指定为放置数据的变量。