[英]OUT parameter declaration in stored procedure
Excuse me for the pretty simple question, but I can't understand what I have to do from similar questions.对不起,这个非常简单的问题,但我无法从类似的问题中理解我必须做什么。
I have this code, It works but I got asked to insert input value for CountMacro1, CountMacro2, CountMacro3, CountMacro4, which is always 0. How can I explain to mysql that I want those variables set to 0?我有这个代码,它可以工作,但我被要求插入 CountMacro1、CountMacro2、CountMacro3、CountMacro4 的输入值,它始终为 0。我如何向 mysql 解释我希望这些变量设置为 0? set @CountMacro1 = 0;设置@CountMacro1 = 0; seems not to work, why?好像不行,为什么? Thanks everyone :D谢谢大家:D
Mysql workbench do a call and a select like this: Mysql 工作台像这样进行调用和选择:
set @CountMacro1 = 0;
set @CountMacro2 = 0;
set @CountMacro3 = 0;
set @CountMacro4 = 0;
call dbcbt.CountMacro_Audit_Scaduti(@CountMacro1, @CountMacro2, @CountMacro3, @CountMacro4);
select @CountMacro1, @CountMacro2, @CountMacro3, @CountMacro4;
Thats the call in php:这就是 php 中的调用:
$conn = new PDO("mysql:host=$servername;dbname=dbcbt", $username, $password);
$sql = 'CALL CountMacro_Audit_Scaduti(@CountMacro1_OUT,@CountMacro2_OUT,@CountMacro3_OUT,@CountMacro4_OUT)';
$stmt = $conn->prepare($sql);
$CountMacro1_OUT = 0;
$CountMacro2_OUT = 0;
$CountMacro3_OUT = 0;
$CountMacro4_OUT = 0;
$stmt->bindParam(1, $CountMacro1_OUT, PDO::PARAM_INT, 10);
$stmt->bindParam(2, $CountMacro2_OUT, PDO::PARAM_INT, 10);
$stmt->bindParam(3, $CountMacro3_OUT, PDO::PARAM_INT, 10);
$stmt->bindParam(4, $CountMacro4_OUT, PDO::PARAM_INT, 10);
print "Values of bound parameters _before_ CALL:\n";
print " 1: {$CountMacro1_OUT} 2: {$CountMacro2_OUT} 3: {$CountMacro3_OUT} 4: {$CountMacro4_OUT}\n";
$stmt->execute();
print "Values of bound parameters _after_ CALL:\n";
print " 1: {$CountMacro1_OUT} 2: {$CountMacro2_OUT} 3: {$CountMacro3_OUT} 4: {$CountMacro4_OUT}\n";
CREATE DEFINER=`root`@`localhost` PROCEDURE `CountMacro_Audit_Scaduti`(
OUT CountMacro1 int,
OUT CountMacro2 int,
OUT CountMacro3 int,
OUT CountMacro4 int
)
BEGIN
set @CountMacro1 = 0;
set @CountMacro2 = 0;
set @CountMacro3 = 0;
set @CountMacro4 = 0;
select count(*)
INTO CountMacro1
from t_audit
WHERE (t_audit.Data_Scadenza < NOW()) AND (t_audit.Data_Completamento ="" OR t_audit.Data_Completamento is null) AND (t_audit.Macro1= 1 OR t_audit.Macro2= 1 OR t_audit.Macro3= 1);
select count(*)
INTO CountMacro2
from t_audit
WHERE t_audit.Data_Scadenza < NOW() AND (t_audit.Data_Completamento ="" OR t_audit.Data_Completamento is null) AND (t_audit.Macro1= 2 OR t_audit.Macro2= 2 OR t_audit.Macro3= 2);
select count(*)
INTO CountMacro3
from t_audit
WHERE t_audit.Data_Scadenza < NOW() AND (t_audit.Data_Completamento ="" OR t_audit.Data_Completamento is null) AND (t_audit.Macro1= 3 OR t_audit.Macro2= 3 OR t_audit.Macro3= 3);
select count(*)
INTO CountMacro4
from t_audit
WHERE t_audit.Data_Scadenza < NOW() AND (t_audit.Data_Completamento ="" OR t_audit.Data_Completamento is null) AND (t_audit.Macro1= 4 OR t_audit.Macro2= 4 OR t_audit.Macro3= 4);
END
You can declare Local variables and then you can assign the default value.您可以声明局部变量,然后您可以分配默认值。
CREATE PROCEDURE MYPROC(var OUT)
BEGIN
DECLARE CountMacro1 INT DEFAULT 0;
DECLARE CountMacro2 INT DEFAULT 0;
DECLARE CountMacro3 INT DEFAULT 0;
DECLARE CountMacro4 INT DEFAULT 0;
--Your code
END;
In your case, you are trying to assign default value to @CountMacro1
which is User defined variables and in your query you are assigning the count to local variable CountMacro1
.在您的情况下,您试图将默认值分配给@CountMacro1
,它是用户定义的变量,并且在您的查询中您将计数分配给局部变量CountMacro1
。 Both are different.两者是不同的。
You can run the below procedure to see the difference您可以运行以下程序以查看差异
CREATE PROCEDURE prc_test ()
BEGIN
DECLARE CountMacro1 INT DEFAULT 1;
SET @CountMacro1 = 2;
SELECT CountMacro1 , @CountMacro1 ;
END;
OUTPUT输出
+-------------+--------------+
| CountMacro1 | @CountMacro1 |
+-------------+--------------+
| 1 | 2 |
+-------------+--------------+
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.