[英]How can I pass parameter to stored procedure through a query?
I have this stored procedure: 我有这个存储过程:
CREATE PROCEDURE `test`(OUT `vote_value` INT, OUT `score` INT, IN `user_id` INT, IN `time_range` INT)
NO SQL
BEGIN
SELECT coalesce(sum(vote_value), 0), coalesce(sum(score), 0)
INTO vote_value, score
FROM reputations
WHERE owner_id = user_id
AND date_time > CASE time_range
WHEN 'WEEK'
THEN unix_timestamp(DATE_SUB(now(), INTERVAL 1 WEEK))
WHEN 'MONTH'
THEN unix_timestamp(DATE_SUB(now(), INTERVAL 1 MONTH))
WHEN 'YEAR'
THEN unix_timestamp(DATE_SUB(now(), INTERVAL 1 YEAR))
ELSE 1 END;
END
As you see my procedure gets 4 parameters. 如您所见,我的过程获取4个参数。 Both
vote_value
and score
have OUT
direction (as result) , And both user_id
and time_range
have IN
Direction. vote_value
和score
都具有OUT
方向(结果) ,而user_id
和time_range
都具有IN
方向。 So I have to pass those two which have IN
direction when I call the procedure. 因此,当我调用该过程时,必须传递具有
IN
方向的两个。
Ok, I can call procedure about like this: 好的,我可以这样调用程序:
CALL test(@vote_value, @score, 10, 'WEEK');
SELECT @vote_value, @score;
It would work as well. 它也会工作。 But please focus on the third parameter which is
10
. 但请注意第三个参数
10
。 I don't want to pass it manually by hand. 我不想手动通过它。 I want it be a value from a query.
我希望它是来自查询的值。 Something like this:
像这样:
CALL test(@vote_value, @score, u.id, 'WEEK');
SELECT id, @vote_value, @score FROM Users u;
But it throws syntax error . 但这会引发语法错误 。 Does anybody know how can I do such a thing?
有人知道我该怎么做吗? The expected result is this .
预期的结果是这样 。
SELECT id into @id FROM Users LIMIT 1;
CALL test(@vote_value, @score, @id, 'WEEK');
Try above code. 尝试上面的代码。
As mention in above code you can get value of that id
using SELECT id into @id
and then use that @id
as parameter for that query. 如上述代码中所述,您可以使用
SELECT id into @id
获取该id
值,然后将该@id
用作该查询的参数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.