While using REPLACE() to convert from half-width to full-width characters, In function, this cannot be possible without declaring 'CONTAINS SQL'

送分小仙女□ 提交于 2021-01-29 14:33:51

问题


we used REPLACE () to convert the character string passed as an argument from half-width characters to full-width characters (replace all half-width characters with REPLACE ()). Finally, a character string in which half-width characters are converted to full-width characters is returned as a result.

When creating this function, it cannot be used without declaring "CONTAINS SQL" (a compilation error occurs in the called procedure) in order to use it in the processing of other stored procedures. The result is different before and after the declaration.

    create or replace procedure zz_kit(
    out O_VALUE1 VARCHAR(20000), OUT O_VALUE2 VARCHAR(20000)    
    )
    SPECIFIC zz_kit

    P1:BEGIN
    --123456ABCDEF   ->123456ABCABC
    update TBL_MEM_ACC SET FILLER1 = FNC_NAME2('A');
    set O_VALUE1=FNC_NAME('A');
    set O_VALUE2=FNC_NAME2('A');
    END P1

By declaring 'CONTAINS SQL' and creating a FUNCTION, we were able to call a FUNCTION from a stored procedure with SQL such as updating a table

create or replace procedure zz_kit2(
out O_VALUE1 VARCHAR(20000), OUT O_VALUE2 VARCHAR(20000)    
)
SPECIFIC zz_kit

P1:BEGIN
--123456ABCDEF   ->123456ABCABC
update TBL_MEM_ACC SET FILLER1 = FNC_NAME2('A');
set O_VALUE1=FNC_NAME('A');
set O_VALUE2=FNC_NAME2('A');
END P1

By not declaring 'CONTAINS SQL' updating a table is not working

The statement was not processed because the function "FNC_NAME2" resolved to specific function "FNC_NAME2" that is not valid in the context where it is used... SQLCODE=-390,SQLSTATE=42887, DRIVER=4.24.92


create or replace procedure zz_kit2(
out O_VALUE1 VARCHAR(20000), OUT O_VALUE2 VARCHAR(20000)    
)
SPECIFIC zz_kit

P1:BEGIN
--123456ABCDEF   ->123456ABCABC
--update TBL_MEM_ACC SET FILLER1 = FNC_NAME2('A');
set O_VALUE1=FNC_NAME('A');
set O_VALUE2=FNC_NAME2('A');
END P1

when simply trying to store the result of a FUNCTION in a variable in a stored procedure as set O_VALUE1=FNC_NAME('A'), content returned will be the same, it is working


when running from command line

db2 "select FNC_NAME('1234ABCDE') from sysibm.sysdummy1"
db2 "select FNC_NAME2('1234ABCDE') from sysibm.sysdummy1"

when running functions as above, the result is different, whether the 'CONTAINS SQL' is declared or not. when 'CONTAINS SQL' is declared and deployed, not all are converted to full-width characters but the processing results are returned.

-------WITH CONTAINS SQL---------------------------------------------------------------------------

CREATE OR REPLACE FUNCTION FNC_NAME(
IN I_ORG_STR   VARCHAR(5000)
)
RETURNS VARCHAR(20000)
SPECIFIC FNC_NAME
LANGUAGE SQL
CONTAINS SQL

BEGIN
DECLARE V_D_CNT INTEGER;
DECLARE V_HD_CNT INTEGER;
DECLARE V_RESULT VARCHAR(20000) DEFAULT NULL;

DECLARE EXIT HANDLER FOR SQLEXCEPTION
BEGIN
RETURN V_RESULT;
END;

IF I_ORG_STR IS NULL OR I_ORG_STR = '' THEN
RETURN V_RESULT;
END IF;

SET V_RESULT = I_ORG_STR;
SET V_RESULT = REPLACE(V_RESULT, '  ', '');
SET V_RESULT = REPLACE(V_RESULT, ' ', '');

SET V_RESULT = TRANSLATE(V_RESULT);

SET V_RESULT = REPLACE(V_RESULT, 'a', 'A');
SET V_RESULT = REPLACE(V_RESULT, 'b', 'B');
SET V_RESULT = REPLACE(V_RESULT, 'c', 'C');
SET V_RESULT = REPLACE(V_RESULT, 'd', 'D');
SET V_RESULT = REPLACE(V_RESULT, 'e', 'E');

SET V_RESULT = REPLACE(V_RESULT, '0', '0');
SET V_RESULT = REPLACE(V_RESULT, '1', '1');
SET V_RESULT = REPLACE(V_RESULT, '2', '2');
SET V_RESULT = REPLACE(V_RESULT, '3', '3');
SET V_RESULT = REPLACE(V_RESULT, '4', '4');


SET V_D_CNT = POSSTR(V_RESULT, '.');
IF V_D_CNT > 0 THEN
SET V_RESULT = REPLACE(V_RESULT, '.','.');
END IF;

RETURN V_RESULT;
END

-----------WITHOUT CONTAINS SQL-----------------------------------------------------------------------

CREATE OR REPLACE FUNCTION FNC_NAME2(
IN I_ORG_STR   VARCHAR(5000)
)
RETURNS VARCHAR(20000)
SPECIFIC FNC_NAME2
LANGUAGE SQL
--CONTAINS SQL

BEGIN
DECLARE V_D_CNT INTEGER;
DECLARE V_HD_CNT INTEGER;
DECLARE V_RESULT VARCHAR(20000) DEFAULT NULL;

DECLARE EXIT HANDLER FOR SQLEXCEPTION
BEGIN
RETURN V_RESULT;
END;

IF I_ORG_STR IS NULL OR I_ORG_STR = '' THEN
RETURN V_RESULT;
END IF;

SET V_RESULT = I_ORG_STR;
SET V_RESULT = REPLACE(V_RESULT, '  ', '');
SET V_RESULT = REPLACE(V_RESULT, ' ', '');

SET V_RESULT = TRANSLATE(V_RESULT);

SET V_RESULT = REPLACE(V_RESULT, 'a', 'A');
SET V_RESULT = REPLACE(V_RESULT, 'b', 'B');
SET V_RESULT = REPLACE(V_RESULT, 'c', 'C');
SET V_RESULT = REPLACE(V_RESULT, 'd', 'D');
SET V_RESULT = REPLACE(V_RESULT, 'e', 'E');

SET V_RESULT = REPLACE(V_RESULT, '0', '0');
SET V_RESULT = REPLACE(V_RESULT, '1', '1');
SET V_RESULT = REPLACE(V_RESULT, '2', '2');
SET V_RESULT = REPLACE(V_RESULT, '3', '3');
SET V_RESULT = REPLACE(V_RESULT, '4', '4');


SET V_D_CNT = POSSTR(V_RESULT, '.');
IF V_D_CNT > 0 THEN
SET V_RESULT = REPLACE(V_RESULT, '.','.');
END IF;

RETURN V_RESULT;
END

We Used REPLACE () to convert the character string passed as an argument from half-width characters to full-width characters (replace all half-width characters with REPLACE ()). Finally, a character string in which half-width characters are converted to full-width characters is returned as a result. Question here is half-width characters can only be replace to full-width characters when declaring 'CONTAINS SQL'? in a function or procedure? is this the only option

env:linux, mini IIASenter image description here

First result is when function contains 'CONTAINS SQL' second result is when function doesn't contain 'CONTAINS SQL'

来源:https://stackoverflow.com/questions/60596094/while-using-replace-to-convert-from-half-width-to-full-width-characters-in-fu

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!