PL/SQL 将名称-值对转换为单行 table
PL/SQL converts name-value pairs to single row table
使用 PL/SQL,我如何转换 table 的名称-值对...
Name Value
--------- -------
Firstname Bob
Surname Smith
Address1 101 High Street
City London
Country UK
...单行 table 像这样:
Firstname Surname Address1 City Country
--------- ------- --------------- ------ -------
Bob Smith 101 High Street London UK
实际上我需要的是将名称-值对转换为 SYS_REFCURSOR 作为上面的单行 table
完整的故事是:
我收到的数据是像这样的单个字符串 (Firstname;Bob;Surname;SmithAddress1;101 High Street;City;London;Country;UK) 我需要的只是将其转换为 SYS_REFCURSOR
我从创建类型开始
TYPE order_type IS TABLE OF VARCHAR2(255) INDEX BY VARCHAR2(255);
neworder order_type;
到目前为止,我设法使用名称-值对做到了这一点
这是个好方法吗?
create table t1 (name varchar2(20), value varchar2(20));
insert into t1 values('Firstname','Bob');
insert into t1 values('Surname','Smith');
insert into t1 values('Address1','101 High Street');
insert into t1 values('City','London');
insert into t1 values('Country','UK');
select * from t1 pivot (max(value) for (name) in ('Firstname' as firstname, 'Surname' as surname, 'Address1' as address, 'City' as city, 'Country' as country));
这样SQL将列数据转换为一行。
如果您将数据作为具有固定顺序的分隔字符串获取,我将使用 instr('Firstname;Bob;Surname;Smith;Address1;101 High Street;City;London;Country;UK',';',1)
和 substring
函数的组合对其进行解析。
类似于:
declare
text varchar2(200) := 'Firstname;Bob;Surname;Smith;Address1;101 High Street;City;London;Country;UK';
firstname varchar2(40);
surname varchar2(40);
address varchar2(40);
city varchar2(40);
country varchar2(40);
begin
firstname := substr(text,instr(text,';',1) + 1,instr(text,';',2) - instr(text,';',1));
surname := substr(text,instr(text,';',3) + 1, instr(text,';',4) - instr(text,';',3));
....
end;
遇到了一个我不知道的有趣实用程序。 dbms_utility.comma_to_table。 http://mohsoracle.blogspot.com/2010/04/oracle-breaking-comma-separated-string.html
因此,如果您想将它用于 return 来自定界字符串的 sys ref 游标,可能类似于。
/* Formatted on 10/28/2016 9:42:52 AM (QP5 v5.256.13226.35510) */
CREATE OR REPLACE PROCEDURE myproc (v_cur OUT SYS_REFCURSOR)
IS
lv_Str_List VARCHAR2 (1000)
:= 'Firstname, Bob, Surname, Smith, Address, 101 High Street, City, London, Country, UK';
lb_cnt BINARY_INTEGER;
la_Tab_Str DBMS_UTILITY.UNCL_ARRAY;
BEGIN
lv_Str_List := '"' || REPLACE (lv_Str_List, ',', '","') || '"';
-- parse the string into comma separated table
DBMS_UTILITY.COMMA_TO_TABLE (lv_Str_List, lb_cnt, la_Tab_Str);
FOR i IN 1 .. la_Tab_Str.COUNT
LOOP
-- display substring
DBMS_OUTPUT.PUT_LINE (TRIM (la_Tab_Str (i)));
END LOOP;
OPEN v_cur FOR
SELECT *
FROM (SELECT REPLACE (TRIM (la_Tab_Str (2)), '"', '') AS Firstname,
REPLACE (TRIM (la_Tab_Str (4)), '"', '') AS Surname,
REPLACE (TRIM (la_Tab_Str (6)), '"', '') AS Address,
REPLACE (TRIM (la_Tab_Str (8)), '"', '') AS City,
REPLACE (TRIM (la_Tab_Str (10)), '"', '') AS Country
FROM DUAL) t;
END;
我在字符串中进行了硬编码,但它可以作为变量出现,然后用逗号替换分号。
当我运行下面在toad中测试一下。
DECLARE
V_CUR SYS_REFCURSOR;
BEGIN
V_CUR := NULL;
MYPROC ( V_CUR );
:to_grid := V_CUR;
COMMIT;
END;
我得到了
FIRSTNAME SURNAME ADDRESS CITY COUNTRY
Bob Smith 101 High Street London UK
使用 PL/SQL,我如何转换 table 的名称-值对...
Name Value
--------- -------
Firstname Bob
Surname Smith
Address1 101 High Street
City London
Country UK
...单行 table 像这样:
Firstname Surname Address1 City Country
--------- ------- --------------- ------ -------
Bob Smith 101 High Street London UK
实际上我需要的是将名称-值对转换为 SYS_REFCURSOR 作为上面的单行 table
完整的故事是: 我收到的数据是像这样的单个字符串 (Firstname;Bob;Surname;SmithAddress1;101 High Street;City;London;Country;UK) 我需要的只是将其转换为 SYS_REFCURSOR
我从创建类型开始
TYPE order_type IS TABLE OF VARCHAR2(255) INDEX BY VARCHAR2(255);
neworder order_type;
到目前为止,我设法使用名称-值对做到了这一点
这是个好方法吗?
create table t1 (name varchar2(20), value varchar2(20));
insert into t1 values('Firstname','Bob');
insert into t1 values('Surname','Smith');
insert into t1 values('Address1','101 High Street');
insert into t1 values('City','London');
insert into t1 values('Country','UK');
select * from t1 pivot (max(value) for (name) in ('Firstname' as firstname, 'Surname' as surname, 'Address1' as address, 'City' as city, 'Country' as country));
这样SQL将列数据转换为一行。
如果您将数据作为具有固定顺序的分隔字符串获取,我将使用 instr('Firstname;Bob;Surname;Smith;Address1;101 High Street;City;London;Country;UK',';',1)
和 substring
函数的组合对其进行解析。
类似于:
declare
text varchar2(200) := 'Firstname;Bob;Surname;Smith;Address1;101 High Street;City;London;Country;UK';
firstname varchar2(40);
surname varchar2(40);
address varchar2(40);
city varchar2(40);
country varchar2(40);
begin
firstname := substr(text,instr(text,';',1) + 1,instr(text,';',2) - instr(text,';',1));
surname := substr(text,instr(text,';',3) + 1, instr(text,';',4) - instr(text,';',3));
....
end;
遇到了一个我不知道的有趣实用程序。 dbms_utility.comma_to_table。 http://mohsoracle.blogspot.com/2010/04/oracle-breaking-comma-separated-string.html
因此,如果您想将它用于 return 来自定界字符串的 sys ref 游标,可能类似于。
/* Formatted on 10/28/2016 9:42:52 AM (QP5 v5.256.13226.35510) */
CREATE OR REPLACE PROCEDURE myproc (v_cur OUT SYS_REFCURSOR)
IS
lv_Str_List VARCHAR2 (1000)
:= 'Firstname, Bob, Surname, Smith, Address, 101 High Street, City, London, Country, UK';
lb_cnt BINARY_INTEGER;
la_Tab_Str DBMS_UTILITY.UNCL_ARRAY;
BEGIN
lv_Str_List := '"' || REPLACE (lv_Str_List, ',', '","') || '"';
-- parse the string into comma separated table
DBMS_UTILITY.COMMA_TO_TABLE (lv_Str_List, lb_cnt, la_Tab_Str);
FOR i IN 1 .. la_Tab_Str.COUNT
LOOP
-- display substring
DBMS_OUTPUT.PUT_LINE (TRIM (la_Tab_Str (i)));
END LOOP;
OPEN v_cur FOR
SELECT *
FROM (SELECT REPLACE (TRIM (la_Tab_Str (2)), '"', '') AS Firstname,
REPLACE (TRIM (la_Tab_Str (4)), '"', '') AS Surname,
REPLACE (TRIM (la_Tab_Str (6)), '"', '') AS Address,
REPLACE (TRIM (la_Tab_Str (8)), '"', '') AS City,
REPLACE (TRIM (la_Tab_Str (10)), '"', '') AS Country
FROM DUAL) t;
END;
我在字符串中进行了硬编码,但它可以作为变量出现,然后用逗号替换分号。
当我运行下面在toad中测试一下。
DECLARE
V_CUR SYS_REFCURSOR;
BEGIN
V_CUR := NULL;
MYPROC ( V_CUR );
:to_grid := V_CUR;
COMMIT;
END;
我得到了
FIRSTNAME SURNAME ADDRESS CITY COUNTRY
Bob Smith 101 High Street London UK