Using a record as parameter in dynamic SQL

Hi,
I'm trying to pass a record (%rowtype) as parameter
to a procedure which i call from inside a dynamic statement. This doesnt seem to work however, I get the reply "not a valid sql-statement".
Apparently, Oracle does not allow for this since records are not a SQL-type but a PL/SQL-type.
Have anyone found a way around this?
(Except for the obvious to pass just the primary key and
populate the record inside the procedure).
The code looks like:
declare
v_rec employee%rowtype;
v_stmt varchar2(500);
begin     
select *
          into v_rec
          from emplyee
          where rownum = 1;
v_stmt := 'some_proc ( :param )';
execute immediate v_stmt using v_rec;
end;
Any advice appreciated,
Thanks
Anders Grusell

Hi,
Here's a example of something similar which we had done for the data conversion excercise recently.
The script uses dynmanic PL/SQL block and executes it using Execute Immediate. AAnd within the dynamic PL/SQL block, a procedure Pr_Pb_Test is called which takes in %ROWTYPE as ainput parameter.
See if this helps.
-- Shailender Mehta --
Set ServerOutput On Size 999999;
Declare
i Number;
lValue Varchar2(10);
type rc is ref cursor;
iVar1 Varchar2(10) := 'Test Var';
l_cursor rc;
ltest1 Mr_Test1%RowType; -- RowType Structure for Mirror
lSql Varchar2(100);
lNewTab Tb_Test%RowType; -- RowType Structure for the New System Table
/* This procedure uploads to new system table the 1st time
The table Test1 after successful upload will be taken Offline
or deleted
Procedure abc Is
lSqlStr Long;
Begin
SQL> desc test1;
Name Null? Type
ID NUMBER
COL1 VARCHAR2(10)
lSqlStr := 'Declare
ltest Test1%RowType;
type rc is ref cursor;
l_cursor rc;
lNewTab Tb_Test%RowType; -- RowType Structure for new system Table
Begin
Open l_cursor For :lSql;
Loop
Fetch l_cursor Into ltest;
Exit When l_cursor%NotFound;
lNewTab.Id := ltest.Id;
lNewTab.Col1 := ltest.Col1;
-- Procedure which inserts into the new system
-- table using %ROWTYPE
Pr_Tb_Test (lNewTab);
End Loop;
End;
Execute Immediate lSqlStr USING lsql;
End abc;
Begin
Begin
Select code_value
Into lValue
From DC_CODES
Where code_type_cd = 'LASTRUNDT'
And code_value Is Not Null;
i := 1;
Exception
When No_Data_Found Then i := 0;
End;
If ( i = 0 ) Then
Delete From Tb_Test;
lSql := 'Select * from test1';
abc;
Else
-- Subsequent uploads to the new system tables using MIRROR table : Mr_Test1
SQL> desc mr_test1;
Name Null? Type
ID NUMBER
COL1 VARCHAR2(10)
ACTION_TYPE VARCHAR2(1)
lSql := 'Select * from mr_test1 Where Action_Type = ''I''';
Open l_cursor for lSql;
Loop
Fetch l_cursor Into ltest1;
Exit When l_cursor%NotFound;
lNewTab.Id := ltest1.Id;
lNewTab.Col1 := ltest1.Col1;
Pr_Tb_Test (lNewTab); -- Procedure which inserts into the new system
End Loop;
Close l_cursor;
End If;
Update dc_codes set code_value = sysdate;
End;
/

Similar Messages

  • Using a comma-delimited string in Dynamic SQL

    Hi --
    If I receive a comma-delimited string as an in parameter, can I simply use that (in string format) when building my dynamic sql?
    Thanks,
    Christine

    The problem is, that you can not use bind variables
    here, only literals. This causes
    eventual performance problems.And to avoid the inevitable database performance problems Dmytro mentions you can use a function to convert the string to a varray and select from that. This also avoids having to use dynamic sql.
    First you create a varray and conversion function.
    SQL> create or replace type tabstr_t as table of varchar2(255)
      2  /
    Type created.
    SQL> create or replace function tabstr (
      2      p_str in varchar2,
      3      p_sep in varchar2 default ','
      4      )
      5  return tabstr_t
      6  as
      7      l_str long default p_str || p_sep;
      8      l_tabstr tabstr_t := tabstr_t();
      9  begin
    10      while l_str is not null loop
    11          l_tabstr.extend(1);
    12          l_tabstr(l_tabstr.count) := rtrim(substr(
    13                  l_str,1,instr(l_str,p_sep)),p_sep);
    14          l_str := substr(l_str,instr(l_str,p_sep)+1);
    15      end loop;
    16      return l_tabstr;
    17  end;
    18  /
    Function created.Then you can use these in either regular sql.
    SQL> var s varchar2(100)
    SQL> exec :s := 'Smith,Scott,Miller'
    PL/SQL procedure successfully completed.
    SQL>
    SQL> select * from emp where ename in
      2      (select upper(column_value) from table(tabstr(:s)));
    EMPNO ENAME    JOB          MGR HIREDATE     SAL   COMM  DEPTNO
      7369 SMITH    CLERK       7902 17-DEC-80    800             20
      7788 SCOTT    ANALYST     7566 09-DEC-82   3000             20
      7934 MILLER   CLERK       7782 23-JAN-82   1300             10Or in pl/sql.
    SQL> var c refcursor
    SQL> begin
      2      open :c for
      3      select * from emp where ename in
      4          (select upper(column_value) from table(tabstr(:s)));
      5  end;
      6  /
    PL/SQL procedure successfully completed.
    SQL> print c
    EMPNO ENAME    JOB          MGR HIREDATE     SAL   COMM  DEPTNO
      7369 SMITH    CLERK       7902 17-DEC-80    800             20
      7788 SCOTT    ANALYST     7566 09-DEC-82   3000             20
      7934 MILLER   CLERK       7782 23-JAN-82   1300             10

  • How to use the column names generated from Dynamic SQL

    Hi,
    I have a problem with Dynamic SQL.
    I have written an SQL which will dynamically generate the Select statement with from and where clause in it.
    But that select statement when executed will get me hundreds of rows and i want to insert each row separately into one more table.
    For that i have used a ref cursor to open and insert the table.
    In the select list the column names will also be as follows: COLUMN1, COLUMN2, COLUMN3,....COLUMNn
    Please find below the sample code:
    TYPE ref_csr IS REF CURSOR;
    insert_csr ref_csr;
    v_select VARCHAR2 (4000) := NULL;
    v_table VARCHAR2 (4000) := NULL;
    v_where VARCHAR2 (4000) := NULL;
    v_ins_tab VARCHAR2 (4000) := NULL;
    v_insert VARCHAR2 (4000) := NULL;
    v_ins_query VARCHAR2 (4000) := NULL;
    OPEN insert_csr FOR CASE
    WHEN v_where IS NOT NULL
    THEN 'SELECT '
    || v_select
    || ' FROM '
    || v_table
    || v_where
    || ';'
    ELSE 'SELECT ' || v_select || ' FROM ' || v_table || ';'
    END;
    LOOP
    v_ins_query :=
    'INSERT INTO '
    || v_ins_tab
    || '('
    || v_insert
    || ') VALUES ('
    || How to fetch the column names here
    || ');';
    EXECUTE IMMEDIATE v_ins_query;
    END LOOP;
    Please help me out with the above problem.
    Edited by: kumar0828 on Feb 7, 2013 10:40 PM
    Edited by: kumar0828 on Feb 7, 2013 10:42 PM

    >
    I Built the statement as required but i need the column list because the first column value of each row should be inserted into one more table.
    So i was asking how to fetch the column list in a ref cursor so that value can be inserted in one more table.
    >
    Then add a RETURNING INTO clause to the query to have Oracle return the first column values into a collection.
    See the PL/SQL Language doc
    http://docs.oracle.com/cd/B28359_01/appdev.111/b28370/returninginto_clause.htm#sthref2307

  • Using bind variables (in & out) with dynamic sql

    I got a table that holds pl/sql code snippets to do validations on a set of data. what the code basically does is receiving a ID and returning a number of errors found.
    To execute the code I use dynamic sql with two bind variables.
    When the codes consists of a simpel query, it works like a charm, for example with this code:
    BEGIN
       SELECT COUNT (1)
       INTO :1
       FROM articles atl
       WHERE ATL.CSE_ID = :2 AND cgp_id IS NULL;
    END;however when I get to some more complex validations that need to do calculations or execute multiple queries, I'm running into trouble.
    I've boiled the problem down into this:
    DECLARE
       counter   NUMBER;
       my_id     NUMBER := 61;
    BEGIN
       EXECUTE IMMEDIATE ('
          declare
             some_var number;
          begin
          select 1 into some_var from dual
          where :2 = 61;
          :1 := :2;
          end;
          USING OUT counter, IN my_id;
       DBMS_OUTPUT.put_line (counter || '-' || my_id);
    END;this code doesn't really make any sense, but it's just to show you what the problem is. When I execute this code, I get the error
    ORA-6537 OUT bind variable bound to an IN position
    The error doesn't seem to make sense, :2 is the only IN bind variable, and it's only used in a where clause.
    As soon as I remove that where clause , the code will work again (giving me 61-61, in case you liked to know).
    Any idea whats going wrong? Am I just using the bind variables in a way you're not supposed to use them?
    I'm using Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - 64bit

    Correction. With execute immediate binding is by position, but binds do not need to be repeated. So my statement above is incorrect..
    You need to bind it once only - but bind by position. And the bind must match how the bind variable is used.
    If the bind variable never assigns a value in the code, bind as IN.
    If the bind variable assigns a value in the code, bind as OUT.
    If the bind variable assigns a value and is used a variable in any other statement in the code, bind as IN OUT.
    E.g.
    SQL> create or replace procedure FooProc is
      2          cnt     number;
      3          id      number := 61;
      4  begin
      5          execute immediate
      6  'declare
      7          n       number;
      8  begin
      9          select
    10                  1 into n
    11          from dual
    12          where :var1 = 61;       --// var1 is used as IN
    13 
    14          :var2 := n * :var1;     --// var2 is used as OUT and var1 as IN
    15          :var2 := -1 * :var2;    --// var2 is used as OUT and IN
    16  end;
    17  '
    18          using
    19                  in out id, in out cnt;  --// must reflect usage above
    20 
    21          DBMS_OUTPUT.put_line ( 'cnt='||cnt || ' id=' || id);
    22  end;
    23  /
    Procedure created.
    SQL>
    SQL> exec FooProc
    cnt=-61 id=61
    PL/SQL procedure successfully completed.
    SQL>

  • Report using ref cursor or dynamic Sql

    Hi,
    I never create a report using a ref cursor or a dynamic sql. Could any one help me to solve the below issue.
    I have 2 tables.
    1. Student_Record
    2. Student_csv_help
    Student_Record the main table where the data is stored.
    Student_csv_help will contain the all the column names of the Student_record.
    CREATE TABLE Student_CSV_HELP
    ENTRY_ID NUMBER,
    RAW_NAME VARCHAR2(40 BYTE),
    DESC_NAME VARCHAR2(1000 BYTE),
    IN_OUTPUT_LIST VARCHAR2(1 BYTE)
    SET DEFINE OFF;
    Insert into TOA_CSV_HELP
    (ENTRY_ID, RAW_NAME, DESC_NAME, IN_OUTPUT_LIST)
    Values
    (1, 'S_ID', 'Student ID', 'Y');
    Insert into TOA_CSV_HELP
    (ENTRY_ID, RAW_NAME, DESC_NAME, IN_OUTPUT_LIST)
    Values
    (2, 'S_Name', 'Student Name', 'Y');
    Insert into TOA_CSV_HELP
    (ENTRY_ID, RAW_NAME, DESC_NAME, IN_OUTPUT_LIST)
    Values
    (3, 'S_Join_date', 'Joining Date', 'Y');
    Insert into TOA_CSV_HELP
    (ENTRY_ID, RAW_NAME, DESC_NAME, IN_OUTPUT_LIST)
    Values
    (4, 'S_Address', 'Address', 'Y');
    Insert into TOA_CSV_HELP
    (ENTRY_ID, RAW_NAME, DESC_NAME, IN_OUTPUT_LIST)
    Values
    (5, 'S_Fee', 'Tution Fee', 'N');
    commit;
    CREATE TABLE Student_record
    S_ID NUMBER,
    S_Name VARCHAR2(100 BYTE),
    S_Join_date date,
    S_Address VARCHAR2(360 BYTE),
    S_Fee Number
    Insert into Student_record
    (S_ID, S_Name, S_Join_date, S_Address,S_Fee)
    Values
    (101, 'john', TO_DATE('12/17/2009 08:00:00', 'MM/DD/YYYY HH24:MI:SS'), 'CA-94777', 2000);
    Insert into Student_record
    (S_ID, S_Name, S_Join_date, S_Address,S_Fee)
    Values
    (102, 'arif', TO_DATE('12/18/2009 08:00:00', 'MM/DD/YYYY HH24:MI:SS'), 'CA-94444', 3000);
    Insert into Student_record
    (S_ID, S_Name, S_Join_date, S_Address,S_Fee)
    Values
    (103, 'raj', TO_DATE('12/19/2009 08:00:00', 'MM/DD/YYYY HH24:MI:SS'), 'CA-94555', 2500);
    Insert into Student_record
    (S_ID, S_Name, S_Join_date, S_Address,S_Fee)
    Values
    (104, 'singh', TO_DATE('12/20/2009 08:00:00', 'MM/DD/YYYY HH24:MI:SS'), 'CA-94666', 2000);
    Commit;
    Now my requirement is:
    I have a form with Student_record data block. When i Click on print Button on this form. It will open another window which has Student_CSV_HELP.DESC_NAME and a check box before this.
    The window look like as below:
    check_box       DESC_NAME+
    X                   S_ID+
    --                   S_Name+
    X                   S_Join_date+
    X                   S_Address+
    --                  S_Fee+
    X  means check box checked.+
    --  means check box Unchecked.+
    After i selected these check boxes i will send 2 parameters to the report server
    1. a string parameter to the report server which has the value 'S_ID,S_Join_date,S_Address' (p_column_name := 'S_ID,S_Join_date,S_Address');
    2. the s_id value from the student_record block (p_S_id := '101');
    Now my requirement is when i click on run. I need a report like as below:
    Student ID : 101+
    Joining Date : 12/17/2009 08:00:00+
    Address : CA-94777+
    This is nothing but the ref cursor should run like as below:
    Select S_id from student_record block S_id = :p_S_id;
    Select S_Join_date from student_record block S_id = :p_S_id;
    Select S_Address from student_record block S_id = :p_S_id;
    So, according to my understanding i have to select the columns at the run time. I dont have much knowledge in creating reports using ref cursor or dynamic sql.
    So please help me to solve this issue.
    Thanks in advance.

    Plain sql should satisfy your need. Try ....
    Select S_id, S_Join_date, S_Address
    from student_record
    where S_id = :p_S_id

  • Try to use dynamic sql connection for additioal data for xml publisher

    I understand that the xml publisher is using template to merger the xml data output (say from Oracle report).
    We have some Oracle seeded report and do not wish to modify these report, but we do need to add additional data to these reports. In the past, the tool we used has the capability to make dynamic sql calls to get these additional data. Can this be achieved in xml publisher?
    I did noticed that the template builder has two options for upload data source: from a data file or from a sql connection, but seems they can not be used together. Not much document I can find for this. Thank you in advance for your help.
    Frank

    Hi All,
    Sandeep: if u have the solution pls send to my email id [email protected]
    ill really appriciate u r response.
    I have the same requirement to develop AR Invoice Report in XML publisher, actually it was developed using Optio Layout.my issue is the actual AR Invoice RDF contains 20 records and it showing the 20 recodes when we run that program (concurrent program output) after that while printing this report uisng the OPTIO layout (The Optio system is fetching some more records (10 records)) it printing total 30 records. i mean here the OPTIO is handling some pl/sql code i guess..
    so now how we can handle this requirement using XMLP.I am using RTF Templates with 5.6.2 v. Is it possible to do the same manner how the OPTIO was doing. Is this possible with any other Templates ?. or we need to customize the RDF itself ? .but I know that I can go ahead and modify the existing out-of-the-box report, but I do not want to do that. I do not have control over the XML that is generated since the XML is generated when the report is run. Is there any way to retrieve the extra information AFTER the xml has been generated and BEFORE the template is applied ?
    Thanks,
    Rad

  • Using database link  with a dynamic SQL in Forms 5

    I have a Form 5 application where a database link is specified in a client version of dynamic SQL. The function that the application is designed to perform is to access data in a table in a remote database and use it to populate another table in a local database with the same structure as the source table. Dynamic SQL is used because the name of the table can only be resolved during run time.
    The problem is that it gives error with code 'ORA-03113' and text 'End-of-file on communication channel' when you try to run it.
    Is it not possible to use a database link with a dynamic SQL? What can I do to overcome the problem ?

    Try to create a local view based ao the remote table and use the view.

  • PLS-00455: cursor 'CUR_1' cannot be used in dynamic SQL OPEN statement

    create or replace function f_my_test_func
    return refcur_pkg.refcur_t1
    is
    cur_1  refcur_pkg.refcur_t1;
    begin
    open cur_1
    for
    'select * from dept';
    return cur_1;
    exception
    when others
    then
    insert into ddl_log (SQLTEXT)
    values
    ('fucntion error'); 
    end;

    I would suggest that cur_1 refcur_pkg.refcur_t1 is a stongly typed ref cursor i.e. it has RETURN dept%ROWTYPE or something similar in the declaration. You can't use strongly typed ref cursors with dynamic SQL in this way. The declaration should be weakly typed or just use sys_refcursor.
    DTYLER_APP@pssdev2> DECLARE
      2
      3      TYPE t_Strong   IS REF CURSOR RETURN dual%ROWTYPE;
      4
      5      lc_Strong       t_Strong;
      6
      7  BEGIN
      8
      9      OPEN lc_Strong FOR
    10      'SELECT * FROM dual';
    11
    12  END;
    13  /
        OPEN lc_Strong FOR
    ERROR at line 9:
    ORA-06550: line 9, column 10:
    PLS-00455: cursor 'LC_STRONG' cannot be used in dynamic SQL OPEN statement
    ORA-06550: line 9, column 5:
    PL/SQL: Statement ignored
    DTYLER_APP@pssdev2>
    DTYLER_APP@pssdev2> DECLARE
      2
      3      TYPE t_Weak   IS REF CURSOR;
      4
      5      lc_Weak       t_Weak;
      6
      7  BEGIN
      8
      9      OPEN lc_Weak FOR
    10      'SELECT * FROM dual';
    11
    12  END;
    13  /
    PL/SQL procedure successfully completed.
    DTYLER_APP@pssdev2> DECLARE
      2
      3      lc_Weak       sys_refcursor;
      4
      5  BEGIN
      6
      7      OPEN lc_Weak FOR
      8      'SELECT * FROM dual';
      9
    10  END;
    11  /
    PL/SQL procedure successfully completed.
    DTYLER_APP@pssdev2>HTH
    David

  • Should I use dynamic SQL for simple updates?

    Please tell me, out of the two options given below, which option should i use to update columns in a table and why?
    what will be the performance difference between the two approaches?
    Please note: The options given below is just for an example
    procedure proc1(var1 varchar2)
    is
    begin
    update tab1 set col1 = var1;
    commit;
    end;
    procedure proc1(var1 varchar2)
    is
    sqlstr varchar2(1000);
    begin
    sqlstr := 'update tab1 set col1 = :v1';
    execute immediate sqlstr using var1;
    commit;
    end;
    Thanks
    Arun

    Arun G Nath wrote:.
    Should I use dynamic SQL for simple updates? No way, not a chance.
    Performance is not the issue (as long as you are using bind variables).
    But with dynamic SQL, you loose compile time checking; you do not know until runtime if the SQL is valid.
    You also loose the dependency between proc1 and tab1, which can be found only if you search user_source.
    (And in either case, you probably want to remove the commit)
    Regards
    Peter

  • Security risk of ref cursor & dynamic sql

    Hi guys,
    I am using Ref Cursor to execute a dynamic sql in my stored procedure. Inputs to the dynamic SQL are passed in as parameters. My code fragments:
    parameters:
    CREATE PROCEDURE mm_select(
    aaa     IN VARCHAR2,
    bbb     IN VARCHAR2 )
    in the code:
    sqlstmt := 'SELECT * FROM dbaivoc.mm ' ||
    'WHERE b in (' || role_id_list || ') ' ||
    'AND c = '|| application_fn_id_list ;
    OPEN RC1 FOR sqlstmt;
    My question is, is it possible for anyone to pass in a malicious SQL like drop table xxx into the parameter list eg. exec mm_select(1,'1; drop table aa;')
    I've tried but it doesn't seem to work. I'm just asking for confirmation. Thanks for any help!
    rgds,
    mas

    I don't see in your small pieces of code what the input parameters aaa and bbb are used for.
    If your statement is always
    sqlstmt := 'select ....'
    then I don't see how this can be changed to 'drop table ...'.
    It would be something different if you have something like this in your procedure:
    execute immediate bbb;

  • Dynamic SQL - Inner Join

    I just starting to Learn and use Dynamic SQL. 
    I am trying use a inner join query in dynamic SQL 
    for Example say 'Select A.id as Itemid , A.name as Itemname from TableA A inner join TableB B On A.ID = B.ID where B.ID typeid = 1 '
    for some reason this is not working . i for sure know the query works as when i run the query it works fine. when i try to run it dynamically it give me blank values.
    Please let me know if you need more information.
    Appreicate Your help!!
    Thanks,
    Chaitanya 

    Here is an example for dynamic SQL from BOL:
    DECLARE @IntVariable int;
    DECLARE @SQLString nvarchar(500);
    DECLARE @ParmDefinition nvarchar(500);
    /* Build the SQL string one time.*/
    SET @SQLString =
    N'SELECT BusinessEntityID, NationalIDNumber, JobTitle, LoginID
    FROM AdventureWorks2012.HumanResources.Employee
    WHERE BusinessEntityID = @BusinessEntityID';
    SET @ParmDefinition = N'@BusinessEntityID tinyint';
    /* Execute the string with the first parameter value. */
    SET @IntVariable = 197;
    EXECUTE sp_executesql @SQLString, @ParmDefinition,
    @BusinessEntityID = @IntVariable;
    /* Execute the same string with the second parameter value. */
    SET @IntVariable = 109;
    EXECUTE sp_executesql @SQLString, @ParmDefinition,
    @BusinessEntityID = @IntVariable;
    Basically any query can be run as dynamic SQL.  However, static SQL is the first choice. Use dynamic SQL if needed.
    Dynamic SQL examples:
    http://www.sqlusa.com/bestpractices/dynamicsql/
    Kalman Toth Database & OLAP Architect
    SQL Server 2014 Design & Programming
    New Book / Kindle: Exam 70-461 Bootcamp: Querying Microsoft SQL Server 2012

  • Dynamic sql and ref cursors URGENT!!

    Hi,
    I'm using a long to build a dynamic sql statement. This is limited by about 32k. This is too short for my statement.
    The query results in a ref cursor.
    Does anyone have an idea to create larger statement or to couple ref cursors, so I can execute the statement a couple of times and as an result I still have one ref cursor.
    Example:
    /* Determine if project is main project, then select all subprojects */
    for i in isMainProject loop
    if i.belongstoprojectno is null then
    for i in ProjectSubNumbers loop
    if ProjectSubNumbers%rowcount=1 then
    SqlStatement := InitialStatement || i.projectno;
    else
    SqlStatement := SqlStatement || PartialStatement || i.projectno;
    end if;
    end loop;
    else
    for i in ProjectNumber loop
    if ProjectNumber%rowcount=1 then
    SqlStatement := InitialStatement || i.projectno;
    else
    SqlStatement := SqlStatement || PartialStatement || i.projectno;
    end if;
    end loop;
    end if;
    end loop;
    /* Open ref cursor */
    open sql_output for SqlStatement;
    Thanks in advance,
    Jeroen Muis
    KCI Datasystems BV
    mailto:[email protected]

    Example for 'dynamic' ref cursor - dynamic WHERE
    (note that Reports need 'static' ref cursor type
    for building Report Layout):
    1. Stored package
    CREATE OR REPLACE PACKAGE report_dynamic IS
    TYPE type_ref_cur_sta IS REF CURSOR RETURN dept%ROWTYPE; -- for Report Layout only
    TYPE type_ref_cur_dyn IS REF CURSOR;
    FUNCTION func_dyn (p_where VARCHAR2) RETURN type_ref_cur_dyn;
    END;
    CREATE OR REPLACE PACKAGE BODY report_dynamic IS
    FUNCTION func_dyn (p_where VARCHAR2) RETURN type_ref_cur_dyn IS
    ref_cur_dyn type_ref_cur_dyn;
    BEGIN
    OPEN ref_cur_dyn FOR
    'SELECT * FROM dept WHERE ' | | NVL (p_where, '1 = 1');
    RETURN ref_cur_dyn;
    END;
    END;
    2. Query PL/SQL in Reports
    function QR_1RefCurQuery return report_dynamic.type_ref_cur_sta is
    begin
    return report_dynamic.func_dyn (:p_where);
    end;
    Regards
    Zlatko Sirotic
    null

  • Problem returning variables from dynamic SQL/PLSQL

    Hi, I have a problem I am trying to solve using a very short piece of dynamic SQL or PLSQL but I am having problems getting the variable values out of the dynamic block.
    I have 16 counters whose names are made up of three variable parts - 'scheme', 'contributory category' and 'employment category'
    The 'scheme' can be either 'no1', 'no2', 'off', 'cg' or 'amc'
    The 'contributory category' can be either 'cont' or 'noncont'
    The 'employment category' can be either 'ft' or 'pt'
    (There are only 16 because only 16 combinations are possible)
    For example the total counter name could be 'v_cg_noncont_ft_count'
    I have created a variable by concatenating the various elements called v_incr_count_name which holds the name of the counter I want to increment.
    I am running this whole thing within an anonymous PLSQL block so I cannot use global variables meaning that my variables are not visible within a dynamic PLSQL block.
    I believe this means that either I need to bind the variables within a PLSQL block or use a SELECT FROM INTO SQL block.
    I have tried a few solutions with no luck such as the following PLSQL:
    v_incr_count := 'v_'||v_scheme||'_'||v_cont_cat||'_'||v_emp_cat||'_count';
    sql_stmt := 'BEGIN :a := :a + 1; END;';
    EXECUTE IMMEDIATE sql_stmt USING v_incr_count_name;
    Unfortunately I am getting the 'IN bind variable bound to an OUT position' error which I believe is because it is trying to return a value into v_incr_count_name which has been defined by default as an IN variable. The problem is that I need to store the returned value into the variable whose name is stored in v_incr_count_name.
    Another solution I tried is:
    v_incr_count_name := 'v_'||v_scheme||'_'||v_cont_cat||'_'||v_emp_cat||'_count';
    sql_stmt := 'SELECT '||v_incr_count_name||' + 1 FROM DUAL';
    EXECUTE IMMEDIATE sql_stmt INTO v_return;
    This solution gives me an 'Invalid colum error'
    Any help would be greatly appreciated
    Cheers, Dan

    Repost:
    Problem with variables in dynamic SQL/PLSQL

  • Problem with variables in dynamic SQL/PLSQL

    Hi, I have a problem I am trying to solve using a very short piece of dynamic SQL/PLSQL but I am having problems getting the variable values out of the dynamic block.
    I need to increment a counter which could be any one of 16 counters I am using and I want to avoid using nested IF statements.
    The variable to be incremented is made up of the 'scheme', the 'contributory category' and the 'employment category' (although not every combination is valid)
    The 'scheme' can be either 'no1', 'no2', 'off', 'cg' or 'amc'
    The 'contributory category' can be either 'cont' or 'noncont'
    The 'employment category' can be either 'ft' or 'pt'
    For example the total variable name could be 'v_cg_noncont_ft_count'
    I have created a variable by concatenating the various elements called v_incr_count_name which holds the name of the variable I want to increment.
    I am running this within an anonymous PLSQL block so I cannot use global variables meaning that my variables are not visible within a dynamic PLSQL block.
    As a result I think I will need to use bind variables with a PLSQL block or a SELECT FROM INTO SQL string
    I have tried various solutions including the following PLSQL solution:
    v_incr_count_name := 'v_'||v_scheme||'_'||v_cont_cat||'_'||v_emp_cat||'_count';
    sql_stmt := 'BEGIN :a := :a + 1; END;';
    EXECUTE IMMEDIATE sql_stmt USING v_incr_count_name;
    Unfortunately I am getting the 'IN bind variable bound to an OUT position' error which I suppose makes sense as I am trying to change the value of a variable in the main PLSQL block from within the dynamic block.
    Another (SQL) solution I tried was:
    v_incr_count_name := 'v_'||v_scheme||'_'||v_cont_cat||'_'||v_emp_cat||'_count';
    sql_stmt := 'SELECT '||v_incr_count_name||' + 1 FROM DUAL';
    EXECUTE IMMEDIATE sql_stmt INTO v_return;
    While this executes and returns the incremented value into v_return, I am still left unable to copy the returned value into the variable whose name is stored in v_incr_count_name
    Any help appreciated
    Cheers, Dan

    this shows the syntax for the using clause
    declare
    a number := 1;
    b number := null;
    v varchar2(10) := 'A';
    begin
    execute immediate 'begin :v1 := :v1 + 1; end;' using in out a;
    dbms_output.put_line(a);
    end;
    /but what you want cannot be done
    SQL> declare
      2  a number := 1;
      3  b number := null;
      4  v varchar2(10) := 'A';
      5  begin
      6  execute immediate 'begin '||v||' := :v1 + 1; end;' using in out a;
      7  dbms_output.put_line(a);
      8  end;
      9  /
    declare
    ERROR at line 1:
    ORA-06550: line 1, column 7:
    PLS-00201: identifier 'A' must be declared
    ORA-06550: line 1, column 7:
    PL/SQL: Statement ignored
    ORA-06512: at line 6the variable "V" contains the name of the desired output variable ("A"). this errors because "A" is not defined within the scope of the dynamic sql.
    "into the variable whose name is stored in v_incr_count_name"
    pl/sql does not support dereferencing variables, so you can't do it.

  • How to control the maximum time that a dynamic sql can execute

    Hi,
    I want to restrict the maximum time that a dynamic sql can execute in a plsql block.
    If the execution is not completed, the execution should be terminated and a exception should be raised.
    Please let me know, if there is any provision for the same in Oracle 10g.
    I was reading about Oracle Resource Database Resource Manager, which talks about restricting the maximum time of execution for Oracle session for a user.
    However I am not sure, if this can be used to control the execution of dynamic sql in a plsql block.
    Please provide some pointers.
    Thank you,
    Warm Regards,
    Navin Srivastava

    navsriva wrote:
    We are building a messaging framework, which is used to send time sensitive messages to boundary system.I assume this means across application/database/server boundaries? Or is the message processing fully localised in the Oracle database instance?
    Every message has a time to live. if the processing of message does not occurs within the specified time, we have to rollback this processing and mark the message in error state.This is a problematic requirement.. Time is not consistent ito data processing on any platform (except real-time ones). For example, messageFoo1 has a TTL (time to live) of 1 sec. It needs to read a number of rows from a table. The mere factor of whether those rows are cached in the database buffer cache, or still residing on disk, will play a major role in execution time. Physical I/O is significantly slower that logical I/O.
    As a result, with the rows on disk, messageFoo1 exceeds the 1s TTL and fails. messageFoo2 is an identical message. It now finds most of the rows that were read by messageFoo1 in the buffer cache, enabling it to complete its processing under 1s.
    What is the business logic behind the fact that given this approach, messageFoo1 failed, and the identical messageFoo2 succeeded? The only difference was physical versus logical I/O. How can that influence the business validation/requirement model?
    If it does, then you need to look instead at a real-time operating system and server platform. Not Windows/Linux/Unix/etc. Not Oracle/SQL-Server/DB2/etc.
    TTL is also not time based in other s/w layers and processing. Take for example the traceroute and ping commands for the Internet Protocol (IP) stack. These commands send an ICMP (Internet Control Message Protocol) packet.
    This packet is constructed with a TTL value too. When TTL is exceeded, the packet expires and the sender receives a notification packet to that regard.
    However, this TTL is not time based, but "+hop+" based. Each server's IP stack that receives an ICMP packet as it is routed through the network, subtracts 1 from the TTL and the forwards the packet (with the new TTL value). When a server's IP stack sees that TTL is zero, it does not forward the packet (with a -1 TTL), but instead responds back to the sender with an ICMP packet informing the sender that the packet's TTL has expired.
    As you can see, this is a very sensible and practical implementation of TTL. Imagine now that this is time based.. and the complexities that will be involved for the IP stack s/w to deal with it in that format.
    Making exact response/execution times part of the actual functional business requirements need to be carefully considered.. as this is very unusual and typically only found in solutions implemented in real-time systems.

Maybe you are looking for

  • Problem with colors showing in PE10

    Is there a known problem with color in Elements 10? I have a gif that when opened in paint shows the light blue color correctly but in PSE10 shows as a buff color. Red, yellow and blue seem about right. It doesnt matter if it is the gif or the psd fi

  • Posted this two other places that I shouldn't have. Front Row Video Issues.

    Alrighty, so QuickTime has been giving me some issues with playback with some videos and I've found a player that has fixed this, and can play QuickTime files as well. My question is this, can I change the default FrontRow player from QuickTime to VL

  • No streaming from iTunes to my ATV

    All was well until I "upgraded" to Lion AND the latest iTunes update, now there is no streaming from itunes to my ATV. Any ideas? Thanks FR

  • IPhoto sort events by photo count

    Hi, I need to track down events with 1 photo or more How can I sort events with photo count included? iPhoto'08 7.1.5 MacOSX 10.7.5 or iPhoto 9.6(910.29) MacOSX 10.10 Yosemite

  • Convert %rowtype function return into sys_refcursor

    I have an interesting problem. We have created a standard pattern for all of our "get" functions for returning data to always return a sys_refcursor to our JAVA application as it is very easy to deal with cursors and convert them to Java Collection o