1.定义: type ref_cur is ref cursor; 2.动态cursor作为out参数 存储过程的实现 举个最简单的例子,根据table name动态获取cursor PROCEDURE P_GET_CUR(I_TABLE_NAME IN VARCHAR2, O_REF_CUR OUT REF_CUR) AS BEGIN IF UPPER(I_TABLE_NAME) = 'T_BANK_ACCOU
1.定义:
type ref_cur is ref cursor;
2.动态cursor作为out参数 存储过程的实现
举个最简单的例子,根据table name动态获取cursor
PROCEDURE P_GET_CUR(I_TABLE_NAME IN VARCHAR2, O_REF_CUR OUT REF_CUR) AS
BEGIN
IF UPPER(I_TABLE_NAME) = 'T_BANK_ACCOUNT' THEN
OPEN O_REF_CUR FOR
SELECT BANK_ACCOUNT
FROM T_BANK_ACCOUNT
WHERE BANK_ACCOUNT IS NOT NULL;
ELSIF UPPER(I_TABLE_NAME) = 'T_FB_PAYMENT_JP' THEN
OPEN O_REF_CUR FOR
SELECT BANK_ACCOUNT
FROM T_FB_PAYMENT_JP
WHERE BANK_ACCOUNT IS NOT NULL;
END IF;
END P_GET_CUR;
3.调用,用项目中mask 口座番号的功能为例,跟大家分享下:
procedure p_bank_acco_mask(i_table_name in varchar2,
i_coulumn in varchar,
o_error_str in out varchar2,
o_result in out number,
m_has_exception in out boolean) as
v_update_sql varchar(800);
v_update_sql2 varchar(800);
i int := 1;
v_bank_account VARCHAR2(50);
v_bank_account_new VARCHAR2(50);
v_char VARCHAR(1);
v_bank_account_curs ref_cur;
v_select_sql VARCHAR2(800);
begin
o_result := PKG_LS_PUB_CODE_CST.BATCH_RESULT__SUCCESS;
BEGIN
p_get_cur(i_table_name,v_bank_account_curs);
savepoint point;
v_update_sql2 := '';
LOOP
FETCH v_bank_account_curs INTO v_bank_account;
EXIT WHEN v_bank_account_curs%NOTFOUND;
BEGIN
for i in 1 .. length(v_bank_account) LOOP
v_select_sql:='SELECT SUBSTR('||i_coulumn||','||i||',1) from '|| i_table_name ||' where '||i_coulumn||'='''||v_bank_account||''' and rownum
v_char:=f_get_char(v_select_sql);
IF v_char IS NULL THEN
RETURN;
END IF;
IF v_char='2' OR v_char='3' THEN--1
v_char:='1';
ELSIF v_char='5' OR v_char='6' THEN--4
v_char:='4';
ELSIF v_char='8' OR v_char='9' THEN--7
v_char:='7';
END IF;
v_bank_account_new:=v_bank_account_new||v_char;
END LOOP;
--should support bank_code='9900'
if i_table_name = 'T_DT_ACCOUNT_JP' then
v_update_sql := ' update ' || i_table_name || ' set ' || i_coulumn ||
' = decode( Head_Bank_Code ' || ',' ||
'''9900''' || ',' || '''12340-01111281''' || ',' ||
v_bank_account_new ||' ) where' || i_coulumn ||'='''|| v_bank_account||'''';
elsif i_table_name = 'T_CASH_BANK' then
v_update_sql := ' update ' || i_table_name || ' set ' || i_coulumn ||
' = decode( CASH_BANK ' || ',' ||
'''9900''' || ',' || '''12340-01111281''' || ',' ||
v_bank_account_new ||' ) where' || i_coulumn ||'='''|| v_bank_account||'''';
elsif i_table_name = 'T_CASH_BANK_LOG' then
v_update_sql := ' update ' || i_table_name || ' set ' || i_coulumn ||
' = decode( CASH_BANK ' || ',' ||
'''9900''' || ',' || '''12340-01111281''' || ',' ||
v_bank_account_new ||' ) where' || i_coulumn ||'='''|| v_bank_account||'''';
elsif i_table_name = 'T_AGM_AGENT' then
v_update_sql := ' update ' || i_table_name ||
' set COMM_ACCOUNT = ' || v_bank_account_new ||'' ||
' where COMM_PAY_BANK_CODE ''9900'' and '|| i_coulumn ||'='''|| v_bank_account||'''';
v_update_sql2 := ' update ' || i_table_name ||
' set COMM_PAY_BRANCH_CODE =''12340'', ' ||
' COMM_ACCOUNT = ''01111281'' ' ||
' where COMM_PAY_BANK_CODE = ''9900''';
elsif i_table_name = 'T_SHOSHIHARAIRIREKIJOUHOU_TBL' then
v_update_sql := ' update ' || i_table_name || ' set ' || i_coulumn ||
' = decode( substr(BANK_ACCOUNT_NO,2,4) ' ||
',' || '''9900''' || ',' || '''12340-01111281''' ||
',' || v_bank_account_new ||' ) where' || i_coulumn ||'='''|| v_bank_account||'''';
elsif i_table_name = 'T_NYUSHUTUKINRIREKIJOUHOU' then
v_update_sql := ' update ' || i_table_name || ' set ' || i_coulumn ||
' = decode( substr(KOUZA_NO,2,4) ' ||
',' || '''9900''' || ',' || '''12340-01111281''' ||
',' || v_bank_account_new ||' ) where' || i_coulumn ||'='''|| v_bank_account||'''';
elsif i_table_name = 'T_ACCOUNT_APPLY_TBL' then
v_update_sql := ' update ' || i_table_name || ' set ' || i_coulumn ||
' = decode( substr(ACCOUNT_NO,1,4) ' ||
',' || '''9900''' || ',' || '''12340-01111281''' ||
',' || v_bank_account_new ||' ) where' || i_coulumn ||'='''|| v_bank_account||'''';
elsif i_table_name = 'T_LSIF_OTHERS' then
v_update_sql := ' update ' || i_table_name || ' set KOUZAINO = ' ||
v_bank_account_new ||'' || ' where BANKCN ''9900'' and '|| i_coulumn ||'='''|| v_bank_account||'''';
v_update_sql2 := ' update ' || i_table_name ||
' set SITENCNJYO =''123'', ' ||
' SITENCNKA = ''40'', ' ||
' KOUZAINO = ''01111281'' ' ||
' where BANKCN = ''9900''';
elsif i_table_name = 'T_LSIF_GETUMATU_NENMATU_MASTER' then
v_update_sql := ' update ' || i_table_name || ' set KOUZAINO = ' ||
v_bank_account_new ||'' || ' where BANKCN ''9900'' and '|| i_coulumn ||'='''|| v_bank_account||'''';
v_update_sql2 := ' update ' || i_table_name ||
' set SITENCN =''12340'', ' ||
' KOUZAINO = ''01111281'' ' ||
' where BANKCN = ''9900''';
elsif i_table_name = 'T_LSIF_KAIKEI' then
v_update_sql := ' update ' || i_table_name || ' set WUZANO = ' ||
v_bank_account_new ||'' ||
' where GANKOWUMEYISHOUWUCN ''9900'' and '|| i_coulumn ||'='''|| v_bank_account||'''';
v_update_sql2 := ' update ' || i_table_name ||
' set SHITEYINMEYISYOUWUCN =''123'', ' ||
' WUZANO = ''01111281'' ' ||
' where GANKOWUMEYISHOUWUCN = ''9900''';
elsif i_table_name = 'T_LSIF_IDOUEXTR' and i_coulumn = 'KOUZAINO_ZEN' then
v_update_sql := ' update ' || i_table_name ||
' set KOUZAINO_ZEN = ' || v_bank_account_new ||'' ||
' where GINKOUCN_ZEN ''9900'' and '|| i_coulumn ||'='''|| v_bank_account||'''';
v_update_sql2 := ' update ' || i_table_name ||
' set SHITENCNE_ZEN =''123'', ' ||
' SHITENCNGE_ZEN = ''40'', ' ||
' KOUZAINO_ZEN = ''01111281'' ' ||
' where GINKOUCN_ZEN = ''9900''';
elsif i_table_name = 'T_LSIF_IDOUEXTR' and i_coulumn = 'KOUZAINO_GO' then
v_update_sql := ' update ' || i_table_name || ' set KOUZAINO_GO = ' ||
v_bank_account_new ||'' ||
' where GINKOUCN_GO ''9900'' and '|| i_coulumn ||'='''|| v_bank_account||'''';
v_update_sql2 := ' update ' || i_table_name ||
' set SHITENCNE_GO =''123'', ' ||
' SHITENCNGE_GO = ''40'', ' ||
' KOUZAINO_GO = ''01111281'' ' ||
' where GINKOUCN_GO = ''9900''';
elsif i_table_name = 'T_DIAGNOSIS_CHARGE_DATA' then
v_update_sql := ' update ' || i_table_name || ' set ACCOUNT_NO = ' ||
v_bank_account_new ||'' || ' where BANK_CN ''9900'' and '|| i_coulumn ||'='''|| v_bank_account||'''';
v_update_sql2 := ' update ' || i_table_name ||
' set BRANCH_CN =''123'', ' ||
' ACCOUNT_NO = ''01111281'' ' ||
' where BANK_CN = ''9900''';
elsif i_table_name = 'T_IFE_MONTHLY_GETUMATU_MASTER' then
v_update_sql := ' update ' || i_table_name || ' set ' || i_coulumn ||
' = decode( BANKCN ' || ',' ||
'''9900''' || ',' || '''12340-01111281''' || ',' ||
v_bank_account_new ||' ) where '|| i_coulumn ||'='''|| v_bank_account||'''';
else
v_update_sql := ' update ' || i_table_name || ' set ' || i_coulumn ||
' = decode( bank_code ' || ',' ||
'''9900''' || ',' || '''12340-01111281''' || ',' ||
v_bank_account_new ||' ) where '|| i_coulumn ||'='''|| v_bank_account||'''';
end if;
v_bank_account_new:='';--clear data
execute immediate v_update_sql;
if v_update_sql2 is not null then
execute immediate v_update_sql2;
end if;
pkg_pub_scd_ci.p_batch_commit();
EXCEPTION
when others then
rollback to point;
o_result := PKG_LS_PUB_CODE_CST.BATCH_RESULT__FAIL;
o_error_str := i_table_name || ',';
pkg_pub_scd_ci.p_log_error('p_bank_acco_mask ,failed to mask table:' ||
i_table_name || ',error info:' ||
sqlerrm || '-------update SQL=' ||
v_update_sql);
m_has_exception := true;
end;
END LOOP;
CLOSE v_bank_account_curs;
END;
end p_bank_acco_mask;
动态创建cursor的函数原理同上,其他带参数的cursor具体可以参考下面的:
--procedure返回记录集:
----------------------声明一个Package--------------
CREATE OR REPLACE PACKAGE pkg_test
AS
TYPEmyrctypeIS REF CURSOR;
PROCEDURE get_r(p_id NUMBER,p_rc OUT myrctype); --Package中声明名为get 的Procedure(只有接口没内容)
END pkg_test;
-----------------声明Package Body,即上面Package中的内容,包括Procedure get---------------------
CREATE OR REPLACE PACKAGE BODY pkg_test
AS
PROCEDURE get_r(p_id NUMBER,p_rc OUT myrctype)
IS
sqlstr VARCHAR2 (500);
BEGIN
IF p_id = 0 THEN
OPEN p_rc FOR
SELECT ID, NAME, sex, address, postcode, birthday
FROM student;
ELSE
sqlstr :=
'select id,name,sex,address,postcode,birthday
from student where id=:w_id'; --w_id是个参数,
--以下 p_rc是个REF CURSOR游标类型,而且是OUT型参数,即可返回一个记录集了。USING p_id就是替换上面SQL中:w_id值拉:)
OPEN p_rc FOR sqlstr USING p_id;
END IF;
END get;
END pkg_test;
--function返回记录集的例子,原理和上面相同,而是用function的return值来返回记录集。
函数返回记录集:
建立带ref cursor定义的包和包体及函数:
复制代码 代码如下:
CREATE OR REPLACE
package pkg_test as
type myrctype is ref cursor;
function get_r(intID number) return myrctype;
end pkg_test;
/
CREATE OR REPLACE
package body pkg_test as
--函数体
function get_r(intID number) return myrctype is
rc myrctype; --定义ref cursor变量
sqlstr varchar2(500);
begin
if intID=0 then
--静态测试,直接用select语句直接返回结果
open rc for select id,name,sex,address,postcode,birthday from student;
else
--动态sql赋值,用:w_id来申明该变量从外部获得
sqlstr := 'select id,name,sex,address,postcode,birthday from student where id=:w_id';
--动态测试,用sqlstr字符串返回结果,用using关键词传递参数
open rc for sqlstr using intid;
end if;
return rc;
end get;
end pkg_test;

MySQL is suitable for beginners to learn database skills. 1. Install MySQL server and client tools. 2. Understand basic SQL queries, such as SELECT. 3. Master data operations: create tables, insert, update, and delete data. 4. Learn advanced skills: subquery and window functions. 5. Debugging and optimization: Check syntax, use indexes, avoid SELECT*, and use LIMIT.

MySQL efficiently manages structured data through table structure and SQL query, and implements inter-table relationships through foreign keys. 1. Define the data format and type when creating a table. 2. Use foreign keys to establish relationships between tables. 3. Improve performance through indexing and query optimization. 4. Regularly backup and monitor databases to ensure data security and performance optimization.

MySQL is an open source relational database management system that is widely used in Web development. Its key features include: 1. Supports multiple storage engines, such as InnoDB and MyISAM, suitable for different scenarios; 2. Provides master-slave replication functions to facilitate load balancing and data backup; 3. Improve query efficiency through query optimization and index use.

SQL is used to interact with MySQL database to realize data addition, deletion, modification, inspection and database design. 1) SQL performs data operations through SELECT, INSERT, UPDATE, DELETE statements; 2) Use CREATE, ALTER, DROP statements for database design and management; 3) Complex queries and data analysis are implemented through SQL to improve business decision-making efficiency.

The basic operations of MySQL include creating databases, tables, and using SQL to perform CRUD operations on data. 1. Create a database: CREATEDATABASEmy_first_db; 2. Create a table: CREATETABLEbooks(idINTAUTO_INCREMENTPRIMARYKEY, titleVARCHAR(100)NOTNULL, authorVARCHAR(100)NOTNULL, published_yearINT); 3. Insert data: INSERTINTObooks(title, author, published_year)VA

The main role of MySQL in web applications is to store and manage data. 1.MySQL efficiently processes user information, product catalogs, transaction records and other data. 2. Through SQL query, developers can extract information from the database to generate dynamic content. 3.MySQL works based on the client-server model to ensure acceptable query speed.

The steps to build a MySQL database include: 1. Create a database and table, 2. Insert data, and 3. Conduct queries. First, use the CREATEDATABASE and CREATETABLE statements to create the database and table, then use the INSERTINTO statement to insert the data, and finally use the SELECT statement to query the data.

MySQL is suitable for beginners because it is easy to use and powerful. 1.MySQL is a relational database, and uses SQL for CRUD operations. 2. It is simple to install and requires the root user password to be configured. 3. Use INSERT, UPDATE, DELETE, and SELECT to perform data operations. 4. ORDERBY, WHERE and JOIN can be used for complex queries. 5. Debugging requires checking the syntax and use EXPLAIN to analyze the query. 6. Optimization suggestions include using indexes, choosing the right data type and good programming habits.


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

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SublimeText3 Mac version
God-level code editing software (SublimeText3)

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