简体   繁体   English

在存储过程中将字段存储在变量中

[英]Storing a field in a variable in stored procedure

DELIMITER $$

DROP PROCEDURE IF EXISTS `ecview_1_10_siruseri`.`test` $$
CREATE DEFINER=`super`@`%` PROCEDURE `test`(In MeterThreadName varchar(30))
BEGIN

Declare MeterThreadId int;

select last_processed_time,meterthreadid=meter_thread_id from meter_thread where name =MeterThreadName;


select ems_device_name, meter_parameter_type_map_id from ems_device e
join meter_parameter_type_map mp on e.meter_id = mp.meter_id and mp.meter_thread_id = meterThreadID;


END $$

DELIMITER ;

I am trying to store the field in a variable meterthreadid=meter_thread_id and then using it in another query ,But all I am getting is the null value for meter_thread_id .. 我试图将字段存储在一个变量meterthreadid = meter_thread_id中 ,然后在另一个查询中使用它,但是我所得到的只是meter_thread_id的空值。

Let me know if the procedure is wrong anywhere ?? 让我知道该程序是否在任何地方出错?

Thanks 谢谢

The = just checks for equality. =只是检查是否相等。 The assignment operator is := . 赋值运算符是:= Though you better write it like 虽然你最好这样写

SELECT 'whatever' INTO variable_name FROM table;

Not sure from the top of my head, if the assignment operator is restricted to user-defined variables (the ones beginning with an @ ). 从我的头上不确定,赋值运算符是否仅限于用户定义的变量(以@开头的变量)。

What you should also keep in mind is, that the variable will hold the value of the last row the select fetched, not from every row. 您还应该记住的是,该变量将保存选择的最后一行的值,而不是每一行的值。 Your procedure all in all is quite...well, the programmers would call it undefined behaviour . 您的过程总的来说是……嗯,程序员将其称为undefined behaviour
Add an ORDER BY clause (preferably with a LIMIT ) or make sure that your WHERE clause restricts the result to one row. 添加一个ORDER BY子句(最好使用LIMIT )或确保您的WHERE子句将结果限制为一行。

All in all I wonder, what your procedure actually is any good for. 总而言之,我想知道您的程序实际上有什么用处。 You could as well just do it all in one SELECT statement. 您也可以只在一个SELECT语句中完成所有操作。

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

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