Using an IN Clause in a Stored Procedure

Hello All:
I have come to the conclusion that my stored procedure is having a problem with the use of an IN clause using a parameterized query.
I have in my Stored Procedure the following: AND oh.status_flag IN (p_OrderStatus)
The p_OrderStatus will contain values along the lines of:
1). p_OrderStatus = 'X'
2). p_OrderStatus = 'X', 'S', 'W'
In my .Net code, I am setting the parameter value as being:
oCommand.Parameters.Add("p_OrderStatus", OracleDbType.Varchar2).Value = Status;
However: this isn't working as something in the IN clause doesn't seem to work as I expected, but if I do the same query through a TORA window, it brings back values.
What is causing this to stop working once I move it to a Stored Procedure and how do I fix it (either the .Net side or the Oracle Side, which I assume it would be something on the Oracle side of things).
Any help is greatly appreciated
Thanks
Andy

One way I've found to get around this is to have .net pass in a comma seperated list of values into the procedure, then have the procedure split it up into a table and use that in the IN. There's probably a better way to do it, but this works:
create or replace
TYPE "STRINGTABLETYPE" AS TABLE OF VARCHAR2(30);
create or replace
FUNCTION "FUNC_STR2STRTBL"
( p_str in varchar2 ) return
stringTableType
as
l_str long default p_str || ',';
l_n number;
l_data stringTableType := stringTabletype();
begin
loop
l_n := instr( l_str, ',' );
exit when (nvl(l_n,0) = 0);
l_data.extend;
l_data( l_data.count ) := ltrim(rtrim(substr(l_str,1,l_n-1)));
l_str := substr( l_str, l_n+1 );
end loop;
return l_data;
end;
Then in your actual query procedure, you can do something like this:
SELECT * FROM table WHERE column IN (select * from THE (select cast(FUNC_STR2TBL(parameter) as STRINGTABLETYPE) from dual))

Similar Messages

  • Using SQL Loader script in a Stored Procedure

    Can I use SQL Loader script in a stored procedure and then execute it from a front-end appl.? The reason for this seemingly convoluted solution is that the users don't want a batch load though the records volume is quite high (around 1 mil). Other loads using ODBC connection or OLE DB seem to be inferior to SQL Loader.

    I would suggest a couple of solutions:
    1. Have a cgi script that can upload the file to the server from a web ui, then have the cgi script call the sql*loader file, and it will insert into the database.
    2. You can try to use External tables. This is avaliable in 9i and onwards. You will be able to make any sql DML on the external table.
    I would normally use sql*loader, move the data to a staging table with nologging, and paralle loading. After it has been loaded into the staging table I would process it into my main tables. Have used this approach with up to 60 million records in one load.
    You can do calls to C procedures, Pro*C procedures through PLSQL, as well as java calls, or use Java stored procedures.
    My experience is that SQL*Loader is the fastest way to load data into the database.

  • How to get a list of values used in the WHERE filter of stored procedures and functions in SQL Server

    How can I get a list of values (one or more) used in the WHERE filter of stored procedures and functions in SQL Server?
    How can get a list of values as shown (highlighted) in the sample stored procedure below?
    ALTER PROC [dbo].[sp_LoanInfo_Data_Extract] AS
    SELECT   [LOAN_ACCT].PROD_DT,
                  [LOAN_ACCT].ACCT_NBR, 
                  [LOAN_NOTE2].OFCR_CD, 
                  [LOAN_NOTE1].CURR_PRIN_BAL_AMT, 
                  [LOAN_NOTE2].BR_NBR,
    INTO #Table1
    FROM
                    dbo.[LOAN_NOTE1],
                    dbo.[LOAN_NOTE2],
                    dbo.[LOAN_ACCT]
    WHERE
                    [LOAN_ACCT].PROD_DT = [LOAN_NOTE1].PROD_DT
                    and
                    [LOAN_ACCT].ACCT_NBR = [LOAN_NOTE1].ACCT_NBR
                    and
                    [LOAN_NOTE1].PROD_DT = [LOAN_NOTE2].PROD_DT
                    and
                    [LOAN_NOTE1].MSTR_ACCT_NBR = [LOAN_NOTE2].MSTR_ACCT_NBR
                    and
                    [LOAN_ACCT].PROD_DT = '2015-03-10'
                    and
                    [LOAN_ACCT].ACCT_STAT_CD IN
    ('A','D')
                    and
                    [LOAN_NOTE2].LOAN_STAT_CD IN
    ('J','Z')
    Lenfinkel

    Hi LenFinkel,
    May I know what is purpose of this requirement, as olaf said,you may parse the T-SQL code (or the execution plan), which is not that easy.
    I have noticed that the condition values in your Stored Procedure(SP) are hard coded and among them there is a date values, I believe some day you may have to alter the SP when the date expires. So why not declare 3 parameters of the SP instead hard coding?
    For multiple values paramter you can use a
    table-valued parameter. Then there's no problem getting the values.
    If you could elaborate your purpose, we may help to find better workaround.
    Eric Zhang
    TechNet Community Support

  • Why do I have to use SELECT INTO clause in a Stored Proc ?

    Hi,
    I'm new to oracle. I've bought Oracle Database 10g SQL and Oracle Database 10 A beginner's Guide, two books from Oracle press but I can't find any answer for that.
    Why when I'm writing a Stored Procedure with a SELECT statement do I have to use an INTO clause ?? And why when the select returns more than one row do I have to use a cursor ? By the way is this the only way to return more than one row from a stored procedure ?
    Any help will be appreciated.
    Regards.
    S. Nunes

    Sounds like you may have some experience with other databases (SQL Server, maybe?) which allow you to create stored procedures which implicitly return result sets simply by embedding select statements.
    In Oracle, the correct way to do this is using ref cursors.
    See the following link for a good explanation:
    http://osi.oracle.com/~tkyte/ResultSets/index.html

  • WITH clause in a stored procedure

    Hi, i have the following code for a stored procedure:
    create or replace
    PROCEDURE DISP_ORDER
    ( param1 IN VARCHAR2
    , param2 OUT Types.cursor_type
    ) AS
    BEGIN
    WITH itemall as (select * from rep_order)
    open param2 for select * from itemall;
    END DISP_ORDER;
    this produce an error
    PL/SQL:SQL statement ignored
    Question: Is the WITH clause not allowed in stored procedure?
    Thanks
    PS: cursor_type is defined, the code is only a example!

    WITH comes after the OPEN
    SQL> var c refcursor
    SQL> begin
      2      open :c for
      3          with test_data as
      4              (
      5              select 1 n from dual union all
      6              select 2 n from dual union all
      7              select 3 n from dual
      8              )
      9          select * from test_data;
    10  end;
    11  /
    PL/SQL procedure successfully completed.
    SQL> print c
             N
             1
             2
             3
    SQL>

  • With clause in a stored procedure with update

    create table TEMP_STAGE as
    (select '111111' student_id, 'N' TMP_TEST FROM DUAL
    UNION
    select '111111' student_id, 'N' TMP_TEST FROM DUAL
    UNION
    select '222222' student_id, 'N' TMP_TEST FROM DUAL
    CREATE OR REPLACE PROCEDURE TEMP_SEC_TEST
    AS
    BEGIN
    UPDATE TEMP_STAGE S
    SET S.TMP_TEST = 'Y'
    WHERE STUDENT_ID IN
    WITH MARK AS
    SELECT '111111' STUDENT_ID FROM DUAL
    select STUDENT_ID from MARK
    END;
    I have a huge sql statement with several 'with' tables in the statement...It works if I execute it as a script but it does not work when I put it in a stored procedure.
    When I execute the above it gives me an error...
    Error Syntax Check (8:9)
    Found 'MARK' expecting .....
    It gives me an error at the wth Mark as line....
    I have to do it in the stored procedure as the statement is very complicated...
    Please help.

    What tool are you using to create the procedure? Error Syntax Check (8:9) is not an Oracle error, nor as far as I know from any Oracle product.
    It works just fine in sqlplus.
    SQL> CREATE procedure my_p AS
      2  BEGIN
      3     UPDATE t
      4     SET descr = 'Un'
      5     WHERE id IN (WITH tmp AS (
      6                     SELECT 1 x FROM dual)
      7                  SELECT x FROM tmp);
      8  END;
      9  /
    Procedure created.
    SQL> SELECT * FROM t;
            ID DESCR
             1 One
    SQL> exec my_p;
    PL/SQL procedure successfully completed.
    SQL> SELECT * FROM t;
            ID DESCR
             1 UnJohn

  • How to use Bpel output as input in stored procedure

    I am a SOA beginner. The scenario is to use the output from a BPEL process in a stored procedure as input and validate it against the data in the database. The data received from BPEL is compared with fields from different tables.
    It is basically a business process data validation.
    Will the output of BPEL be a Xml file and how it can b used in query of a procedure?
    Edited by: 869091 on Jun 29, 2011 12:14 AM

    The output from the BPEL process will be in XML format.
    Your requirement is not clear, please state it properly what are you trying to do.
    -Yatan

  • How to use signed classes/Jars in Java Stored Procedure?

    I am using java encryption API in my java application that I want to deploy as java stored procedure. The API is kept in the signed jar files.
    The Application is running in the MS-DOS environment but not in Oracle8i.
    It gives me following error.
    java.lang.ExceptionInInitializerError: java.lang.SecurityException: Cannot set
    up certs for trusted CAs
    at javax.crypto.b.<clinit>([DashoPro-V1.2-120198])
    at javax.crypto.KeyGenerator.getInstance([DashoPro-V1.2-120198])
    at DesKey.GenerateKey(DesKey.java:63)
    declare
    ERROR at line 1:
    ORA-29532: Java call terminated by uncaught Java exception:
    java.lang.ExceptionInInitializerError
    ORA-06512: at line 4
    (Note: I have enabled the java output in SQL Plus editor otherwise it will give only the second part of error that starts from ERROR at line 1:)
    please guide me how to solve this problem.
    Salman Hameed

    Salman,
    If you do not get a reply on this forum, I recommend you post this question on the Oracle JVM discussion forum as well.
    In addition, I would recommend checking the documentation for Oracle8i. The Oracle8i Java Developer's Guide, the Java Stored Procedures Guide, and the JDBC Developer's guide may have some information on this topic. You can get to this doc from the OTN Documentation page. Click on Oracle8i, then General Documentation, Release 2 (8.1.6), then scroll down to see the link for the Oracle8i Java Developer's documetation. All of the books mentioned above are available from that link.

  • How to use lexical parameters with Sql Server Stored Procedure?

    Hi,
    I'm developing a BI Publisher report on a sql server database. I need to execute a stored procedure to dynamically build the query by replacing the lexical parameters with the values of varaibles of the stored procedure. With Oracle stored procedures, I have used data template and had reference the varaiable in SP by prefixing it with '&'.
    It doesn't work if I try to do the same thing with SQL server. Is there anyone who has come across the similar situation? Please let me know if anyone has got any ideas...
    Thanks in Advance
    Rag

    TopLink currently doesn't support multiple ResultSets. Multiple ResultSets support is considered for a future release.

  • Invoking "java myClass" using Runtime.Exec from a Java Stored Procedure

    Hi All,
    This is regarding the use of Runtime.getRunTime().exec, from a java programme (a Java Stored Procedure), to invoke another Java Class in command prompt.
    I have read many threads here where people have been successuful in invoking OS calls, like any .exe file or batch file etc, from withing Java using the Runtime object.
    Even i have tried a sample java programme from where i can invoke notepad.exe.
    But i want to invoke another command prompt and run a java class, basically in this format:
    {"cmd.exe","java myClass"}.
    When i run my java programme (in command prompt), it doesnt invoke another command prompt...it just stays hanging.
    When i run the java programme from my IDE, VisualCafe, it does open up a command prompt, but doesnt get the second command "java myCLass".
    Infact on the title of the command prompt (the blue frame), shows the path of the java.exe of the Visual Cafe.
    and anyway, it doesnt run my java class, that i have specified inside the programme.
    Even if i try to run a JAR file, it still doesnt do anything.
    (the JAR file other wise runs fine when i manually invoke it from the command prompt).
    Well, my question is, actually i want to do this from a Java Stored Procedure inside oracle 8.1.7.
    My feeling is, since the Java Stored Procedure wont be running from the command prompt (i will be actually invoking it through a Oracle trigger), it may be able to invoke the command prompt and run the java class i want. and that java class has to run with the SUn's Java, not Oracle JAva.
    Does any one have any idea about it?
    Has anyone ever invoked a java class or JAR file in command prompt from another Java Programme?
    YOur help will be highly appreciated.
    (P:S- Right now, my database is being upgraded, so i havent actually been able to create a Java Stored procedure and test it. But i have tested from a normal java programme running in command prompt and also from Visual Cafe).
    Thanks in advance.
    -- Subhasree.

    Hello Hari,
    Thanks for your quick reply.
    Can you please elaborate a little more on exactly how you did? may be just copy an dpaste taht part of teh code here?
    Thanks a lot in advance.
    --Subhasree                                                                                                                                                                                                                                                                                                                                                                                                           

  • Using VARRAYs as parameters to a Stored Procedure

    I'm trying to pass a VARRAY as an IN/OUT parameter into a simple stored procedure by doing the following ..
    call.addNamedInOutputArgument(.., .., .., oracle.sql.ARRAY.class);
    I'm using a DataReadQuery. I set the call in the query, bind all parameters and add the IN/OUT argument to the query (i.e. query.addArgument(<name>)). Then I create an oracle.sql.ArrayDescriptor to describe the ARRAY.
    ArrayDescriptor ad = ArrayDescriptor.createDescriptor(<name of VARRAY>, <connection>);
    The VARRAY is of size 1 whose type is INTEGER. I create an ARRAY of Integer objects.
    Integer fields[1] = new Integer[1];
    fields[0] = new Integer(2);
    ARRAY a = new ARRAY(ad, <connection>, fields);
    I create a Vector of parameters and add the ARRAY to it. Then I execute the query ..
    Object result = session.executeQuery(<query>, <Vector>);
    When I execute the query I keep getting an ORA-06550: wrong number or types of arguments in call to <stored procedure>. Any help would be appreciated. Thanks. -Michael-

    The workaround above using JDBC directly to call store procedures using VARRAY types is still probably your best solution.
    In TopLink 10.1.3 there is a new API on StoredProcedureCall that allows passing the JDBC type code which should allow binding of VARRAY output parameters.
    addNamedInOutputArgument(String procedureParameterName, String inArgumentFieldName, String outArgumentFieldName, int type, String typeName) {                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   

  • Using comma seperated String in a Stored Procedure, in a "IN" keyword

    Hello,
    I am stuck trying to figure out the best way to pass an array of Ids into a stored procedure, and evaluate then in a SELECT WHERE statement. e.g. I have:
    possibleIds VARCHAR2(50);
    BEGIN
    SELECT blah FROM foobar
    WHERE foobar.id IN (possibleIds);
    Where possibleIds looks like "10,11,12,30,101"...
    Is this possible somehow? The above fails because possibleIds is evaluated as a VARCHAR2, and is not "concatenated" to the SQL statement.
    Thanks for the help!

    The split function Scott used is some variant on one of the solutions posted in the Ask Tom link I posted. More specifically, it is probably a variant on either one of the pipelined function solutions, or one based on splitting the string into a SQL table of objects.
    Look at Tom's original response to the question for a working solution. If you have a reasonably new version of Oracle, then you may also want to look at the modificataion/modernization later in the thread which can be found here
    HTH
    John

  • HowTo: Using special characters in SQL passthrough stored procedure parameters?

    Hi all,
    I am creating a Visual FoxPro 5.0a application, where I am trying to call a stored procedure in my Oracle database with the following SQLEXEC command (SQLEXEC is the Foxpro function to send sql statements to the ODBC driver):
    lcSQL = "{Call Insert_FRB.NewItemPage('KABO)n$i,30000000000', '184927', '184927', 'MAIR2001011216151314', 'MAIR')}"
    /* Sorry the above command should of course not break accross pages*/
    lnSuccess = sqlexec(1,lcSQL)
    With the parameters given, I get the ODBC error: "Connectivity error: [Oracle][ODBC Oracle Driver]Syntax Error."
    I get the same error, when using the Oracle ODBC 32bit Test utility.
    The call works just fine, if I replace the "$" in the first parameter 'KABO)n$i,30000000000' with a "normal" character, e.g. 'KABO)nAi,30000000000'. Oracle handles the $-character just fine, the stored procedure is working properly when called directly in SQLPLUS8.
    I am using the Oracle ODBC driver sqo32_73.dll Version2.00.03.01.
    Questions:
    How do I have to submit the "$" to the ODBC driver, in order to be passed through to Oracle unchallenged?
    Does anybody know of other special characters, which are not accepted by the ODBC-drivers for Oracle?
    Thanks to any hints,
    Peter

    You could try downloading the Oracle 8.1.7 client and the latest
    Oracle8 ODBC driver, install them on your machine, and verify
    that the failure goes away. That's obviously the acid test.
    I can tell you that when I worked in the ODBC driver group we
    did identify and fix some bugs where our parser wasn't skipping
    string literals. If this particular bug wasn't fixed earlier,
    it almost certainly was then (I'm guessing that work was done 12-
    18 months ago).
    Justin

  • Use of Java packages in Java stored procedures? (Oracle 10.2)

    Hi all-
    Does Oracle's Java stored procedures support Java packages? In all the examples I've seen, packages are never used, and when I added one to a test file, it compiled it just fine, but referencing the class from the package itself resulted in a compiling error.
    In other words, it seems to make absolutely no difference if I preface my Java class with
    package com.mycompany.whatever;
    and while I can "import com.mycompany.whatever.MyClass" in another Java file, it seems to not care at all whether I do so or not.
    Thanks for any clarification,
    Ilford

    Hi Ilford:
    Sure you can use fully package notation in your Java classes.
    Look at this Java source code:
    http://dbprism.cvs.sourceforge.net/dbprism/cms-2.1/src/com/prism/cms/core/
    All of them are Java Stored procedures.
    Best regards, Marcelo.

  • How can I get list of columns used of specific table in all stored procedure?

    How can I get used column list of a specific table in among all stored procedure?
    Suppose that,
    I have a table(VendorMaster) which has 100 columns just I want to know how many columns used in among all stored procedure.

    We have solved by below query...
    IF OBJECT_ID('tempdb.dbo.#SPDependencyDetails') IS NOT NULL
    DROP TABLE #SPDependencyDetails
    CREATE TABLE #SPDependencyDetails
     Or_Object_Database NVARCHAR(128)
    ,Or_Object_Name NVARCHAR(128)
    ,Ref_Database_Name NVARCHAR(128)
    ,Ref_Schema_Name NVARCHAR(128)
    ,Ref_Object_Name NVARCHAR(128)
    ,Ref_Column_Name NVARCHAR(128)
    ,Is_Selected BIT
    ,Is_Updated BIT
    ,Is_Select_All BIT
    ,Is_All_Columns_Found BIT
    DECLARE @database_name VARCHAR(100)
    DECLARE database_cursor CURSOR
    FOR
    SELECT name
        FROM sys.databases
        WHERE database_id =8
    OPEN database_cursor
    FETCH NEXT FROM database_cursor
    INTO @database_name
    WHILE @@FETCH_STATUS = 0 --Outer Loop begin
    BEGIN
        DECLARE  @WholeLotofSQL NVARCHAR(MAX) =       '
        DECLARE @object_name VARCHAR(150)
        ,@sqlstatement NVARCHAR(2500)
        DECLARE object_cursor CURSOR --Inner cursor, iterates list of objects that match type
        FOR
            SELECT name
                FROM '+@database_name+'.sys.objects AS o
                WHERE o.type = ''P'' --Change Object type to find dependencies of Functions, Views and etc.
                ORDER BY 1    
        OPEN object_cursor
        FETCH NEXT FROM object_cursor INTO @object_name
        WHILE @@FETCH_STATUS = 0  --Inner Loop Begin
            BEGIN
                SET @sqlstatement = ''USE '+@database_name+';
                                    INSERT INTO #SPDependencyDetails
                                    SELECT DB_NAME() AS Or_Object_Database
                                            ,'''''' + @object_name + '''''' AS Or_Object_Name
                                            ,CASE WHEN referenced_database_name IS NULL THEN DB_NAME()
                                                    ELSE referenced_database_name
                                            END AS Ref_Database_Name
                                            ,referenced_schema_name AS Ref_Schema_Name
                                            ,referenced_entity_name AS Ref_Object_Name
                                            ,referenced_minor_name AS Ref_Column_Name
                                            ,is_selected
                                            ,is_updated
                                            ,is_select_all
                                            ,is_all_columns_found
                                        FROM sys.dm_sql_referenced_entities(''''dbo.'' + @object_name + '''''', ''''OBJECT'''');''
                EXEC sys.sp_executesql @sqlstatement
                FETCH NEXT FROM object_cursor INTO @object_name
            END      
        CLOSE object_cursor
        DEALLOCATE object_cursor'
        EXEC sys.sp_executesql @WholeLotofSQL
        FETCH NEXT FROM database_cursor INTO @database_name
    END
    CLOSE database_cursor;
    DEALLOCATE database_cursor;
    SELECT Or_Object_Database as 'Database'
    ,Or_Object_Name as 'Procedure'
    ,Ref_Object_Name as 'Table'
    ,Ref_Column_Name as 'Column'
    FROM #SPDependencyDetails

Maybe you are looking for