mysql存储过程语法和oracle的区别

mysql存储过程语法和oracle的区别

本文目录

  • mysql存储过程语法和oracle的区别
  • mysql 存储过程
  • 创建mysql存储过程语法
  • mysql存储过程的写法
  • mysql存储过程中分支语句有哪些

mysql存储过程语法和oracle的区别


首先是大体一致的,只是分页查询时oracle用的伪列(rownum),mysql用的是limit,具体的可以百度一下分页;
另外oracle对sql语句要求更为严格,而且oracle里变量较mysql更多点,oracle中有number型,有大数据类型,mysql没得;
另外举个例子,oracle不能插入为空列,而mysql是可以的(个人觉得,不知道正确与否)。还有他们两者函数有不同之处,如转日期函数oracle是to_date(’要转的字符串’,’格式’) -- select to_date(’2004-05-07 13:23:44’,’yyyy-mm-dd hh34:mi:ss’) from dual,而mysql是str_to_date(’08/09/2008’, ’%m/%d/%Y’); -- 2008-08-09//都是针对字符串转日期来的。
还有一点,我们常常希望主键可以自动增长,避免我们插入数据时的重复问题,但是oracle不能设置列自动增长,而mysql是可以的,oracle可以用序列加触发器来解决自动增长问题达到与mysql一样的效果。
总体来说百分之九十的sql语句是没区别的。总体来说oracle的格式严格点,对有些字符型的还必须加单引号才能插入,mysql要求就没这么多了。还有当向数据库插入一个日期时,mysql可以直接插入成功,但是oracle需要先转化为sql里面的日期类型才行;oracle较mysql而言更安全,但是收费的,一般大公司用的多。oracle还有存储过程和函数,触发器这些这是mysql没有的。大体就是这样吧。

mysql 存储过程


你应该在做统计吧,估计你不会的就是mysql存储过程的语法 我之前也写过 很是郁闷 我给你一段代码 是我用mysql写过的一个存储过程 你看看 主要是了解里面的语法 看懂了 你所说的需求并不难
有看不懂的地方一起讨论 :
begin
declare tikk datetime ;
declare done int default 0;
declare userid int default 0;
declare moduleid int default 0;
declare couid int default 0;
declare mduname varchar(255) ;
declare opsid int default 0;
declare c1 cursor for Select I_userID,I_operationID from space_operation_record where status《》0 group by I_userID,I_operationID order by createtime desc;
declare continue handler for sqlstate ’02000’ set done =1;
set tikk = now();
open c1;
repeat
fetch c1 into userid, opsid;
if not done then
select I_moduleID from space_operation where status《》0 and ID=opsid into moduleid;
if moduleid 《》 ’’ then
select Nvc_identification from space_operation where status《》0 and ID=opsid into @identiftion;
if moduleid 》 0 then
Select Nvc_ename from space_module where status《》0 and ID=moduleid into mduname;
else
set mduname = ’space’;
end if;
create temporary table if not exists sp_tab1(id bigint(20),Nvc_content MEDIUMTEXT,I_obyuID bigint(20),I_tID bigint(20),createtime datetime);
INSERT INTO sp_tab1 Select ID,Nvc_content,I_objectID,I_tmID,createtime from space_operation_record where status《》0 and I_operationID=opsid and I_userID=userid ;
select count(*) from sp_tab1 into couid;

set @ihod = 0;
set @listp = ’’;
set @listpp = ’’;
set @content0p = ’’;
set @content0 = ’’;
while couid 》 0 do
select ID,Nvc_content,I_obyuID,createtime,I_tID into @iok,@conuiy,@objiplk,@crtimhr,@tmids from sp_tab1 where ID 》 @ihod order by ID asc limit 0,1;
if @iok 《》 ’’ then
if mduname = ’blog’ then
INSERT INTO space_operation_stat(I_operationID,I_userID,Nvc_content,D_stattime,createtime) VALUES (@iok,userid,@conuiy,@crtimhr,tikk);
elseif mduname = ’team’ then
if(@identiftion = ’addblog’ || @identiftion = ’mdyblog’) then
INSERT INTO space_operation_stat(I_operationID,I_userID,Nvc_content,D_stattime,I_tmID,createtime) VALUES (@iok,userid,@conuiy,@crtimhr,@tmids,tikk);
else
set @listpp = CONCAT(@listpp,CONCAT(@objiplk,’,’));
set @operarry1p = substring_index(@conuiy,’|’,1);
set @operarry2p = substring_index(@conuiy,’|’,-1);
set @content0p = CONCAT(@content0p,CONCAT(@operarry2p,SPACE(1)));
set @objlistp = substring(@listpp,1,length(@listpp)-1);
end if;
elseif mduname = ’space’ then
if(@identiftion = ’headphoto’ || @identiftion = ’status’) then
INSERT INTO space_operation_stat(I_operationID,I_userID,Nvc_content,D_stattime,I_tmID,createtime) VALUES (@iok,userid,@conuiy,@crtimhr,@tmids,tikk);
else
set @listppr = CONCAT(@listppr,CONCAT(@objiplk,’,’));
set @operarry1pr = substring_index(@conuiy,’|’,1);
set @operarry2pr = substring_index(@conuiy,’|’,-1);
set @content0pr = CONCAT(@content0pr,CONCAT(@operarry2pr,SPACE(1)));
set @objlistpr = substring(@listppr,1,length(@listppr)-1);
end if;
else
set @listp = CONCAT(@listp,CONCAT(@objiplk,’,’));
set @operarry1 = substring_index(@conuiy,’|’,1);
set @operarry2 = substring_index(@conuiy,’|’,-1);
set @content0 = CONCAT(@content0,CONCAT(@operarry2,SPACE(1)));
set @objlist = substring(@listp,1,length(@listp)-1);
end if;
set @ihod = @iok;
end if;
set couid = couid -1;
end while;
if @content0 《》 ’’ then
set @contentp = CONCAT(@operarry1,concat(’|’,@content0));
Select createtime,ID into @uitimej,@IDjok from space_operation_record where status《》0 and I_operationID=opsid order by createtime desc limit 0,1;
if @uitimej 《》 ’’ then
INSERT INTO space_operation_stat(I_operationID,I_userID,Nvc_content,D_stattime,createtime,Nvc_objlist) VALUES(@iok,userid,@contentp,@crtimhr,tikk,@objlist);
end if;
end if;
if @content0p 《》 ’’ then
if @identiftion = ’addphoto’ then
set @contentp = CONCAT(@operarry1p,CONCAT(’|’,@content0p));
else
set @contentp = CONCAT(@operarry1p,CONCAT(@content0p,’|’));
end if;

Select createtime,ID into @uitimej,@IDjok from space_operation_record where status《》0 and I_operationID=opsid order by createtime desc limit 0,1;
if @uitimej 《》 ’’ then
INSERT INTO space_operation_stat(I_operationID,I_userID,Nvc_content,D_stattime,createtime,Nvc_objlist,I_tmID) VALUES(@iok,userid,@contentp,@crtimhr,tikk,@objlistp,@tmids);
end if;
end if;
if @content0pr 《》 ’’ then
set @contentp = CONCAT(@operarry1p,concat(’|’,@content0pr));
Select createtime,ID into @uitimej,@IDjok from space_operation_record where status《》0 and I_operationID=opsid order by createtime desc limit 0,1;
if @uitimej 《》 ’’ then
INSERT INTO space_operation_stat(I_operationID,I_userID,Nvc_content,D_stattime,createtime,Nvc_objlist,I_tmID) VALUES(@iok,userid,@contentp,@crtimhr,tikk,@objlistp,@tmids);
end if;
end if;
delete from sp_tab1;
end if;
end if;
until done end repeat;
close c1;
drop temporary table if exists sp_tab1 ;
UPDATE space_operation_play SET status=0;
UPDATE space_operation_display SET status=0;
Select createtime into @ptimes from space_operation_stat where status《》0 order by createtime desc limit 0,1;
if @ptimes 《》’’ then
create temporary table if not exists sp_tab2(id bigint(20),Nvc_content MEDIUMTEXT,I_userID bigint(20),I_lyuID bigint(20),D_stattime datetime);
INSERT INTO sp_tab2 Select ID,Nvc_content,I_userID,I_tmID,D_stattime from space_operation_stat where status《》0 and createtime=@ptimes order by D_stattime desc limit 0,30;
select count(*) from sp_tab2 into @cou1id;
set @uoj = 0;
while @cou1id 》 0 do
select ID,Nvc_content,I_userID,D_stattime,I_lyuID into @io1k,@conui1y,@objipl1k,@crtimh1r,@unlpa from sp_tab2 where ID 》 @uoj order by ID asc limit 0,1;
if @io1k 《》 ’’ then
INSERT INTO space_operation_play(I_statID,Nvc_content,D_stattime,I_userID,Createtime,I_tmID) VALUES (@io1k,@conui1y,@crtimh1r,@objipl1k,now(),@unlpa);
set @uoj = @io1k;
end if;
set @cou1id = @cou1id -1;
end while;
drop temporary table if exists sp_tab2 ;
end if;
end

创建mysql存储过程语法


既然错误提示已经显示存储过程“r“已经存在,说明该存储确实是存在的。题主可以运行下列SQL语句,检视现存所有的存储过程列表:
show procedure status;
或者
select `name` from mysql.proc where db = ’database_name’ and `type` = ’PROCEDURE’;
应该可以看到存储过程“r“,除非您不是数据库管理员或权限不够。

mysql存储过程的写法


使用MySQL编程和SQL Server基本思想是一致的,语法有较大的区别。SQL Server不管是条件、循环等区块仍然要使用begin与end作为起始和结束的标识,而MySQL则直接用END something(如while,if,loop),当然,由于自己接触MySQL并不长,没有使用SQL Server那么熟练,所以写这些也是非常基础的部分,而在实际问题中会千变万化,所以还是需要更多的实践来不断提高。

mysql存储过程中分支语句有哪些


存储过程:
create procedure p()
begin
/*thi procedure does nothing*/
end;
1.参数
Parameters 参数
让我们更进一步的研究怎么在存储过程中定义参数1. CREATE PROCEDURE p5
() ...
2. CREATE PROCEDURE p5
([IN] name data-type) ...
3. CREATE PROCEDURE p5
(OUT name data-type) ...
4. CREATE PROCEDURE p5
(INOUT name data-type) ...
2.Conditions and if-then-else 条件式和 if-then-else
3.Loops 循环语句
WHILE ... END WHILE
LOOP ... END LOOP
REPEAT ... END REPEAT
GOTO
4.DECLARE HANDLER syntax 声明异常处理的语法
DECLARE
{ EXIT | CONTINUE }
HANDLER FOR
{ error-number | { SQLSTATE error-string } | condition }
SQL statement
5.Cursors 游标
游标实现功能摘要:
DECLARE cursor-name CURSOR FOR SELECT ...;
OPEN cursor-name;
FETCH cursor-name INTO variable [, variable];
CLOSE cursor-name;
已现经在可我以们完开成始基着本眼的游事标了务如。声虽明然游我标们,的打存开储游过标程,中从的游游标标里语法读取还,并关没闭有完游整标。
6.Functions 函数
Summary:
摘要 CREATE FUNCTION
Limitations of functions
函数的限制
我们已经很清楚可以在存储过程中使用的元素了。下面我要讲的是前面没有提到的函数。

免责声明:本网信息来自于互联网,目的在于传递更多信息,并不代表本网赞同其观点。其原创性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容、文字的真实性、完整性、及时性本站不作任何保证或承诺,并请自行核实相关内容。本站不承担此类作品侵权行为的直接责任及连带责任。如若本网有任何内容侵犯您的权益,请及时联系我们,本站将会在24小时内处理完毕。
相关文章
返回顶部