我的sql语法不能用于比较存储过程中的本地vairaiable

时间:2017-05-29 10:22:12

标签: mysql sql

我有以下存储过程

DELIMITER $$

USE `mydb$$

DROP PROCEDURE IF EXISTS `add_master_profile_data_temp`$$

CREATE PROCEDURE `add_master_profile_data_temp`(
  IN _msisdn VARCHAR (50),  
  IN _device_identifier TEXT  
)
BEGIN
DECLARE lv_IDMsisdn BIGINT DEFAULT NULL;
DECLARE lv_IDDevice BIGINT  DEFAULT NULL ;
  SET     @lv_IDMsisdn := (SELECT id     FROM    `master_profile`    WHERE `msisdn` = _msisdn) ;

  SET     @lv_IDDevice := (SELECT id   FROM    `master_profile`     WHERE device_identifier =_device_identifier   AND `msisdn` <> _msisdn   AND `master_profile_status` = 1) ;  

  IF (@lv_IDDevice <> @lv_IDMsisdn)  THEN    

         SELECT  @lv_IDDevice ,@lv_IDMsisdn ,2 ;
  ELSE

         SELECT  @lv_IDDevice ,@lv_IDMsisdn ,1;
   END IF;





END$$

DELIMITER ;

问题在于语法@lv_IDDevice&lt;&gt; @lv_IDMsisdn总是假的 而结果我得到了

@lv_IDDevice @lv_IDMsisdn 1

62(NULL)1

修改

我甚至尝试过

CREATE PROCEDURE `add_master_profile_data_temp`(
  IN _msisdn VARCHAR (50),  
  IN _device_identifier TEXT  
)
BEGIN
DECLARE lv_IDMsisdn BIGINT;
DECLARE lv_IDDevice BIGINT  ;
  #SET     @lv_IDMsisdn := (SELECT id     FROM    `master_profile`    WHERE `msisdn` = _msisdn) ;

  #SET     @lv_IDDevice := (SELECT id   FROM    `master_profile`     WHERE device_identifier =_device_identifier   AND `msisdn` <> _msisdn   AND `master_profile_status` = 1) ;  


  SELECT id  INTO lv_IDMsisdn   FROM    `master_profile`    WHERE `msisdn` = _msisdn ;

  SELECT id INTO lv_IDDevice  FROM    `master_profile`     WHERE device_identifier =_device_identifier   AND `msisdn` <> _msisdn   AND `master_profile_status` = 1 ;  

  IF (lv_IDDevice IS NOT NULL AND  lv_IDDevice <> lv_IDMsisdn)  THEN     

         SELECT  lv_IDDevice ,lv_IDMsisdn ,2 ;
  ELSE

         SELECT  lv_IDDevice ,lv_IDMsisdn ,1;
  END IF;





END$$

DELIMITER ;

2 个答案:

答案 0 :(得分:0)

有两件事需要考虑:

删除2个DECLARE语句,因为它们是无意义的:

DECLARE lv_IDMsisdn BIGINT DEFAULT NULL;
DECLARE lv_IDDevice BIGINT  DEFAULT NULL ;

试试下面的sql:

SELECT @lv_IDMsisdn := id     FROM    `master_profile`    WHERE `msisdn` = _msisdn;

SELECT @lv_IDDevice :=id   FROM    `master_profile`     WHERE device_identifier =_device_identifier   AND `msisdn` <> _msisdn   AND `master_profile_status` = 1;  

然后,创建一个调试表。看看@lv_IDMsisdn和@lv_IDDevice

的真正价值
CREATE TABLE `debug_table`( `ID` INT NOT NULL AUTO_INCREMENT, `DecimalValue` DECIMAL(10,6), PRIMARY KEY (`ID`) );

- 在设置值

后添加2个语句
INSERT INTO `debug_table`(@lv_IDMsisdn);
INSERT INTO `debug_table`(@lv_IDDevice);

答案 1 :(得分:0)

我最终将@lv_IDMsisdn设置为0,因为它为null。 像SET @lv_IDMsisdn = 0;

很奇怪为什么不等于没有给出真实的结果,一方面我们有价值而另一方面我们有空

问题是语法

IF (`lv_IDDevice` IS NOT NULL AND  `lv_IDDevice`  <> `lv_IDMsisdn`)  THEN

另一位用户建议的一件事是(虽然我没有尝试过)

  IF (`lv_IDDevice` IS NOT NULL AND  Not (`lv_IDDevice`  <=> `lv_IDMsisdn`))  THEN