Home  >  Article  >  Database  >  sql存储过程实现 添加新用户,判断是否已存在

sql存储过程实现 添加新用户,判断是否已存在

WBOY
WBOYOriginal
2016-06-07 17:48:391812browse

文章分享一篇关于利用存储过程实现 添加新用户,判断是否已存在功能,以前我们都是写在单独的sql语句里面,下面来看看实现方法。

 代码如下 复制代码

 

DELIMITER $$

DROP PROCEDURE IF EXISTS `monitor`.`p_user_ Insertuser `$$ 

#若存在同名存储过程先删除该存储过程monitor是名 p_user_Insertuser是存储过程名(p_表明_操作+表明)

CREATE DEFINER=`liteng`@`%` PROCEDURE `p_user_Insertuser`(

          userName varchar(200)                         #DAL层(数据访问层)传入参数用户名

)

BEGIN

         declare id int default 0;                             #定义类型为int默认值为的变量id

         USER_ID into id from monitor_user where USER_Name=userName and USER_State=1 limit 1;      

        #查找表中USER_Name列中等于参数userName且USER_State(用户状态,当用户被删除时,此值为0;存在时为1)为1的列,并将该数据主键放入变量id中

         if(!id) then                                              #如果不存在和变量userName相等的数据,即无此人注册过

             INSERT INTO  monitor_user  (USER_Name) VALUES(userName);                              

            #插入用户名为userName的数据

             select row_count();                              #返回影响的行数

         else

             select -1;                                           #若已存在该数据则返回-1

         end if;

    END$$

 

DELIMITER ;

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn