如何检查函数或变量的输出为NULL

时间:2015-02-20 12:40:10

标签: mysql

我对Mysql语法感到困惑。我想检查ExtractValue(xml,'// order [1] / quantity [$ @ i]')函数的值是否为NULL。它可以分配给变量,也可以跳过此操作。我试过这个并且有语法错误:

DELIMITER $$

DROP PROCEDURE IF EXISTS `sp_test_for_null`$$

CREATE PROCEDURE `sp_test_for_null`()
BEGIN
DECLARE xml VARCHAR(1000);
SET xml = '';
DECLARE test VARCHAR(1000);
SET test = (SELECT ExtractValue(xml, '//order[1]/quantity[$@i]');
IF (test IS NULL) THEN SELECT 1; END IF;
END$$

DELIMITER ;

CALL sp_test_for_null;

1 个答案:

答案 0 :(得分:1)

我试试这个(请注意所有DECLARE都在开头:

DELIMITER $$

DROP PROCEDURE IF EXISTS `sp_test_for_null`$$

CREATE PROCEDURE `sp_test_for_null`()
BEGIN
    DECLARE xml VARCHAR(1000);
    DECLARE test VARCHAR(1000);

    SET xml = '';
    SET test = (SELECT ExtractValue(xml, '//order[1]/quantity[$@i]');
    SELECT ISNULL(test, 1, 0);
END$$

DELIMITER ;

CALL sp_test_for_null;