Home Common Problem How to write oracle stored procedures

How to write oracle stored procedures

Jul 24, 2019 pm 03:49 PM
oracle

oracle存储过程的写法是【CREATE OR REPLACE PROCEDURE 存储过程名(param1 in type,param2 out type)IS 变量1 类型(值范围);BEGIN select count...】。

How to write oracle stored procedures

推荐教程:oracle教程

1、存储过程的基本语法:

 CREATE OR REPLACE PROCEDURE 存储过程名(param1 in type,param2 out type)
  IS
  变量1 类型(值范围);
  变量2 类型(值范围);  
  BEGIN
    select count(*) into 变量1 from 表名 where 列名=param1;
    if (判断条件) then
      select 列名 into 变量2 from 表名 where 列名=param1;
      DBMS_OUTPUT.put_line('打印信息');
    Elsif (判断条件) then
      dbms_output.put_line('打印信息');
    Else
      Raise 异常名 (NO_DATA_FOUND);
    End if;
  Exception
      When others then
        Rollback;   
  END;
Copy after login

2、已命名的异常:

  命名的系统异常               产生原因
  ACCESS_INTO_NULL           未定义对象
  CASE_NOT_FOUND              CASE中若未包含相应的WHEN,并且没有设置ELSE时
  COLLECTION_IS_NULL           集合元素未初始化
  CURSER_ALREDAY_OPEN          游标已经打开
  DUP_VAL_NO_INDEX              唯一索引对应的列上有重复的列
  INVALID_CUSER               在不合法的游标上操作
  INVALID_NUMBER               内嵌的SQL语句不能将字符转换为数字
  NO_DATA_FOUND               使用select into 未返回行,或应用索引表未初始化
  TOO_MANY_ROWS                执行select into 时,结果集超过一行
  ZERO_DIVIDE                  除数为0
  SUBSCRIPT_BEYOND_COUNT          元素下标超过嵌套表或VARRAY的最大值
  SUBSCRIPT_OUTSIDE_LIMIT          使用嵌套表或VARRAY时,将下标指定为负数
  VALUE_ERROR               赋值时,变量长度不足以容纳实际数量
  LOGIN_DENIED               PL/SQL应用程序连接到ORACLE数据库时,提供了不正确的用户名和密码
  NO_LOGGED_ON                 PL/SQL应用程序在没有连接ORACLE数据库的情况下访问数据 
  PROGRAM_ERROR              PL/SQL内部问题,可能需要重装数据字典& PL/SQL系统包
  ROWTYPE_MISMATCH             宿主游标变量与PL/SQL游标变量的返回类型不兼容
  SELF_IS_NULL                 使用对象类型时,在NULL对象上调用对象方法
  STORAGE_ERROR                运行PL/SQL时,超出内存空间
  SYS_INVALID_ID              无效的ROWID字符串
  TIMEOUT_ON_RESOURCE             ORACLE在等待资源时超时
Copy after login

3、实际例子

2.1 没有参数的过程

create or replace procedure test_count 
 is 
  v_total int;
  v_date varchar(20);
 begin
    select count(*) into v_total from dual;
    select to_char(sysdate,'yyyy-mm-dd') into v_date from dual;     
    DBMS_OUTPUT.put_line('总人数:'||v_total);   
    DBMS_OUTPUT.put_line('当前时间:'||v_date);  
  end;
Copy after login

调用方法:

begin
    test_count;
end;
Copy after login

2.2 仅带传入参数的过程

 create or replace procedure test_count1(v_id in varchar2)
       as 
       v_name varchar(100);
       begin
         select c_name into v_name from tb_store where c_stono=v_id;
         DBMS_OUTPUT.put_line(v_id||'店的名称为:'||v_name);
        exception
         when no_data_found then dbms_output.put_line('no_data_found');
       end;
Copy after login

调用方法:

begin
 test_count1(11910);
end;
Copy after login

2.3 仅带输出参数的过程 

create or replace procedure test_count2(v_name out varchar2)
  is
  begin
        select c_name into v_name from tb_store where c_stono='1101';
    exception
        when no_data_found then dbms_output.put_line('no_data_found');
  end;
Copy after login

调用方法:

declare 
  v_name varchar(200);
  begin
    test_count2(v_name);
    dbms_output.put_line(v_name);
  end;
Copy after login

2.4 带输入参数和输出参数的存储过程

  create or replace procedure test_count3(v_id in int,v_name out varchar2) 
   is 
       begin   
            select c_name into v_name from tb_store where c_stono=v_id;
            dbms_output.put_line(v_name);
    exception
             when no_data_found then dbms_output.put_line('no_data_found');
  end;
Copy after login

调用方法:

  declare 
  v_name varchar(200);
  begin
    test_count3('1101',v_name);
  end;
Copy after login

The above is the detailed content of How to write oracle stored procedures. For more information, please follow other related articles on the PHP Chinese website!

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)

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.

How to paginate oracle database How to paginate oracle database Apr 11, 2025 pm 08:42 PM

Oracle database paging uses ROWNUM pseudo-columns or FETCH statements to implement: ROWNUM pseudo-columns are used to filter results by row numbers and are suitable for complex queries. The FETCH statement is used to get the specified number of first rows and is suitable for simple queries.

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.

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

How to create oracle dynamic sql How to create oracle dynamic sql Apr 12, 2025 am 06:06 AM

SQL statements can be created and executed based on runtime input by using Oracle's dynamic SQL. The steps include: preparing an empty string variable to store dynamically generated SQL statements. Use the EXECUTE IMMEDIATE or PREPARE statement to compile and execute dynamic SQL statements. Use bind variable to pass user input or other dynamic values ​​to dynamic SQL. Use EXECUTE IMMEDIATE or EXECUTE to execute dynamic SQL statements.

How to solve garbled code in oracle How to solve garbled code in oracle Apr 11, 2025 pm 10:09 PM

Oracle garbled problems can be solved by checking the database character set to ensure they match the data. Set the client character set to match the database. Convert data or modify column character sets to match database character sets. Use Unicode character sets and avoid multibyte character sets. Check that the language settings of the database and client are correct.