MySQL存储过程 - 访问返回的列值

时间:2016-03-16 06:04:23

标签: mysql tsql stored-procedures

我该怎么做?

CREATE DEFINER=`root`@`localhost` PROCEDURE `MyProcedure`(
IN param VARCHAR(50))
BEGIN
    SELECT id, column2, column3
    FROM table1
    WHERE column5 = param;
    LIMIT 1;

    IF (FOUND_ROWS() > 0) THEN
        UPDATE table1 SET column4 = 1 WHERE id = theIdFromRowAbove;
    END IF;
END

我希望我的存储过程从table1中选择一行,如果找到一行,则更新table1,其中id =找到的行中的id。我一直在搜索互联网,但仍然无法弄明白。我不确定要搜索什么。

1 个答案:

答案 0 :(得分:1)

您可以在查找行设置为true

后获取变量并将其值设置为false
CREATE DEFINER=`root`@`localhost` PROCEDURE `MyProcedure`()
BEGIN
    DECLARE find_row INT DEFAULT 0;
    DECLARE theIdFromRowAbove INT DEFAULT 0;

    SELECT id, column2, column3, 1 INTO theIdFromRowAbove, @column2, @column3, find_row
    FROM table1
    WHERE id = 5
    LIMIT 1;

    IF (find_row) THEN
        UPDATE table1 SET column4 = 1 WHERE id = theIdFromRowAbove;
    END IF;
END

了解有关SELECT....INTO Official Link

的更多信息