3、在Oracle中对存储过程的调用 (1)过程调用方式一 Sql代码 declare realsalemp.sal%type; realname varchar (40); realjob varchar (40); begin //过程调用开始 realsal:=1100; realname:= '' ; realjob:= 'CLERK' ; runbyparmeters(realsal,realname,rea
3、在Oracle中对存储过程的调用
(1)过程调用方式一
Sql代码
- declare
- realsal emp.sal%type;
- realname varchar(40);
- realjob varchar(40);
- begin //过程调用开始
- realsal:=1100;
- realname:='';
- realjob:='CLERK';
- runbyparmeters(realsal,realname,realjob);--必须按顺序
- DBMS_OUTPUT.PUT_LINE(REALNAME||' '||REALJOB);
- END; //过程调用结束
(2)过程调用方式二
Sql代码
- declare
- realsal emp.sal%type;
- realname varchar(40);
- realjob varchar(40);
- begin //过程调用开始
- realsal:=1100;
- realname:='';
- realjob:='CLERK';
- --指定值对应变量顺序可变
- runbyparmeters(sname=>realname,isal=>realsal,sjob=>realjob);
- DBMS_OUTPUT.PUT_LINE(REALNAME||' '||REALJOB);
- END; //过程调用结束
(3)过程调用方式三(SQL命令行方式下)
Sql代码
- 1、SQL>exec proc_emp('参数1','参数2');//无返回值过程调用
- 2、SQL>var vsal number
- SQL> exec proc_emp ('参数1',:vsal);// 有返回值过程调用
- 或者:call proc_emp ('参数1',:vsal);// 有返回值过程调用
4、JAVA调用Oracle存储过程
(1)不带输出参数情况,过程名称为pro1,参数个数1个,数据类型为整形数据
Java代码
- import java.sql. * ;
- public class ProcedureNoArgs{
- public static void main(String args[]) throws Exception{
- //加载Oracle驱动
- DriverManager.registerDriver( new oracle.jdbc.driver.OracleDriver());
- //获得Oracle数据库连接
- Connection conn = DriverManager.getConnection
- ("jdbc:oracle:thin:@MyDbComputerNameOrIP:1521:ORCL", sUsr, sPwd " );
- //创建存储过程的对象
- CallableStatement c = conn.divpareCall( " {call pro1(?)} " );
- //给Oracle存储过程的参数设置值 ,将第一个参数的值设置成188
- c.setInt( 1 , 188 );
- // 执行Oracle存储过程
- c.execute();
- conn.close();
- }
- }
(2)带输出参数的情况,过程名称为pro2,参数个数2个,数据类型为整形数据,返回值为整形类型。
Java代码
- import java.sql.*;
- public class ProcedureWithArgs {
- public static void main(String args[]) throws Exception{
- //加载Oracle驱动
- DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
- //获得Oracle数据库连接
- Connection conn = DriverManager.getConnection
- ("jdbc:oracle:thin:@MyDbComputerNameOrIP:1521:ORCL", sUsr, sPwd ");
- //创建Oracle存储过程的对象,调用存储过程
- CallableStatement c=conn.divpareCall("{call pro2(?,?)}");
- //给Oracle存储过程的参数设置值 ,将第一个参数的值设置成188
- c.setInt(1,188);
- //注册存储过程的第二个参数
- c.registerOutParameter(2,java.sql.Types.INTEGER);
- c.execute(); //执行Oracle存储过程
- //得到存储过程的输出参数值并打印出来
- System.out.println (c.getInt(2));
- conn.close();
- }
- }
二、 函数
1、基本语法规则
Sql代码
- create or replace function (Name in type, Name in type, ...)
- return number
- is
- Result number;
- begin
- return (Result);
- end ;
2、具体事例(查询出empno=7935的sal值)
Sql代码
- create or replace function ret_emp_sal(v_ename varchar2)
- return number
- is
- v_sal number(7,2);
- begin
- select nvl(sal,0) into v_sal from emp where lower(ename)=lower(v_ename);
- return v_sal;
- end;
3、函数调用:
Sql代码
- SQL> var vsla number
- SQL> call ret_emp_sal('7935') into :vsal;
4、与存储过程的区别
(1)返回值的区别,函数有1个返回值,而存储过程是通过参数返回的,可以有多个或者没有
(2)调用的区别,函数可以在查询语句中直接调用,而存储过程必须单独调用.
(3)使用场景的区别,函数一般情况下是用来计算并返回一个计算结果
而存储过程一般是用来完成特定的数据操作(比如修改、插入数据库表或执行某些DDL语句等等)
三、包
包用于组合逻辑相关的过程和函数,它由包规范和包体两个部分组成。包规范用于定义公用的常量、变量、过程
和函数,创建包规范可以使用CREATE PACKAGE命令,创建包体可以使用CREATE PACKAGE BODY.
1、创建包规范
Sql代码
- create package emp_pkg is
- procedure emp_update_ename(v_empno varchar2,v_ename varchar2);
- function emp_get_sal(v_empno varchar2) return number;
- end;
2、创建包体
Sql代码
- create or replace package body emp_pkg
- is
- // 存储过程
- procedure emp_update_ename
- (
- v_empno varchar2,
- v_ename varchar2
- )
- is
- vename varchar2(32);
- begin
- update emp set ename=v_ename where empno=v_empno;
- commit;
- select ename into vename from emp where empno=v_empno;
- dbms_output.put_line('雇员名称:'||vename);
- end;
- // 函数
- function emp_get_sal
- (
- v_empno varchar2
- )
- return number is
- vsal number(7,2);
- begin
- select sal into vsal from emp where empno=v_empno;
- return vsal;
- end;
- nd;
3、包调用
在没有创建包规范就创建包体,会失败,要使用包,必须先创建包规范,然后在创建包体。
当要调用包的过程和函数时,在过程和函数的名称前加上包名作为前缀(包名.子程序名称),
而如果要访问其他方案的包时需要在包的名称前加上方案的名称(方案名称.包名.子程序名称)。
(1)调用包函数
Sql代码
- SQL> var vsla number
- SQL> call emp_pkg.emp_get_sal('7935') into :vsal;
(2)调用包存储过程
Sql代码
- SQL> exec emp_pkg.emp_update_ename('7935','helong');

InnoDB uses redologs and undologs to ensure data consistency and reliability. 1.redologs record data page modification to ensure crash recovery and transaction persistence. 2.undologs records the original data value and supports transaction rollback and MVCC.

Key metrics for EXPLAIN commands include type, key, rows, and Extra. 1) The type reflects the access type of the query. The higher the value, the higher the efficiency, such as const is better than ALL. 2) The key displays the index used, and NULL indicates no index. 3) rows estimates the number of scanned rows, affecting query performance. 4) Extra provides additional information, such as Usingfilesort prompts that it needs to be optimized.

Usingtemporary indicates that the need to create temporary tables in MySQL queries, which are commonly found in ORDERBY using DISTINCT, GROUPBY, or non-indexed columns. You can avoid the occurrence of indexes and rewrite queries and improve query performance. Specifically, when Usingtemporary appears in EXPLAIN output, it means that MySQL needs to create temporary tables to handle queries. This usually occurs when: 1) deduplication or grouping when using DISTINCT or GROUPBY; 2) sort when ORDERBY contains non-index columns; 3) use complex subquery or join operations. Optimization methods include: 1) ORDERBY and GROUPB

MySQL/InnoDB supports four transaction isolation levels: ReadUncommitted, ReadCommitted, RepeatableRead and Serializable. 1.ReadUncommitted allows reading of uncommitted data, which may cause dirty reading. 2. ReadCommitted avoids dirty reading, but non-repeatable reading may occur. 3.RepeatableRead is the default level, avoiding dirty reading and non-repeatable reading, but phantom reading may occur. 4. Serializable avoids all concurrency problems but reduces concurrency. Choosing the appropriate isolation level requires balancing data consistency and performance requirements.

MySQL is suitable for web applications and content management systems and is popular for its open source, high performance and ease of use. 1) Compared with PostgreSQL, MySQL performs better in simple queries and high concurrent read operations. 2) Compared with Oracle, MySQL is more popular among small and medium-sized enterprises because of its open source and low cost. 3) Compared with Microsoft SQL Server, MySQL is more suitable for cross-platform applications. 4) Unlike MongoDB, MySQL is more suitable for structured data and transaction processing.

MySQL index cardinality has a significant impact on query performance: 1. High cardinality index can more effectively narrow the data range and improve query efficiency; 2. Low cardinality index may lead to full table scanning and reduce query performance; 3. In joint index, high cardinality sequences should be placed in front to optimize query.

The MySQL learning path includes basic knowledge, core concepts, usage examples, and optimization techniques. 1) Understand basic concepts such as tables, rows, columns, and SQL queries. 2) Learn the definition, working principles and advantages of MySQL. 3) Master basic CRUD operations and advanced usage, such as indexes and stored procedures. 4) Familiar with common error debugging and performance optimization suggestions, such as rational use of indexes and optimization queries. Through these steps, you will have a full grasp of the use and optimization of MySQL.

MySQL's real-world applications include basic database design and complex query optimization. 1) Basic usage: used to store and manage user data, such as inserting, querying, updating and deleting user information. 2) Advanced usage: Handle complex business logic, such as order and inventory management of e-commerce platforms. 3) Performance optimization: Improve performance by rationally using indexes, partition tables and query caches.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Dreamweaver Mac version
Visual web development tools

SublimeText3 English version
Recommended: Win version, supports code prompts!

Notepad++7.3.1
Easy-to-use and free code editor

Atom editor mac version download
The most popular open source editor

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.