存储过程和查询执行之间的结果不同

时间:2015-05-06 12:35:35

标签: mysql stored-procedures

我试图用Navicat用mysql执行存储过程,当我执行这个存储过程时,我得到了0行,但是当我执行存储过程的一部分时,我想得到1行这就是我想。存储过程是这样的:

BEGIN
#Routine body goes here...

IF @autor=0 THEN

 SELECT DISTINCT bdocus.ano_pub
  FROM            bdocus INNER JOIN
                     descriptores_bdocus ON bdocus.nserie = descriptores_bdocus.nserie INNER JOIN
                     descriptores ON descriptores_bdocus.iddescriptor = descriptores.id 
  WHERE        (bdocus.descriptores LIKE '%Victimización%')AND(bdocus.ano_pub LIKE CONCAT(@anyo,'%')) AND (bdocus.codigo_area LIKE CONCAT(@tema,'%')) AND(bdocus.codigo_colectivo LIKE CONCAT(@colectivo,'%')) AND (descriptores.nombre = @descriptores)
 GROUP BY bdocus.ano_pub
 ORDER BY bdocus.ano_pub DESC;
ELSE
 SELECT DISTINCT bdocus.ano_pub
 FROM            bdocus INNER JOIN
                     descriptores_bdocus ON bdocus.nserie = descriptores_bdocus.nserie INNER JOIN
                     descriptores ON descriptores_bdocus.iddescriptor = descriptores.id INNER JOIN
                     autores_bdocus ON bdocus.nserie = autores_bdocus.nserie
  WHERE        (bdocus.descriptores LIKE '%Victimización%') AND (autores_bdocus.idautor = @autor) AND(bdocus.ano_pub LIKE CONCAT(@anyo,'%')) AND (bdocus.codigo_area LIKE CONCAT(@tema,'%')) AND(bdocus.codigo_colectivo LIKE CONCAT(@colectivo,'%')) AND (descriptores.nombre = @descriptores)
  GROUP BY bdocus.ano_pub
  ORDER BY bdocus.ano_pub DESC;
  END IF;

END

参数是这样的:

IN `@anyo` varchar(20),IN `@tema` varchar(20),IN `@autor` int,  
IN `@colectivo` varchar(30),IN `@descriptores` varchar(40)

和我想要的值适用于@anyo='2013'@tema='%'@autor=44439@colectivo='%'@descriptores='Violencia sexual'

当我运行存储过程的第二部分时,我的意思是:

SELECT DISTINCT bdocus.ano_pub
FROM            bdocus INNER JOIN
                     descriptores_bdocus ON bdocus.nserie = descriptores_bdocus.nserie INNER JOIN
                     descriptores ON descriptores_bdocus.iddescriptor = descriptores.id INNER JOIN
                     autores_bdocus ON bdocus.nserie = autores_bdocus.nserie
 WHERE        (bdocus.descriptores LIKE '%Victimización%') AND (autores_bdocus.idautor = 44439) AND(bdocus.ano_pub LIKE CONCAT('%','%')) AND (bdocus.codigo_area LIKE CONCAT('%','%')) AND(bdocus.codigo_colectivo LIKE CONCAT('%','%')) AND (descriptores.nombre = 'Violencia sexual')
GROUP BY bdocus.ano_pub
ORDER BY bdocus.ano_pub DESC;

我得到的结果是我想要的值。那么区别在哪里?提前谢谢。

2 个答案:

答案 0 :(得分:0)

我认为,由于条件的原因,我无法获得结果,我的意思是,您的程序无法进入'其他'部分,因此你没有得到那个结果。你可以做的是尝试分别运行第一个选择

SELECT DISTINCT bdocus.ano_pub
  FROM            bdocus INNER JOIN
                     descriptores_bdocus ON bdocus.nserie = descriptores_bdocus.nserie INNER JOIN
                     descriptores ON descriptores_bdocus.iddescriptor = descriptores.id 
  WHERE        (bdocus.descriptores LIKE '%Victimización%')AND(bdocus.ano_pub LIKE CONCAT(@anyo,'%')) AND (bdocus.codigo_area LIKE CONCAT(@tema,'%')) AND(bdocus.codigo_colectivo LIKE CONCAT(@colectivo,'%')) AND (descriptores.nombre = @descriptores)
 GROUP BY bdocus.ano_pub
 ORDER BY bdocus.ano_pub DESC;

只是为了看看是否与整个程序的结果相同,如果你得到0行,就像你运行程序一样,可能就是这个原因。尝试更改条件,以便首先检查第一个选择(这是我主要需要的那个),然后转到另一个。

应该像

-- see that i changed the condition
IF @autor != 0 THEN

 SELECT DISTINCT bdocus.ano_pub
 FROM            bdocus INNER JOIN
                     descriptores_bdocus ON bdocus.nserie = descriptores_bdocus.nserie INNER JOIN
                     descriptores ON descriptores_bdocus.iddescriptor = descriptores.id INNER JOIN
                     autores_bdocus ON bdocus.nserie = autores_bdocus.nserie
  WHERE        (bdocus.descriptores LIKE '%Victimización%') AND (autores_bdocus.idautor = @autor) AND(bdocus.ano_pub LIKE CONCAT(@anyo,'%')) AND (bdocus.codigo_area LIKE CONCAT(@tema,'%')) AND(bdocus.codigo_colectivo LIKE CONCAT(@colectivo,'%')) AND (descriptores.nombre = @descriptores)
  GROUP BY bdocus.ano_pub
  ORDER BY bdocus.ano_pub DESC;
  END IF;

ELSE

SELECT DISTINCT bdocus.ano_pub
  FROM            bdocus INNER JOIN
                     descriptores_bdocus ON bdocus.nserie = descriptores_bdocus.nserie INNER JOIN
                     descriptores ON descriptores_bdocus.iddescriptor = descriptores.id 
  WHERE        (bdocus.descriptores LIKE '%Victimización%')AND(bdocus.ano_pub LIKE CONCAT(@anyo,'%')) AND (bdocus.codigo_area LIKE CONCAT(@tema,'%')) AND(bdocus.codigo_colectivo LIKE CONCAT(@colectivo,'%')) AND (descriptores.nombre = @descriptores)
 GROUP BY bdocus.ano_pub
 ORDER BY bdocus.ano_pub DESC;

END

尝试并告诉我它是怎么回事,其他可能的事情,是你在调用程序时所参与的参数

答案 1 :(得分:0)

重要的是指出9.4. User-Defined Variables和常规参数13.1.15. CREATE PROCEDURE and CREATE FUNCTION Syntax之间的差异,是不同的变量。

在您的示例中@anyo可能是NULL`@anyo`已分配给'2013'

SQL Fiddle example