【问题标题】:MySQL Error 1064 : near NULL at line 1MySQL 错误 1064:在第 1 行接近 NULL
【发布时间】:2017-02-11 05:44:11
【问题描述】:

我一直在试图弄清楚我在 beloe 程序中做错了什么

CREATE  PROCEDURE `example`(IN col_n char(50),IN p_cont char(50),IN p_ud int)
BEGIN

set @S = CONCAT('select rn.emp_id as     emp_id,controller,perma,permb,permc,permd,order from emp n
join resource_emp rn on rn.emp_id = n.emp_id 
join resource r on r.resource_id = rn.resource_id 
join u_resource ur on ur.resource_id = r.resource_id 
join user u on u.u_id =  ur.ur_id
join( 
select title,min(order) as pr from emp n
join resource_emp rn on rn.emp_id = n.emp_id 
join resource r on r.resource_id = rn.resource_id 
join u_resource ur on ur.resource_id = r.resource_id 
join user u on u.u_id =  ur.ur_id
where u.u_id = ',@p_ud,' group by title)a on a.title = n.title and a.pr = order
where u.u_id = ',@p_ud,' n.con = ''',@p_cont,''' and ,@col_n,' = 1
group by n.title order by n.order');

PREPARE stmt1 FROM @S;

EXECUTE stmt1;

DEALLOCATE PREPARE stmt1;
END

我正在尝试执行以下过程但出现错误

call hop_thlc_t.auth('perma', 'submit', 2);

错误代码:1064。您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以在第 1 行的“NULL”附近使用正确的语法

虽然发生这种情况,但我仍然可以通过使用以下参数将上述过程作为查询运行并获得所需的结果

 set @col_n ='perma';
 set @p_cont = 'submit' ;
 set @p_ud =2; 

感谢任何解决问题的帮助

【问题讨论】:

  • 你在所有参数前添加了@,删除它们怎么样?
  • 嗨,当您在参数列表中使用 @p_cont 时,我在您的查询中看到了 @p_con
  • 错字,已更正

标签: mysql stored-procedures mysql-workbench


【解决方案1】:

正如 Sami Kuhmonen 在评论中建议的那样,只需从作为过程参数的变量中删除 @

存储过程中的局部变量在名称前没有@。如果您使用像 @col_n 这样的变量,它与 col_n 的变量不同,后者是您的过程参数的变量。

带有@ 的变量称为Session Variables。它们在您的存储过程之外有一个范围。您发现可以在调用过程之前设置值。同样,如果你在过程中设置了这个变量的值,在你的过程返回后它仍然会有那个值。

我在这里的回答中发布了一个演示:https://stackoverflow.com/a/41925146/20860

很遗憾,每条规则都有一个例外。当您使用PREPARE 时,您必须使用会话变量。局部变量不适用于PREPARE

【讨论】:

    猜你喜欢
    • 2021-10-05
    • 2015-01-26
    • 2019-03-18
    • 2023-02-14
    • 2012-02-12
    • 1970-01-01
    • 2021-11-23
    • 1970-01-01
    • 2018-12-28
    相关资源
    最近更新 更多