Quantcast
Channel: dBforums – Everything on Databases, Design, Developers and Administrators
Viewing all articles
Browse latest Browse all 13329

Returning a cursor from a function to loop through and insert

$
0
0
Hi!

I'm working on a batch of queries and there is a requirement where I have to convert this SQL Server queries to similar-function Oracle.

SQL Server:
Code:

WITH Cte
    AS (SELECT cast('<S>' + replace(replace(N'$(AppServers)', ';', ','), ',', '</S><S>') + '</S>' AS XML) AS Servers)
INSERT INTO INSTANCE
            (INSTANCE_ID,
            SERVER_NAME,
            INSTANCE_IDENTIFIER,
            IDENTIFIER_PREFIX)
SELECT ROW_NUMBER() OVER (ORDER BY SERVER_NAME) - 1,
      SERVER_NAME,
      NULL,
      0
FROM  (SELECT DISTINCT upper(Split.Server.value('.', 'VARCHAR(100)')) AS SERVER_NAME
        FROM  Cte
              CROSS apply Servers.nodes('/S') Split(Server)) Servers
ORDER  BY SERVER_NAME;

Oracle:
Code:

DECLARE
  L_INPUT VARCHAR2(4000) := 'foo,bar,baz,wibble';
  L_COUNT BINARY_INTEGER;
  L_ARRAY DBMS_UTILITY.LNAME_ARRAY;
BEGIN
  DBMS_UTILITY.COMMA_TO_TABLE(LIST => REGEXP_REPLACE(L_INPUT, '(^|,)', '\1x'), TABLEN => L_COUNT, TAB => L_ARRAY);
  DBMS_OUTPUT.PUT_LINE(L_COUNT);
  FOR I IN 1 .. L_COUNT
  LOOP
    DBMS_OUTPUT.PUT_LINE('Element ' || TO_CHAR(I) || ' of array contains: ' || SUBSTR(L_ARRAY(I), 2));
    INSERT INTO INSTANCE VALUES (I, SUBSTR(L_ARRAY(I), 2), NULL, 0);
    COMMIT;
  END LOOP;
END;

Now, the requirement changed that we should have a function that will accept two values: CSV for columns, and CSV of CSV for values (multiple rows). Example:

SomeFunction(
"Directory,ID,Location,UserName,Password,Selector",
"(Directory=Voice Active Directory A,ID=VT-AD1,Location=Canada,UserName=admin,Password=passw0rd,Selector=AD1),(Directory=Voice Active Directory B,ID=VT-AD2,Location=https://beta-proxy.voice.com/VTadp/Proxy/[/url],UserName=admin,Password=passw0rd,Selector=AD2),(Directory=Voice Active Directory C,ID=VT-AD3,Location=https://final-proxy.voice.com/VTadp/Proxy/,UserName=admin,Password=passw0rd)"

The second parameter is a big one as it has all the row data. Also, it has the column names as well because the third row data has a missing selector column value and should be inserted as NULL. The main reason I want to return a resultset indicated by a pointer is that the target table to insert into can be any. It will be the caller's responsibility to send the correct data to the function and handle the returned collection in a loop for the desired table making this function very general. I'll manage that after getting the generic result set. I'll worry about how to navigate that result set once I can actually get it. Can anyone suggest a basic example? Thank you.

Viewing all articles
Browse latest Browse all 13329

Trending Articles