zoukankan      html  css  js  c++  java
  • mysql增删改查存储过程

    ----------------------------------------------------------分页--------------------------------------------------
    create PROCEDURE P_GetStudentList
    (
    _TrueName varchar(50),
    _Age int,
    _PageIndex int,
    _PageSize int,
    out _TotalCount int
    )
    BEGIN
           DECLARE _sql VARCHAR(200) DEFAULT 'select * from v_userlist ';-- 空格
           DECLARE _countSql VARCHAR(200) DEFAULT 'select count(ID) from v_userlist ';
           DECLARE _whereSql VARCHAR(200) DEFAULT ' where 1=1 ';
           if(_TrueName is not null and _TrueName<>'') THEN
                set _whereSql=CONCAT(_whereSql,' and TrueName like ''%',_TrueName,'%''');

           END IF;
           if(_Age>0) THEN
                set _whereSql=CONCAT(_whereSql,' and Age=',_Age);
           END if;

           -- 算总记录数
              set _countSql=CONCAT(_countSql,_whereSql,' into @totalCount');-- 将总记录数放在会话变量@totalCount
              set @_countSql=_countSql;
              PREPARE pre_count_sql from @_countSql;-- 预编译
              EXECUTE pre_count_sql;
              set _TotalCount=@totalCount;-- 保存总记录数
              DEALLOCATE PREPARE pre_count_sql;

                -- 拼接分页数据
             set _whereSql=CONCAT(_whereSql,' limit ',(_PageIndex-1)*_PageSize,',',_PageSize);
             set _sql=CONCAT(_sql,_whereSql);
             SET @_sql=_sql;
             PREPARE pre_sql from @_sql;
             EXECUTE pre_sql;
             DEALLOCATE PREPARE pre_sql;
    END;
    --------------------------------------插入-----------------------------------
    CREATE PROCEDURE test_proc_ins(
    IN i_id INT,
    IN i_name VARCHAR(100),
    OUT o_ret INT)
    BEGIN
    start transaction;
    INSERT INTO testproc VALUES (i_id, i_name);
    INSERT INTO testproc VALUES (i_id+1,i_name);
    commit; -- 语句1,提交后,事务已结束
    set o_ret = 1;
    start transaction; -- 再启一个事务
    INSERT INTO testproc VALUES (i_id+2,i_name); -- 语句2
    INSERT INTO testproc VALUES (i_id+2,i_name); -- 语句3
    set o_ret = 2;
    commit; -- 数据正常的情况下,需要再次commit以结束事务
    END;
    -----------------------------------------------------------------------------------------------------------------------------------
    ----------------------------------------------------添加---------------------------------------------------------------------------
    create PROCEDURE InsertUser(
    uName varchar(100),
    lName varchar(100),
    rId int
    )
    BEGIN
    START TRANSACTION;
    insert into userinfo(UserName,loginName,roleId) values(uName,lName,rId);
    COMMIT;
    END

    call InsertUser('asd','asd',1);

    ----------------------------------------------------------删除-------------------------------------------------------------------------
    create PROCEDURE DeleteUser(
    uId int
    )
    BEGIN
    START TRANSACTION;
    delete from userinfo where Id=uId;
    COMMIT;
    END

    ---------------------------------------------------------修改------------------------------------------------------------------------
    create PROCEDURE UpdateUser(
    uName varchar(100),
    lName varchar(100),
    rId int,
    uId int
    )
    BEGIN
    START TRANSACTION;
    Update userinfo set UserName=uName,loginName=lName,roleId=rId where Id=uId;
    COMMIT;
    END

    -------------------------------------------------------查询分页------------------------------------------------------------------------
    create PROCEDURE PageUser(
    uName varchar(100),
    pageIndex int,
    pageSize int
    )
    BEGIN
    DECLARE rid int;
    set rid=((pageIndex-1)*pageSize);
    select * from userinfo where UserName like uName LIMIT rid,pageSize;
    END

    ---------------------------------------------------------添加修改-----------------------------------------------------------------------
    create PROCEDURE UpdateAddUser(
    uName varchar(100),
    lName varchar(100),
    rId int,
    uId int
    )
    BEGIN
    IF(uId>0) THEN
    Update userinfo set UserName=uName,loginName=lName,roleId=rId where Id=uId;
    END IF;
    IF(uId=0) THEN
    insert into userinfo(UserName,loginName,roleId) values(uName,lName,rId); END IF;END

  • 相关阅读:
    Python进阶: Decorator 装饰器你太美
    计算机网络自顶向下方法第3章-传输层 (Transport Layer).2
    Python进阶:值传递,引用传递?不存在的,是赋值传递
    Python进阶:对象复制与比较,分深浅,见真假
    Python基础:模块化来搭项目
    这说明一个问题
    我稍微思考了一下
    由LruCache和DiskLruCache提供三级缓存支持的ImageLoader
    回忆一个加塞方法
    三年六班的李子明同学,你妈拿了两本计算机基础在二号树上等你
  • 原文地址:https://www.cnblogs.com/swjlove/p/13716560.html
Copyright © 2011-2022 走看看