MySQL存储过程和MSSQL的区别
1. MSSQL的关键字procedure可以缩写为proc,而MySQL不能;
2. MySQL参数要申明是in、out还是inout,而MSSQL不用;
3. MySQL用CALL调用存储过程,MSSQL用EXEC;
4. MySQL有输出参数时要用变量@赋值并查询;
关键字参数、分页
实例1:
mysql> create procedure cs_ro_getquestionlist
-> (
-> in b int,-- 起始位置
-> in l int,-- 条数
-> in kw nvarchar(100)-- 关键字
-> )
-> begin
-> select id,title from w_question where locate(kw,title)>0 limit b,l;
-> end;
mysql> call cs_ro_getquestionlist(0,10,'中国');
含有输入、输出参数
实例2:
mysql> create procedure cs_ro_getquestionlist
-> (
-> in b int,-- 起始位置
-> in l int,-- 条数
-> in kw nvarchar(100),-- 关键字
-> out t int-- 返回总数
-> )
-> begin
-> select count(1) into t from w_question where locate(kw,title)>0;
-> select id,title from w_question where locate(kw,title)>0 limit b,l;
-> end;
mysql> call cs_ro_getquestionlist(0,10,'中国',@a);
mysql> select @a;
事务处理
实例3:
mysql> create procedure cs_px_addclass
-> (
-> in nid int,
-> in nname nvarchar(20),
-> in nQuestionNum int,
-> in npx int,
-> in npycode nvarchar(20)
-> )
-> begin
-> DECLARE t_error INTEGER DEFAULT 0;
-> DECLARE CONTINUE HANDLER FOR SQLEXCEPTION SET t_error=1;
-> START TRANSACTION;-- 事务开始
-> insert into w_questionclass(id,name,questionnum,px,pycode)
-> VALUES (nid,nname,nquestionnum,npx,npycode);
-> IF t_error = 1 THEN-- 如果失败则回滚
-> ROLLBACK;
-> ELSE
-> COMMIT;-- 执行成功,事务结束
-> END IF;
-> select t_error;-- 将事务的执行状态返回给被调者
-> end;
mysql> call cs_px_addclass(1,'测试',1,1,'cs');
+--------+
| t_error|