我正在将一些查询滚动到存储过程中并且遇到了#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`
我想我忽略了一些明显的东西?
我在两个方面都试过这个:
答案 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
指定为放置数据的变量。