2017-02-11 100 views
1

我一直在试图找出什么我的beloe程序MySQL错误1064:在靠近1号线

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 

我试图执行如下的过程中做错了NULL,但有错误

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

错误代码:1064.您的SQL语法错误;检查对应于你的MySQL服务器版本的手册,在第1行使用接近'NULL'的正确语法

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

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

任何有助于解决这个问题认识

+1

您在所有参数前添加了@,如何删除它们? –

+0

嗨,当您在参数列表中使用'@ p_cont'时,您在查询中看到@ p_con'。 – mandar

+0

错字,纠正它 – RData

回答

1

只取@了你的变量是过程参数,因为萨米Kuhmonen暗示的评论。

存储过程中的局部变量在名称前没有@。如果你使用一个像@col_n这样的变量,它与col_n不同,它是你的程序变量的变量。

变量@被称为Session Variables。他们在你的存储过程之外有一个范围。您发现您可以在调用您的程序之前设置该值。同样,如果您在过程中设置了此变量的值,那么在您的过程返回后它仍将具有该值。

我在我的答案贴了演示在这里:https://stackoverflow.com/a/41925146/20860

不幸的是,有一个例外,每一个规则。当您使用PREPARE时,您的必须使用会话变量。局部变量对PREPARE不起作用。