views:

63

answers:

1

I have a select query on a relational table in a plsql procedure.

I want to convert the results of this query into a user defined type object to return via odp.net.

How would I go about doing this?

+3  A: 

(this is from one of my other post today) this is a walkthrough on getting started: http://www.oracle.com/technology/obe/hol08/dotnet/udt/udt_otn.htm

while this is a bit more detailed: http://download.oracle.com/docs/html/E10927_01/featUDTs.htm

but the real meat & potatoes are already installed on your computer after you install ODP in the Samples directory: %ORA_HOME%\product\11.1.0\client_1\odp.net\samples\2.x\UDT

but the pl/sql side of things:

First create the singleton udt to handle one row at a time

 CREATE TYPE TESTTYPE IS OBJECT(COLA VARCHAR2(50) , COLB NUMBER(10) );
 create or replace procedure GetTestType(lTestType OUT NOCOPY TESTTYPE)
 IS
  BEGIN
     SELECT TESTTYPE('ValA',123) 
       INTO LTESTTYPE
       FROM DUAL ;
  END GetTestType ;

follow the directions in the above links to get the .net side insynch

NOW FOR A COLLECTION:

CREATE TYPE TESTTYPETABLE IS TABLE OF TESTTYPE ;

CREATE OR REPLACE PROCEDURE GETTESTTYPETABLE(lTestTypeTable OUT NOCOPY TestTypeTable)
 IS
  BEGIN
     SELECT TESTTYPE(COLA,COLB)
               bulk collect INTO lTestTypeTable
         FROM (
             SELECT 'ValA' COLA ,123 COLB
               FROM DUAL
               UNION
             SELECT 'ValB' COLA ,234 COLB
               FROM DUAL
               UNION
             SELECT 'Valc' COLA ,456 COLB
               FROM DUAL
         ) ;

END GETTESTTYPETABLe;

then in the .net side of things this will effectively be a value of TESTTYPE()

Now to save you some time you can use the RETURNING clause on INSERT/UPDATE/DELETES as such

create table testTable (colA varchar2(50) , colB number(10) );
CREATE SEQUENCE TESTSEQ START WITH 1 NOCACHE;

DECLARE
lTestTypeTable TestTypeTable ; 
BEGIN
    UPDATE TESTTABLE
      SET
      COLA = '1' ,
      COLB = 'a'
    WHERE COLA IS NULL
     RETURNING TESTTYPE(COLA,COLB)  --NOTE IF YOU HAVE ONE ROW YOU MAY DROP THE BULK COLLECT AND PUT IT INTO THE SINGLE ROW TYPE
     BULK COLLECT INTO
     lTestTypeTable
     ;
END ; 
/



DECLARE
lTestType TestType; 
BEGIN
    INSERT INTO TESTTABLE(COLA, COLB)
    VALUES ('BBB' , testSeq.NEXTVAL )  
     RETURNING TESTTYPE(COLA,COLB)
     INTO
     lTestType
     ;

     DBMS_OUTPUT.PUT_LINE('MY NEW SEQUENCE # IS SET TO ' || lTestType.COLB) ;
END ; 
/

unfortunately it seems that you cannot do a

insert into TT ... SELECT * from .. RETURNING Type(x,y) BULK COLLECT INTO lVariable;

so instead of rote COPING FROM THIS SITE, IT TELLS OF A "work-around" TO GET THE BULK COLLECT TO WORK IN AN INSERT STATEMENT http://www.oracle-developer.net/display.php?id=413

tanging
Thanks very much. It was the PL/SQL side of things I was looking for. I wasn't clear enough in my other question so I'll mark up there also.
haymansfield
np, I added some additional implementation details of getting SQL data into collections from update/inserts/etc (this would have saved me some time!)
tanging