mysql存储过程检查用户名是否退出

时间:2016-01-13 11:11:07

标签: mysql stored-procedures

我正在尝试创建一个存储过程来检查数据库中是否存在用户名。但是,呃

CREATE PROCEDURE `USER_EXISTS`
(IN `GIVEN_USERNAME` VARCHAR(32))
BEGIN
    SET @User_exists = 0;
    SELECT COUNT(1) INTO @found
    FROM `users`
    WHERE `username` = `GIVEN_USERNAME`
    IF @found > 0 THEN
        SET @User_exists = 1
    END IF
    SELECT @User_exists
END

1 个答案:

答案 0 :(得分:3)

试试这个

CREATE PROCEDURE USER_EXISTS(IN GIVEN_USERNAME VARCHAR(32))
BEGIN
    SET @User_exists = 0;
    SELECT 1 INTO @User_exists
    FROM users
    WHERE username = GIVEN_USERNAME;       
    SELECT @User_exists
END