[心缘地方]同学录
首页 | 功能说明 | 站长通知 | 最近更新 | 编码查看转换 | 代码下载 | 常见问题及讨论 | 《深入解析ASP核心技术》 | 王小鸭自动发工资条VBA版
登录系统:用户名: 密码: 如果要讨论问题,请先注册。

[备忘]存储过程转为mysql的一点注意事项

上一篇:[备忘]邮件无法发送到 SMTP 服务器。传输错误代码为 0x80040217。服务器响应为 not available
下一篇:[备忘]vmware9安装mac系统,u盘无法识别,device in use

添加日期:2015/1/30 17:19:38 快速返回   返回列表 阅读2505次
(1)入参,出参不带@,out写在前面。
(2)不能return,得定义一个label,然后leave,如:
ExitLabel:BEGIN
.....
....LEAVE ExitLabel;
...
(3)declare 变量要集中放在begin后面,不能随便放。
declare 变量也不用@,后面用时也不带@

(4)in变量,貌似引用时,可以带@,能取到值。

(5)out变量,别带@,带了@,存储过程外面看不到这个值。

(6)set @aa=0;这种变量直接用即可,不用事先delare,

带@就永远带@,记住@aa和aa是两个变量。

(7)select的影响行数使用FOUND_ROWS(),
DECLARE aff INT; /* SQL 影响记录条数 */
..select...
SELECT FOUND_ROWS()  INTO aff;

不过debug时,经常没数据,还返回1,贼奇怪。

(8)select字段into变量,多个时,是这样写:
select xx字段,yy字段,zz字段 into v_xx,v_yy,v_zz ....
否则报yy字段不存在。

(9)最郁闷的是变量名不能和字段名重名,真TM变态,~~

(10)if ...then..
...elseif ...then
...elseif ...then
...else
...end if;

(11)异常处理

    DECLARE EXIT HANDLER FOR SQLEXCEPTION
        BEGIN
            RETURN 0;
        END;
这个是个function,所以能return,
这个是exit处理,就是出sql异常时,直接exit了。
还可以是continue处理什么的。

(12)事务处理

    DECLARE EXIT HANDLER FOR SQLEXCEPTION
        BEGIN
            ROLLBACK;
        END;

    START TRANSACTION;
...update...什么的



DROP PROCEDURE IF EXISTS `sptbUnit_ValidatedUnitAndUnitAlias`;
DELIMITER //

create PROCEDURE sptbUnit_ValidatedUnitAndUnitAlias(
    v_fdUnit varchar(64),
    v_fdUnitAlias varchar(64),
    v_fdUpdateID varchar(32),
    v_fdUpdateName varchar(32),
    out fdReturnCode int

ExitLabel:BEGIN
DECLARE aff INT; /* SQL 影响记录条数 */

    declare v_fdBasicUnit varchar(64);
    declare v_fdState int;
    declare v_fdType int;
    declare v_fdKind varchar(64);
    declare v_fdFactor float;
    
    select 
        fdState,fdType,fdFactor,fdBasicName
        into v_fdState,v_fdType,v_fdFactor,v_fdBasicUnit
    from tbUnit where fdName=@v_fdUnit;
    
    SELECT FOUND_ROWS()  INTO aff;
    if aff=0 then
            set fdReturnCode=100;
    elseif v_fdState<>1  then
            set fdReturnCode=103;
    elseif v_fdType=1 or v_fdType=2 then
            select 
                fdState,fdType
                into v_fdState,v_fdType
            from tbUnit where fdName=@v_fdUnitAlias;
            SELECT FOUND_ROWS()  INTO aff;
            if aff=0 then
                    set fdReturnCode=400;
            elseif v_fdState<>1 then
                    set fdReturnCode=403;
            elseif v_fdType<>0 then
                    set fdReturnCode=401;
            else 
                    update tbUnit set fdShowName=@v_fdUnit,fdType=3,fdFactor=v_fdFactor, fdBasicName=v_fdBasicUnit,
                    fdUpdateTime=now()  where fdName=@v_fdUnitAlias;
            end if;
    else
            set fdReturnCode=102;
    end if;
END
//
DELIMITER ;

 

评论 COMMENTS
没有评论 No Comments.

添加评论 Add new comment.
昵称 Name:
评论内容 Comment:
验证码(不区分大小写)
Validation Code:
(not case sensitive)
看不清?点这里换一张!(Change it here!)
 
评论由管理员查看后才能显示。the comment will be showed after it is checked by admin.
CopyRight © 心缘地方 2005-2999. All Rights Reserved