Home Database Mysql Tutorial Informix和Oracle存储过程的异同

Informix和Oracle存储过程的异同

Jun 07, 2016 pm 05:45 PM
date oracle procedure

建立存储过程的语法:

  一、Informix

  create procedure proc_name( [....in_parameter_list])

  returning out_para_list / out_result_set;

  二、Oracle

  create [or replace] procedure procedue_name

  [ (arg1 [ {in | out | in out }] type

  (argn [ {in | out | in out }] type,)]

  {is | as} --代替DECLARE关键字

  [ 变量定义区]

  begin

  end procedure_name;

  三、几个简单的例子

  1、没有参数也没有返回值

  1)Informix

  create procedure pNoParam()

  begin

  on exception

  rollback work;

  return;

  end exception

  begin work;

  delete from t1;

  delete from t2;

  commit work;

  end;

  end procedure;

  2)Oracle

  create or replace procedure pNoParam

  as

  begin

  delete from t1;

  delete from t2;

  commit;

  exception

  when others then

  begin

  rollback;

  end;

  end pNoParam;

  2、有输入输出

  往t1表中插入一条记录,返回值表示插入是否成功。

  1)Informix

  create procedure pNormalParam(f1 integer, f2 varchar(10))

  returning integer;

  begin

  on exception

  rollback work;

  return -1;

  end exception

  begin work;

  insert into t1 values(f1, f2);

  commit work;

  return 0;

  2)Oracle

  create or replace procedure pNormalParam(f1 number,

  f2 varchar2, v_Result out number)

  as

  begin

  insert into t1 values(f1,f2);

  commit;

  v_Result = 0;

  return;

  exception

  when others then

  begin

  rollback;

  v_Result := -1;

  end;

  end pNormalParam;

  需要注意的是,在oracle存储过程中,参数是不能加上size的,比如f1,在t1表中该字段是number(10,0),而这里只能写number,而不能写number(10,0)。

  

  

  3、返回记录集

  1)Informix

  create procedure pReturnSet() returning integer, varchar(10);

  define i integer;

  define j varchar(10);

  foreach

  select f1, f2 into i, j from t1

  return i, j with resume;

  end foreach;

  end procedure;

  2)Oracle

  create or replace package TestRefCursorPkg as type TestRefCursorTyp is ref cursor; procedure pReturnSet(RefCursor out TestRefCursorTyp); end TestRefCursorPkg;

  create or replace package body TestRefCursorPkg as

  procedure pReturnSet (RefCursor out TestRefCursorTyp)

  as

  localCursor TestRefCursorTyp;

  begin

  open localCursor for select f1, f2 from t1;

  RefCursor := localCursor;

  end pReturnSet;

  end TestRefCursorPkg;

  /

  四、其他差异说明

  1、错误捕捉

  1)Informix使用

 

  on exception

  end exception

  2)Oracle

  使用

  exception

  when others then

  2、对游标的处理

  1)Informix

  create procedure pHasCursor()

  define v_f1 integer;

  begin

  on exception

  rollback work;

  return;

  end exception

  begin work;

  foreach curt1 with hold for

  select f1 into v_f1 from t1 -- 注意这里没有分号

  if (v_f1 = 1) then

  update t1 set f2 = 'one' where current of curt1;

  elif (v_f1 = 2) then

  update t1 set f2 = 'two' where current of curt1;

  else

  update t1 set f2 = 'others' where current of curt1;

  end if;

  end foreach;

  commit work;

  end;

  end procedure;

  2)Oracle

  create or replace procedure pHasCursor

  as

  v_f1 number(10,0);

  cursor curt1 is

  select f1 from t1 for update;

  begin

  open curt1;

  loop

  fetch curt1 into v_f1;

  exit when curt1%notfound;

  if (v_f1 = 1) then

  update t1 set f2 = 'one' where current of curt1;

  elsif (v_f1 = 2) then

  update t1 set f2 = 'two' where current of curt1;

  else

  update t1 set f2 = 'others' where current of curt1;

  end if;

  end loop;

  commit;

  return;

  exception

  when others then

  begin

  rollback;

  end;

  end pHasCursor;

  3、在存储过程中调用另外一个存储过程

  1)Informix

  Call pNoParam();

  Call pNormalParam(1, ‘a’) returning v_Result;

  2)Oracle

  pNoParam;

  pNormalParam(1, ‘a’, v_Result);

  4、日期操作

  1)当前时间

  ① Informix

  define cur_dtime_var datetime year to second;

  当前日期时间: let cur_dtime_var = current; -- datetime

  ② Oracle

  Currtime date;

  Currtime := sysdate;

  2)当前日期的增减

  ① Informix

  let tmp_date = today + 3 UNITS day; -- 当前时间加三天

  let tmp_datetime = current + 1 UNITS second; -- 当前时间加1秒种

  ② Oracle

  Tmp_date := sysdate + 3; -- 当前时间加三天

  Tmp_date := sysdate + 1/24/3600; --当前时间加1秒种

  3)日期转换成字符串

  ① Informix

  let v_PeriodEndTime = year(v_date)||extend(v_date,month to month)

  ||extend(v_date,day to day) ||extend(v_date,hour to hour)

  ||extend(v_date,minute to minute)|| extend(v_date,second to second);

  ② Oracle

  v_PeriodEndTime := to_char(v_date, 'yyyymmddhh24miss');

   4)字符串转换成日期

  假设字符串的形式是yyyymmddhhmiss形式的

  ① Informix

  -- 直接转换成日期

  let v_BeginDate = substr(v_BeginTime,1,4)||'-'||substr(v_BeginTime,5,2)

  ||'-'||substr(v_BeginTime,7,2)||' '||substr(v_BeginTime,9,2)

  ||':'||substr(v_BeginTime,11,2)||':'||substr(v_BeginTime,13,2);

  -- 这个月的第一天

  let v_date = substr(v_BeginTime,1,4)||'-'

  ||substr(v_BeginTime,5,2)||'-1 00:00:00';

  -- 这个星期的第一天

  let v_date = substr(v_BeginTime,1,4)||'-'||substr(v_BeginTime,5,2)

  ||'-'||substr(v_BeginTime,7,2)||' 00:00:00';

  let v_week = weekday(v_date);

  let v_date = v_date - v_week UNITS day;

  ② Oracle

  -- 直接转换成日期

  v_BeginDate := to_date(v_BeginTime, 'yyyymmddhh24miss');

  -- 这个月的第一天

  v_BeginDate := trunc(to_date(v_BeginTime, 'yyyymmddhh24miss'), ‘mm’);

  -- 这个星期的第一天

  v_BeginDate := trunc(to_date(v_BeginTime, 'yyyymmddhh24miss'), ‘day’);

  5)事务

  在oracle中缺省情况下,一个事务的结束就是下一个事务的开始,所以对于一个事务来说,我们只要写commit;即可,不需要明确标出什么时候开始一个事务,而informix需要。

  6)打印调试信息

  7)Informix

  --设置跟踪模式

  set debug file to "trace_check"; -- with append;

  --说明“with append”表示以追加模式打开跟踪结果文件

  trace '开始执行存储过程'

  trace 'v_date='||v_date;

  trace ‘存储过程执行完毕’

  trace off;

  执行完以后打开当前目录下的trace_check即可看到打印出来的信息。

  8)Oracle

  DBMS_OUTPUT.PUT_LINE(‘开始执行存储过程’);

  DBMS_OUTPUT.PUT_LINE('v_date='||v_date);

  DBMS_OUTPUT.PUT_LINE(‘存储过程执行完毕’);

  先设置一下缓冲区的大小

  set serveroutput on size 100000; -- 如果不执行该语句,会看不到调试信息

  执行完毕以后,打印出来的信息就会直接显示在界面上。

  5、关于参数的说明

  如果存储过程想返回一个参数,在informix中是通过返回值的形式实现的,而在oracle是通过输出参数或者输入输出参数实现的。

  举例:

  1)Informix:

  create procedure p1() returning integer;

  return 0;

  end procedure;

  2)oracle:

  create or replace procedure p1(x out number)

  as

  begin

  x := 0;

  end p1;

  6、赋值

  1)informix

  let v_1 = 100;

  2)oracle

  v_1 := 100;

  7、if语句

  1)informix

  if (v_1 =100) then

  elif (v_1=200) then

  Else

  end if;

  2)oracle

  if (v_1 =100) then

  elsif (v_1=200) then

  Else

  end if;

Statement of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1664
14
PHP Tutorial
1268
29
C# Tutorial
1243
24
What to do if the oracle can't be opened What to do if the oracle can't be opened Apr 11, 2025 pm 10:06 PM

Solutions to Oracle cannot be opened include: 1. Start the database service; 2. Start the listener; 3. Check port conflicts; 4. Set environment variables correctly; 5. Make sure the firewall or antivirus software does not block the connection; 6. Check whether the server is closed; 7. Use RMAN to recover corrupt files; 8. Check whether the TNS service name is correct; 9. Check network connection; 10. Reinstall Oracle software.

How to solve the problem of closing oracle cursor How to solve the problem of closing oracle cursor Apr 11, 2025 pm 10:18 PM

The method to solve the Oracle cursor closure problem includes: explicitly closing the cursor using the CLOSE statement. Declare the cursor in the FOR UPDATE clause so that it automatically closes after the scope is ended. Declare the cursor in the USING clause so that it automatically closes when the associated PL/SQL variable is closed. Use exception handling to ensure that the cursor is closed in any exception situation. Use the connection pool to automatically close the cursor. Disable automatic submission and delay cursor closing.

How to create cursors in oracle loop How to create cursors in oracle loop Apr 12, 2025 am 06:18 AM

In Oracle, the FOR LOOP loop can create cursors dynamically. The steps are: 1. Define the cursor type; 2. Create the loop; 3. Create the cursor dynamically; 4. Execute the cursor; 5. Close the cursor. Example: A cursor can be created cycle-by-circuit to display the names and salaries of the top 10 employees.

What steps are required to configure CentOS in HDFS What steps are required to configure CentOS in HDFS Apr 14, 2025 pm 06:42 PM

Building a Hadoop Distributed File System (HDFS) on a CentOS system requires multiple steps. This article provides a brief configuration guide. 1. Prepare to install JDK in the early stage: Install JavaDevelopmentKit (JDK) on all nodes, and the version must be compatible with Hadoop. The installation package can be downloaded from the Oracle official website. Environment variable configuration: Edit /etc/profile file, set Java and Hadoop environment variables, so that the system can find the installation path of JDK and Hadoop. 2. Security configuration: SSH password-free login to generate SSH key: Use the ssh-keygen command on each node

What to do if the oracle log is full What to do if the oracle log is full Apr 12, 2025 am 06:09 AM

When Oracle log files are full, the following solutions can be adopted: 1) Clean old log files; 2) Increase the log file size; 3) Increase the log file group; 4) Set up automatic log management; 5) Reinitialize the database. Before implementing any solution, it is recommended to back up the database to prevent data loss.

Oracle's Role in the Business World Oracle's Role in the Business World Apr 23, 2025 am 12:01 AM

Oracle is not only a database company, but also a leader in cloud computing and ERP systems. 1. Oracle provides comprehensive solutions from database to cloud services and ERP systems. 2. OracleCloud challenges AWS and Azure, providing IaaS, PaaS and SaaS services. 3. Oracle's ERP systems such as E-BusinessSuite and FusionApplications help enterprises optimize operations.

How to stop oracle database How to stop oracle database Apr 12, 2025 am 06:12 AM

To stop an Oracle database, perform the following steps: 1. Connect to the database; 2. Shutdown immediately; 3. Shutdown abort completely.

How to export oracle view How to export oracle view Apr 12, 2025 am 06:15 AM

Oracle views can be exported through the EXP utility: Log in to the Oracle database. Start the EXP utility, specifying the view name and export directory. Enter export parameters, including target mode, file format, and tablespace. Start exporting. Verify the export using the impdp utility.

See all articles