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;

TograntpermissionstonewMySQLusers,followthesesteps:1)AccessMySQLasauserwithsufficientprivileges,2)CreateanewuserwiththeCREATEUSERcommand,3)UsetheGRANTcommandtospecifypermissionslikeSELECT,INSERT,UPDATE,orALLPRIVILEGESonspecificdatabasesortables,and4)

ToaddusersinMySQLeffectivelyandsecurely,followthesesteps:1)UsetheCREATEUSERstatementtoaddanewuser,specifyingthehostandastrongpassword.2)GrantnecessaryprivilegesusingtheGRANTstatement,adheringtotheprincipleofleastprivilege.3)Implementsecuritymeasuresl

ToaddanewuserwithcomplexpermissionsinMySQL,followthesesteps:1)CreatetheuserwithCREATEUSER'newuser'@'localhost'IDENTIFIEDBY'password';.2)Grantreadaccesstoalltablesin'mydatabase'withGRANTSELECTONmydatabase.TO'newuser'@'localhost';.3)Grantwriteaccessto'

The string data types in MySQL include CHAR, VARCHAR, BINARY, VARBINARY, BLOB, and TEXT. The collations determine the comparison and sorting of strings. 1.CHAR is suitable for fixed-length strings, VARCHAR is suitable for variable-length strings. 2.BINARY and VARBINARY are used for binary data, and BLOB and TEXT are used for large object data. 3. Sorting rules such as utf8mb4_unicode_ci ignores upper and lower case and is suitable for user names; utf8mb4_bin is case sensitive and is suitable for fields that require precise comparison.

The best MySQLVARCHAR column length selection should be based on data analysis, consider future growth, evaluate performance impacts, and character set requirements. 1) Analyze the data to determine typical lengths; 2) Reserve future expansion space; 3) Pay attention to the impact of large lengths on performance; 4) Consider the impact of character sets on storage. Through these steps, the efficiency and scalability of the database can be optimized.

MySQLBLOBshavelimits:TINYBLOB(255bytes),BLOB(65,535bytes),MEDIUMBLOB(16,777,215bytes),andLONGBLOB(4,294,967,295bytes).TouseBLOBseffectively:1)ConsiderperformanceimpactsandstorelargeBLOBsexternally;2)Managebackupsandreplicationcarefully;3)Usepathsinst

The best tools and technologies for automating the creation of users in MySQL include: 1. MySQLWorkbench, suitable for small to medium-sized environments, easy to use but high resource consumption; 2. Ansible, suitable for multi-server environments, simple but steep learning curve; 3. Custom Python scripts, flexible but need to ensure script security; 4. Puppet and Chef, suitable for large-scale environments, complex but scalable. Scale, learning curve and integration needs should be considered when choosing.

Yes,youcansearchinsideaBLOBinMySQLusingspecifictechniques.1)ConverttheBLOBtoaUTF-8stringwithCONVERTfunctionandsearchusingLIKE.2)ForcompressedBLOBs,useUNCOMPRESSbeforeconversion.3)Considerperformanceimpactsanddataencoding.4)Forcomplexdata,externalproc


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

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

Hot Article

Hot Tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

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.

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

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.
