0

我无法在这段代码中得到 OUT 变量的结果,我做错了什么吗?谁能帮忙?

BEGIN 
    DECLARE done INT DEFAULT FALSE;
    DECLARE cid INT(10);    
    DECLARE cuserid VARCHAR(50);
    DECLARE cur1 CURSOR FOR SELECT id,username FROM tblcustomer;
    DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE; 
    OPEN cur1;
    read_loop: LOOP
        FETCH cur1 INTO cid,cuserid;   
            IF done THEN
                LEAVE read_loop;
            END IF;   
            SET customers_list = CONCAT(customers_list,cid,':',cuserid,',');
    END LOOP;
    CLOSE cur1;
END
4

1 回答 1

1

您的代码段中缺少过程原型,但假设如下:

CREATE PROCEDURE foo(OUT customers_list VARCHAR(100))
BEGIN
    ...
    SET customers_list = 'foo-list';
END ;

这就是您检索“返回值”的方式:

CALL foo(@var);
SELECT @var; -- outputs "foo-list"

严格来说,一个过程没有“返回值”。一个函数有:

CREATE FUNCTION bar() RETURNS VARCHAR(100)
BEGIN
    ...
    RETURN 'bar-list';
END ;

SELECT bar(); -- outputs "bar-list";
于 2013-09-09T16:55:26.123 回答