SQL XML
SQL XML Contents
Summary DBMS_XMLQuery package bind values to produce more than one xml file
I'm using Oracle 8.1.7 and the XDK 9.0.1.1.0A for PL/SQL
You can get the latest version of the XDK at OTN
One common problem that I face in some applications is the need to produce different xml files based on the same query, for instance you want to produce a file with all the orders for a particular customer. One approach would be to make a procedure that receives a parameter with the customer number and produce the xml file for that customer.
But sometimes you need to make this for several customers in a row, off course that you can call that procedure several times, but that will fall in some overhead of creating the context every time and parsing the SQL statement.
Or you can use this approach, what I've made is the following:
I created one object that will work like an parameter array for the procedure.
CREATE OR REPLACE TYPE t_cust IS TABLE OF NUMBER;
Now you can have a procedure that get as parameter the object t_cust
CREATE OR REPLACE PROCEDURE SeveralResults (p_cust IN t_cust) IS queryCtx DBMS_XMLquery.ctxType; result CLOB; i NUMBER := 1; BEGIN queryCtx := DBMS_XMLQuery.newContext('SELECT * FROM Orders WHERE CustID = :pCust'); FOR i IN 1..p_cust.COUNT LOOP --Bind the value of i to pCust on the queryCtx DBMS_XMLQuery.setBindValue(queryCtx,'pCust',p_cust(i)); result := DBMS_XMLQuery.getXML(queryCtx); Clob2table(result); END LOOP; DBMS_XMLQuery.closeContext(queryCtx); END;
CREATE OR REPLACE PROCEDURE SeveralResults (p_cust IN t_cust) IS
queryCtx DBMS_XMLquery.ctxType; result CLOB; i NUMBER := 1;
queryCtx DBMS_XMLquery.ctxType;
result CLOB;
i NUMBER := 1;
BEGIN
queryCtx := DBMS_XMLQuery.newContext('SELECT * FROM Orders WHERE CustID = :pCust'); FOR i IN 1..p_cust.COUNT LOOP --Bind the value of i to pCust on the queryCtx DBMS_XMLQuery.setBindValue(queryCtx,'pCust',p_cust(i)); result := DBMS_XMLQuery.getXML(queryCtx); Clob2table(result); END LOOP; DBMS_XMLQuery.closeContext(queryCtx);
queryCtx := DBMS_XMLQuery.newContext('SELECT * FROM Orders WHERE CustID = :pCust');
FOR i IN 1..p_cust.COUNT LOOP
--Bind the value of i to pCust on the queryCtx DBMS_XMLQuery.setBindValue(queryCtx,'pCust',p_cust(i)); result := DBMS_XMLQuery.getXML(queryCtx); Clob2table(result);
--Bind the value of i to pCust on the queryCtx
DBMS_XMLQuery.setBindValue(queryCtx,'pCust',p_cust(i));
result := DBMS_XMLQuery.getXML(queryCtx);
Clob2table(result);
END LOOP;
DBMS_XMLQuery.closeContext(queryCtx);
END;
Now you can call the procedure SeveralResults like this
SEVERALRESULTS
This would produce two diferent clobs one for each customer passed on as parameter.
Pedro Gil
Partners
Dream.In.Code dotNet Slackers dotNet Spider Your HTML Source VisualBuilder.com DevGuru Planet Source Code ZVON.ORG Web Design ASPAlliance XML Pitstop Scripts
The Spot 4 SAP Bitshop Web Hosting