Dynamic SQL with cursor variables in pro*c

Please, what I need to do in order to be able
to do something like this:
EXEC SQL DECLARE :c CURSOR FOR :s;
In other words: I want to use variables
in cursor names.
Is it possible ? How ?
Thank you.

OK. Here is an example of a Dynamic SQL #4 program I wrote several years ago (It's still running in production). It is currently running on a Sun E10K with Oracle 8.1.5. This code is JUST the pro*c part of a program which uses IBM MQ Series. MQ passes this program a Select SQL statement (as well as other parameters) as an argument. The Pro*c code allocates the structures, parses, and fetches, and executes the statement. I realize that this will not be easy to read. There are SOME comments in the code. However, I realize that to read anyone elses code is a chore. When you add Dynamic #4 to the list, it becomes much more complicated. Anyway, you'll probably need to copy and paste the code to a text file. It will be easier to read.
==========================================
Code
==========================================
| Program: mqsql.pc |
| Creator: Jim Wartnick |
| Purpose: XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX |
| XXXXXXXXXXX |
| Date: 01/03/1997 |
| Modifications: |
#include"mqsql.h"
#define DEBUG
#ifdef DEBUG
FILE *fp=stdout;
#endif
int
disconnect_db() {
char msg[256], oraclmsg[256];
char buf[MAX_STRING_LENGTH+4];
int buf_len = 0, msg_len = 0;
EXEC SQL
COMMIT WORK RELEASE;
#ifdef DEBUG
fprintf(fp, " --> Disconnecting from database. RC: %d.\n", sqlca.sqlcode);
fflush(fp);
#endif
if (sqlca.sqlcode != 0) {
buf_len = sizeof(oraclmsg)-1;
sqlglm(oraclmsg, &buf_len, &msg_len);
oraclmsg[msg_len] = '\0';
error("disconnect_db()", "disconnect", sqlca.sqlcode, oraclmsg);
return(FAILURE);
return(SUCCESS);
int
connect_db() {
char msg[256], oraclmsg[256];
char buf[MAX_STRING_LENGTH+4];
int buf_len = 0, msg_len = 0;
char user[4];
strcpy(user, "/");
EXEC SQL
CONNECT :user;
#ifdef DEBUG
fprintf(fp, " --> Connecting to database. RC is %d\n", sqlca.sqlcode);
fflush(fp);
#endif
if (sqlca.sqlcode != 0) {
buf_len = sizeof(oraclmsg)-1;
sqlglm(oraclmsg, &buf_len, &msg_len);
oraclmsg[msg_len] = '\0';
sprintf(Results, "%9d", sqlca.sqlcode);
error("connect_db()", "connect", sqlca.sqlcode, oraclmsg);
return(FAILURE);
return(SUCCESS);
int
allocate_sqlda() {
| Initialize the SQLDA structure. |
| We only need the select descriptor |
| because we do not have any bind |
| variables. |
if ((Select_da = sqlald(
MAX_COLUMN_COUNT,
MAX_COLUMN_STRING_LENGTH,
MAX_INDICATOR_VARS
)) == (SQLDA *) 0) {
#ifdef DEBUG
fprintf(fp, " Memory allocation for Select Descriptor failed.\n");
fflush(fp);
#endif
strcpy(Results, "000000001");
error("allocate_sqlda()", "create SQLDA", 0, "Memory Error");
return(FAILURE);
#ifdef DEBUG
fprintf(fp, " Memory allocation for Select Descriptor succeeded.\n");
fflush(fp);
#endif
Select_da->N = MAX_COLUMN_COUNT;
return(SUCCESS);
int
prepare_sql() {
char msg[256], oraclmsg[256];
int buf_len = 0, msg_len = 0;
| Prepare the Sql statement. |
EXEC SQL
PREPARE sql_stmt
FROM :Sql;
#ifdef DEBUG
fprintf(fp, " Prepared SQL: %s. RC: %d.\n", Sql, sqlca.sqlcode);
fflush(fp);
#endif
if (sqlca.sqlcode != 0) {
buf_len = sizeof(oraclmsg)-1;
sqlglm(oraclmsg, &buf_len, &msg_len);
oraclmsg[msg_len] = '\0';
sprintf(Results, "%9d", sqlca.sqlcode);
error("prepare_sql()", "Parse", sqlca.sqlcode, oraclmsg);
return(FAILURE);
return(SUCCESS);
int
declare_cursor() {
char msg[256], oraclmsg[256];
int buf_len = 0, msg_len = 0;
| Set up the cursor to loop through |
EXEC SQL
DECLARE sql_cursor
CURSOR FOR sql_stmt;
#ifdef DEBUG
fprintf(fp, " Declared cursor. RC: %d\n", sqlca.sqlcode);
fflush(fp);
#endif
if (sqlca.sqlcode != 0) {
buf_len = sizeof( oraclmsg)-1;
sqlglm(oraclmsg, &buf_len, &msg_len);
oraclmsg[msg_len] = '\0';
sprintf(Results, "%9d", sqlca.sqlcode);
error("declare_cursor()", "declare cursor", sqlca.sqlcode, oraclmsg);
return(FAILURE);
return(SUCCESS);
int
open_cursor() {
char msg[256], oraclmsg[256];
int buf_len = 0, msg_len = 0;
| Open the cursor. |
EXEC SQL
OPEN sql_cursor;
#ifdef DEBUG
fprintf(fp, " Opened cursor. RC:%d\n", sqlca.sqlcode);
fflush(fp);
#endif
if (sqlca.sqlcode != 0) {
buf_len = sizeof(oraclmsg)-1;
sqlglm(oraclmsg, &buf_len, &msg_len);
oraclmsg[msg_len] = '\0';
sprintf(Results, "%9d", sqlca.sqlcode);
error("open_cursor()", "open cursor", sqlca.sqlcode, oraclmsg);
return(FAILURE);
return(SUCCESS);
int
describe_select_list() {
char msg[256], oraclmsg[256];
int buf_len = 0, msg_len = 0;
| Get description of columns |
EXEC SQL
DESCRIBE SELECT LIST FOR sql_stmt
INTO Select_da;
#ifdef DEBUG
fprintf(fp, " Described columns. RC %d\n", sqlca.sqlcode);
fflush(fp);
#endif
if (sqlca.sqlcode != 0) {
buf_len = sizeof(oraclmsg)-1;
sqlglm(oraclmsg, &buf_len, &msg_len);
oraclmsg[msg_len] = '\0';
sprintf(Results, "%9d", sqlca.sqlcode);
error("describe_select_list()", "describe select list", sqlca.sqlcode, oraclmsg);
return(FAILURE);
return(SUCCESS);
int
setup_sqlda() {
char buf[MAX_STRING_LENGTH+4];
int done = FALSE, nullok = 0, i = 0;
#ifdef DEBUG
fprintf(fp, " --> Setting up SQLDA.\n");
fflush(fp);
#endif
if (allocate_sqlda() == FAILURE)
return(FAILURE);
if (prepare_sql() == FAILURE)
return(FAILURE);
if (declare_cursor() == FAILURE)
return(FAILURE);
if (open_cursor() == FAILURE)
return(FAILURE);
if (describe_select_list() == FAILURE)
return(FAILURE);
| Too many columns in select list. |
if (Select_da->F < 0) {
strcpy(Results, "000000001");
error("setup_sqlda()", "check select list count", 0, "Too many values in select list");
return(FAILURE);
| Set the number of columns to the actual |
| number of columns. |
Select_da->N = Select_da->F;
| We are going to convert all fields to a string. |
for (i == 0; i < Select_da->F; i++) {
sqlnul(&(Select_da->T), &(Select_da->T[i]), &nullok);
Select_da->T[i] = EXT_STRING;
Select_da->L[i] = MAX_STRING_LENGTH;
| Allocate the result area to be as big as |
| MAX_STRING_LENGTH. |
if ((Select_da->V[i] = malloc(Select_da->L[i])) == NULL) {
#ifdef DEBUG
fprintf(fp, " Allocation of column values failed.\n");
fflush(fp);
#endif
strcpy(Results, "000000001");
error("setup_sqlda()", "allocate column values", 0, "Memory Error");
return(FAILURE);
if ((Select_da->I[i] = (short *) malloc(sizeof(short))) == NULL) {
#ifdef DEBUG
fprintf(fp, " Allocation of idicator values failed.\n");
fflush(fp);
#endif
strcpy(Results, "000000001");
error("setup_sqlda()", "allocate indicator values", 0, "Memory Error");
return(FAILURE);
#ifdef DEBUG
fprintf(fp, " Allocation of memory for values succeeded.\n");
fflush(fp);
#endif
return(SUCCESS);
| add_eom adds the end of message |
| delimiter (an aditional comma). |
int
add_eom() {
char *result_ptr;
if (strlen(Results) >= sizeof(Results) - 1) {
strcpy(Results, "000000001");
error("add_eom()", "Add eom failed. Size overflow", 0, "Memory Error");
return(FAILURE);
result_ptr = &Results[strlen(Results)-1];
while (*result_ptr && (*result_ptr != ','))
result_ptr--;
if (*result_ptr) {
result_ptr++;
*(result_ptr++) = ',';
*result_ptr = '\0';
return(SUCCESS);
int close_cursor() {
char msg[256], oraclmsg[256];
int buf_len = 0, msg_len = 0;
| Close the cursor. |
EXEC SQL
CLOSE sql_cursor;
#ifdef DEBUG
fprintf(fp, " Closing cursor. RC: %d\n", sqlca.sqlcode);
fflush(fp);
#endif
if (sqlca.sqlcode != 0) {
buf_len = sizeof(oraclmsg)-1;
sqlglm(oraclmsg, &buf_len, &msg_len);
oraclmsg[msg_len] = '\0';
error("generate_sql()", "close cursor", sqlca.sqlcode, oraclmsg);
return(FAILURE);
return(SUCCESS);
int
fetch() {
char msg[256], oraclmsg[256];
char buf[MAX_STRING_LENGTH+4];
int buf_len = 0, msg_len = 0;
EXEC SQL
FETCH sql_cursor
USING DESCRIPTOR Select_da;
#ifdef DEBUG
fprintf(fp, " --> Fetching rows. RC %d\n", sqlca.sqlcode);
fflush(fp);
#endif
if (sqlca.sqlcode != 0) {
if (sqlca.sqlcode != NODATAFOUND) {
buf_len = sizeof(oraclmsg)-1;
sqlglm(oraclmsg, &buf_len, &msg_len);
oraclmsg[msg_len] = '\0';
error("fetch()", "Error fetching row.", sqlca.sqlcode, oraclmsg);
return(sqlca.sqlcode);
return(NODATAFOUND);
return(SUCCESS);
| Free up any memory structures. |
void
free_memory() {
int i = 0;
for (i = 0; i < Select_da->F; i++) {
free(Select_da->V[i]);
free(Select_da->I[i]);
| generate_sql() uses the message we received from the queue |
| (a SQL statement) to query the database. We have to use |
| dynamic Sql Version 4 for this type of Sql. The number of |
| columns we are selecting is unknown. This means we can't |
| use the INTO clause. |
int
generate_sql() {
char buf[MAX_STRING_LENGTH+4];
int rc = SUCCESS, done = FALSE, nullok = 0, i = 0;
#ifdef DEBUG
if ((fp = fopen(SQLLOG, "a")) == NULL)
fp = stderr;
#endif
| Connect to the database |
if (connect_db() == FAILURE)
return(FAILURE);
if (setup_sqlda() == FAILURE)
return(FAILURE);
| Place the answer in a comma delimited buffer. |
memset(Results, NULL, sizeof(Results));
done = FALSE;
while (!done) {
rc = fetch();
if (rc != SUCCESS) {
if (Results[0] == '\0')
sprintf(Results, "%9d,", rc);
done = TRUE;
else {
| Put return code of success in first. |
if (Results[0] == '\0')
strcpy(Results, "000000000,");
for (i = 0; i < Select_da->F; i++) {
Select_da->V[i][Select_da->L[i]] = '\0';
| Check to see if the value is null. |
if (*Select_da->I[i] < 0)
strcpy(buf, " ,");
else
sprintf(buf, "%s,", Select_da->V[i]);
if (strlen(Results) + strlen(buf) > sizeof(Results)) {
strcpy(Results, "000000001");
error("generate_sql()", "String concat failed. Size overflow", 0, "Memory Error");
return(FAILURE);
strcat(Results, buf);
#ifdef DEBUG
fprintf(fp, " --> Results %s\n", Results);
fflush(fp);
#endif
| Close the cursor. |
close_cursor();
| Disconnect from the database |
disconnect_db();
| Remove trailing comma. |
if (add_eom() == FAILURE)
return(FAILURE);
#ifdef DEBUG
fflush(fp);
#endif
free_memory();
#ifdef DEBUG
fclose(fp);
#endif
null

Similar Messages

  • Problem in SQL with CURSOR( ) ,Why the CURSOR did not work?

    hi All:
    I have a problem in SQL with CURSOR.
    The data is as the attachments.
    Here is the SQL statement as follow:
    SELECT A.WADCTO,A.WADOCO,B.IGCOST,CURSOR (SELECT X.IGLITM
    FROM F3102 X
    WHERE X.IGDOCO=A.WADOCO
    AND X.IGCOST IN ('B1','D1','C3')) AS DETAIL
    FROM F4801 A INNER JOIN F3102 B ON A.WADOCO=B.IGDOCO AND A.WADCTO=B.IGDCTO AND B.IGCOST>' '
    WHERE A.WADOCO='10004'
    The statement above returns records as follow:
    WADC WADOCO IGCOST DETAIL
    WO 10004 A1 CURSOR STATEMENT : 4
    CURSOR STATEMENT : 4
    IGLITM
    1KV90CPG2
    1KV90CPG2
    1KV90CPG2
    But, after I add one statement in the subquery, there is no record returned from CURSOR.
    Here is the SQL statement:
    SELECT A.WADCTO,A.WADOCO,B.IGCOST,CURSOR (SELECT X.IGLITM
    FROM F3102 X
    WHERE X.IGDOCO=A.WADOCO
    AND X.IGCOST=B.IGCOST
    AND X.IGCOST IN ('B1','D1','C3')) AS DETAIL
    FROM F4801 A INNER JOIN F3102 B ON A.WADOCO=B.IGDOCO AND A.WADCTO=B.IGDCTO AND B.IGCOST>' '
    WHERE A.WADOCO='10004'
    The statement above returns records as follow:
    WADC WADOCO IGCOST DETAIL
    WO 10004 A1 CURSOR STATEMENT : 4
    CURSOR STATEMENT : 4
    no rows selected
    Why the CURSOR did not work?
    The database version is Oracle Database 10g Release 10.2.0.4.0 - 64bit Production.
    F3102 DATA:
    IGDOCO     IGDCTO     IGLITM     IGCOST
    10004     WO     1KV90CPG2      A1
    10004     WO     1KV90CPG2      B1
    10004     WO     1KV90CPG2      C3
    10004     WO     1KV90CPG2      D1
    F4801 DATA:
    WADCTO     WADOCO
    WO     10004
    Edited by: user2319139 on 2010/3/2 上午 1:17
    Edited by: user2319139 on 2010/3/2 上午 1:20

    Why this structure and not a join?
    The cursor() function returns a cursor handle that needs to be processed - in other words, the client needs to fetch data from it. The Oracle® Database SQL Reference+ (http://download.oracle.com/docs/cd/B19306_01/server.102/b14200/expressions005.htm#i1035107) describes it as being equivalent to a ref cursor handle.
    Thus why are you creating ref cursor handles as a column in a SQL projection - where each row will have a "+nested+" ref cursor handle to process. What problem are you attempting to hack solve this way?

  • Dynamic sql with dynamic bulk collection variable

    Hi,
    I am facing the issue while bulk collecting dynamic sql query data into dynamic variable.
    Eg:
    query1:= << dynamic select query>>
    Execute immediate query1 bulk collect into Dynamic_varibale;
    here dynamic_varible is pl/sql table type with 1 column.
    How do i declare "dynamic_variable" here????
    please suggest...

    create type t_id is table of number
    SQL> create type t_id is table of number
      2  /
    Type created.
    SQL> declare
      2
      3   v_tid t_id;
      4   v_results sys_refcursor;
      5
      6   v_employee_id number;
      7   v_name varchar2(100);
      8
      9   v_sql varchar2(1000);
    10
    11
    12  begin
    13   v_tid := t_id(7902,7934);
    14
    15  --
    16
    17
    18   v_sql := 'select empno, ename from scott.emp ' ||CHR(10)
    19           || 'where empno in (select column_value from table(cast(:v_tid as
    t_id)))';
    20
    21   dbms_output.put_line(v_sql);
    22   dbms_output.put_line('----------');
    23
    24   open v_results for v_sql using v_tid;
    25
    26
    27   IF v_results IS NOT NULL
    28     THEN
    29        LOOP
    30           FETCH v_results
    31            INTO v_employee_id, v_name;
    32
    33           EXIT WHEN (v_results%NOTFOUND);
    34           dbms_output.put_line(v_name);
    35        END LOOP;
    36
    37        IF v_results%ISOPEN
    38        THEN
    39           CLOSE v_results;
    40        END IF;
    41    END IF;
    42
    43  end;
    44  /
    select empno, ename from scott.emp
    where empno in (select column_value from
    table(cast(:v_tid as t_id)))
    FORD
    MILLER

  • Dynamic SQL and IN CLAUSE from Pro C code

    Hi Guys,
    Tyring to embed sql in Pro C. Here I don't know in hand how many items will be there in the IN Clause of my dynamic sql. Tried this with a loop and then adding actual values to the stement and then executing it. This worked but as this hard coding makes it literal sql and hence hampers performance. Can any one help me with how to put bind variables where we don't know how many of them will be there in a dynamic sql.
    Thanks,

    Dynamic SQL supports user defined types, try passing a collection and using TABLE(CAST(collection)) in the SQL statement.
    In the current approach (creating IN clause at runtime) keep in mind that in a IN clause you can put a maximum of 255 elements...
    Max

  • Dynamic SQL Stored Procedure call in Pro*C Application

    Hello All
    Could you help with a sample Pro*C program (or snippet) which has a dynamic call to a stored procedure? The procedure to be called is determined at run time and so also the number of parameters to it.
    Is this even possible using Pro*C?
    Thanks in Advance
    RD

    Hi Colin & Faust
    We found sample pro*c programs for dynamically calling PL/SQL blocks but not for dynamically calling stored procedures.
    Heres the program we compiled successfully but it doesnt work, it gives a core dump on execution. Could you point out whats wrong.
    #include<stdio.h>
    #include<iostream>
    #include<fstream>
    #include<stdlib.h>
    #include<sqlca.h>
    #include<string>
    #include<sys/types.h>
    #include<sys/ipc.h>
    #include<sys/msg.h>
    #include<thread.h>
    #include<sqlda.h>
    #include<sqlcpr.h>
    using namespace std;
    #define TRUE 1
    int main()
    EXEC SQL BEGIN DECLARE SECTION;
    char *UserID = getenv( "DATABASE" );
    char query[100];
    EXEC SQL END DECLARE SECTION;
    SQLDA *bind_des;
    EXEC SQL CONNECT :UserID;
    EXEC SQL WHENEVER SQLERROR CONTINUE;
    bind_des = SQLSQLDAAlloc( 0, 2, (size_t)10, (size_t)0 );
    bind_des->N = 2;
    strcpy( query, "CALL sel_dept( :empno,:name );");
    //Calling stored procedure sel_dept( no NUMBER IN, name VARCHAR2 OUT)
    EXEC SQL PREPARE sql_stmt from :query;
    EXEC SQL DESCRIBE BIND VARIABLES FOR sql_stmt INTO bind_des;
    bind_des->N = bind_des->F;
    //cout << " bind_des->N after assigment = " << bind_des->N;
    //--------------------- Enter the Input values -----------------------------
    //parameter :1
    bind_des->L[0] = 4;
    /* Allocate storage for value and null terminator. */
    bind_des->V[0] = (char *) malloc(bind_des->L[0] + 1);
    /* Store value in bind descriptor. */
    strcpy(bind_des->V[0], "50");
    /* Set datatype to STRING. */
    bind_des->T[0] = 2;
    //parameter :2
    bind_des->L[1] = 20;
    /* Allocate storage for value and null terminator. */
    bind_des->V[1] = (char *) malloc(bind_des->L[1] + 1);
    /* Store value in bind descriptor. */
    strcpy(bind_des->V[1], "");
    /* Set datatype to STRING. */
    bind_des->T[1] = 5;
    EXEC SQL EXECUTE sql_stmt;
    //cout<<"name"<<"\t"<bind_des->V[0]<<"\t"<<bind_des->V[1]<<endl;
    cout << "name"<<"\t";
    cout << "bind_des->V[0]=" << bind_des->V[0]<<"\t";
    cout << "bind_des->V[1]=" << bind_des->V[1]<<"\t"<<endl;
    for (int i = 0; i < bind_des->F; i++) /* for bind descriptor */
    free(bind_des->V);
    free(bind_des->I[i]);
    SQLSQLDAFree(0, bind_des);
    EXEC SQL COMMIT;
    return 0;

  • How execute a dynamic statement with a variable number of bind variables

    Hi all.
    I would like to execute SQL statements in a PL/SQL function.
    SQL statements must use bind variable in order to avoid parsing time. But the number of arguments depends on the context. I can have from 10 to several hundreds of arguments (these arguments are used in a 'IN' clause).
    To minimise the number of differents signature (each new signature involve a parsing), the number of argument is rounded.
    My problem is : how to set dynamicaly the bind variables ?
    Cause it is pretty simple to construct dynamicaly the SQL statement, but using an
    " OPEN .... USING var1, var2, ..., varX "
    statement, it is not possible to handle a variable nomber of bind variable.
    I am looking for the best way to do the same thing that it can be done in java/JDBC with the PreparedStatement.setObject(int parameterIndex, Object x).
    I saw the dbms_sql package and bond_variable procedure : is a the good way to do such a thing ?
    Thanks

    If the variation is only values in an IN list, I would suggest using an object type for the bind variable. This lets you have just one bind variable, regardless of how many values are in the IN list.
    The dynamic SQL ends up looking like:
    ' ... where c in (select * from table(:mylist))' using v_list;where v_list is a collection based on a SQL user-defined type that can be populated incrementally, or in one shot from a delimited list of values using a helper function.
    I use this approach all the time in dynamic searches.
    See this link for more details:
    http://asktom.oracle.com/pls/ask/f?p=4950:8:::::F4950_P8_DISPLAYID:110612348061

  • Cursor Variables in Pro*C/C++

    I have very strange behaivour of the client application that uses cursor variable.
    I have Oracle 8i enterprise 8.1.6 release for Sun/Solaris.
    The follow SQL is installed on server:
    create or replace package dummy is
    type vemap_cur is ref cursor;
    end;
    create or replace procedure GET_CUR
    ( N IN number, cur IN OUT dummy.VEMAP_CUR ) as
    begin
    open cur for
    select LS.ID, LS.SCALE
    from LAYERS LS
    where LS.ID = (select C.ID from CTM C where C.ORDERINDEX = N);
    end;
    This procedure (GET_CUR) i call from embedded SQL in follow manner:
    EXEC SQL BEGIN DECLARE SECTION;
    struct
    int n1;
    int n2;
    } resrec;
    sql_cursor c1;
    int num;
    EXEC SQL END DECLARE SECTION;
    EXEC SQL ALLOCATE :c1;
    EXEC SQL AT DB_VE EXECUTE
    BEGIN
    GET_VEC( :num, :c1 );
    END;
    END-EXEC;
    Till now it is Ok. But when i run the follow line
    EXEC SQL FETCH :c1 INTO :resrec;
    i accept ORA-01012: not logged on
    error !!!!
    I checked the connection via using cursor in paralell and got the correct answer.
    Therefore i think this error is not related to the actual problem and i have no idea what this problem is. I tried to open cursor via anonymous PL/SQL block inside my C program with same result.
    Need help ASAP.
    Leonid
    null

    Hi Leonid, Andrea
    When you use "CONNECT AT :db_handle" instead of default connection "CONNECT", you have to give the connect handle to the command you want to execute.
    ie:
    EXEC SQL AT :db_handle PREPARE SQL_stat FROM "select ...";
    EXEC SQL AT :db_handle DECLARE cur_stat CURSOR for SQL_stat;
    EXEC SQL AT :db_handle OPEN cur_stat ;
    EXEC SQL AT :db_handle FETCH cur_stat INTO :resrec;
    Leonid, the error you had is probably because you connected at "DB_VE", and tried to select from default connect (that you're not connected to ==> ORA-01012)
    Try EXEC SQL AT :DB_VE FETCH :c1 INTO :resrec;
    or, if you connect to only 1 database, use "EXEC SQL CONNECT;" instead of (EXEC SQL CONNECT :pConnectString AT "DB_VE";) so that you use default connect name and you don't have to add "AT :DB_VE" to your SQL commands.
    I know this reply comes long after your request, but I hope it may still help anybody.

  • Cannot create dynamic page with cursor based on linked table

    I get the following error when i try to create a dynamic portal page that uses a pl/sql cursor:
    ORA-06550: line 1, column 24:
    PL/SQL: ORA-00980: synonym translation is no longer valid
    ORA-06550: line 1, column 24:
    PL/SQL: SQL Statement ignored (WWV-11230)
    Failed to parse as REPORTS - DECLARE CURSOR C1 IS SELECT * FROM
    [email protected]; BEGIN FOR R1 IN C1 LOOP HTP.P(','||'<BR>');
    END LOOP; END; (WWV-08300)
    Dynamic page:
    <HTML>
    <HEAD>
    <TITLE>Example</TITLE>
    </HEAD>
    <BODY>
    <H2>Example of A Dynamic Page</H2>
    <ORACLE>
    declare
    cursor c1 is
    select * from [email protected];
    begin
    for r1 in c1 loop
    htp.p('hello<br>');
    end loop;
    end;
    </ORACLE>
    </BODY>
    </HTML>
    When i use the sql query from the cursor in the page below, i get no errors:
    <HTML>
    <HEAD>
    <TITLE>Example</TITLE>
    </HEAD>
    <BODY>
    <H2>Example of A Dynamic Page</H2>
    <ORACLE>select * from [email protected]</ORACLE>
    </BODY>
    </HTML>
    I tried a dynamic page with a cursor on session_roles and had no problems. I assume that there is an issue with the database link or the privileges. The queries seem to get executed under portal_public;
    Oracle Portal Version: 9.0.4.0.99

    I get the following error when i try to create a dynamic portal page that uses a pl/sql cursor:
    ORA-06550: line 1, column 24:
    PL/SQL: ORA-00980: synonym translation is no longer valid
    ORA-06550: line 1, column 24:
    PL/SQL: SQL Statement ignored (WWV-11230)
    Failed to parse as REPORTS - DECLARE CURSOR C1 IS SELECT * FROM
    [email protected]; BEGIN FOR R1 IN C1 LOOP HTP.P(','||'<BR>');
    END LOOP; END; (WWV-08300)
    Dynamic page:
    <HTML>
    <HEAD>
    <TITLE>Example</TITLE>
    </HEAD>
    <BODY>
    <H2>Example of A Dynamic Page</H2>
    <ORACLE>
    declare
    cursor c1 is
    select * from [email protected];
    begin
    for r1 in c1 loop
    htp.p('hello<br>');
    end loop;
    end;
    </ORACLE>
    </BODY>
    </HTML>
    When i use the sql query from the cursor in the page below, i get no errors:
    <HTML>
    <HEAD>
    <TITLE>Example</TITLE>
    </HEAD>
    <BODY>
    <H2>Example of A Dynamic Page</H2>
    <ORACLE>select * from [email protected]</ORACLE>
    </BODY>
    </HTML>
    I tried a dynamic page with a cursor on session_roles and had no problems. I assume that there is an issue with the database link or the privileges. The queries seem to get executed under portal_public;
    Oracle Portal Version: 9.0.4.0.99

  • Dynamic sql and cursors

    We are running an oracle sql procedure that uses a LOT of dynamic sql. We are using a 3rd party package (SQR) as a sort of shell to run the sql procedure. The 3rd party package passes to us an oracle error. This error says, in effect, that there are no inactive database cursors available and that the sql program is too large to process. We conclude from this that we must increase one or more of the cursor parameters in init.ora (v$parameters). Is this the correct assumption? If not, does anyone know what we can do? We'd prefer not to break up the sql procedure into smaller pieces.

    increase the parameter for open cursors.
    check, wether all cursors in your programs are closed in time, or if you are using ref cursors from front-ends (e.g. Java JDBC) that this front-ends close these ref cursors , too.
    If you want to decrease the size of procedures get rid of comments, superfluos spaces, tabs, etc.
    keep a commented version outside vor documentation purposes.
    Hope thsi helps

  • How to reduce different versions of SQL with bind variables

    There is an application, developed on Java.
    Application executes SQL following types:
    select * from t where id in (:1)
    select * from t where id in (:1, :2)
    select * from t where id in (:1, :2, :3)
    select * from t where id in (:1, :2, :3, :4)
    ...And as a result very many versions (thousands) of similar SQL.
    Do you know a method to reduce number of such SQLs?
    I see one method: use one form of SQL with large number of prepared bid variables.
    Like as
    select * from t where id in (:1, :2, :3, :4, :5, :6, :7, :8, ...);And if query will be executed with one variable, the others will be equal to null.
    Is there another method?

    Cannot you insert those values into a temporary table and work within a subquery against that table ? That will make the code more secure, especially if the number of values is high.
    Nicolas.

  • Preparing Dynamic SQL statement for inserting in Pro*C

    Hi Friends,
    From quite some time i am struggling writing Dynamic SQL statement for dynamic insert and update in Pro*C.
    Can somebody go through my code and suggest me the rigth way of doing.
    Right now it throws an error saying " Error while updating ORA-00904: invalid column name "
    Please help me.
    Girish.
    int main()
    EXEC SQL BEGIN DECLARE SECTION;
    char *uid ="scott/tiger";
    static char sqlstmt[129];
    struct /* DEPT record */
    int dept_num;
    char dept_name[15];
    char location[14];
    } dept_rec;
    EXEC SQL END DECLARE SECTION;
    EXEC SQL WHENEVER SQLERROR DO sql_error();
    EXEC SQL CONNECT :uid;
    dept_rec.dept_num = 50;
    strcpy(dept_rec.dept_name,"ADMIN");
    strcpy(dept_rec.location,"IN");
    strcpy(sqlstmt,"UPDATE dept set DNAME = dept_rec.dept_name where DEPTNO = dept_rec.dept_num");
    EXEC SQL EXECUTE IMMEDIATE:sqlstmt;
    EXEC SQL COMMIT;
    exit(0);
    void sql_error()
    printf("\nError while updating %s",sqlca.sqlerrm.sqlerrmc);
    EXEC SQL ROLLBACK;
    }

    A bit rusty here but this is how I see it.
    Think of it this way ..
    all Oracle is going to see is:
    UPDATE dept set DNAME = dept_rec.dept_name where DEPTNO = dept_rec.dept_num
    Its NOT going to know what dept_rec.dept_name is or dept_rec.dept_num is ..
    it doesnt go back and fill in those values.
    You need something like
    strcpy(sqlstmt,"UPDATE dept set DNAME = \"");
    strcat(sqlstmt,dept_rec.dept_name);
    strcat(sqlstmt,"\" where DEPTNO = ");
    strcat(sqlstmt,dept_rec.dept_num);
    printf(sqlsmt); # Just to be sure the update statement look right during testing.

  • Want to create dynamic SQL with table join

    I want to create a dynamic SQL query so that the user can enter any two table names on the selection screen and two field names that he wants to join. After which i should be able to dynamically generate the SQL (native or open) and show the result as report. I have already read the forum threads and know how to create dynamic SQL for single table, what i m struggling with is how to do inner join. I know i can use nested select but that will make the query very slow because i want to extend it to more than 2 tables later.
    Will give points to useful answer for sure, thanks for reading.

    Hi,
    Following is a piece of code which I have used in my program.
    DATA: ws_flds(72)   TYPE c.
    DATA: ftab  LIKE TABLE OF ws_flds.
    ws_flds = 'rbukrs rprctr racct ryear '.
      APPEND ws_flds TO ftab.
       SELECT (ftab)
        INTO CORRESPONDING FIELDS OF TABLE it_grp_glpca
        FROM glpca FOR ALL ENTRIES IN i_cert_item
        WHERE kokrs = c_kokrs
            AND rldnr = '8A'
            AND rrcty IN ('0','2')
            AND rvers  = '000'
            AND rbukrs = i_cert_item-bukrs
            AND ryear  = p_ryear
            AND rprctr = i_cert_item-prctr
            AND racct  = i_cert_item-saknr
            AND ( poper BETWEEN '001' AND ws_poper )
            AND aufnr IN s_aufnr
            AND kostl IN s_kostl
            AND rfarea IN s_fkber
            AND rmvct IN s_rmvct
            AND sprctr IN s_sprctr
            AND ( racct BETWEEN c_low AND c_high ).
    You can now pass your table name as (p_table) or append fieldnames to the internal table (ftab). if it is join then you can append this table like abukrs asaknr..etc.
    Regards
    Subramanian

  • Error in Dynamic LOV with Bind Variable

    Hi
    I created 2 Dynamic LOV's in which Second one is with a Bind Variable.Then I creted a Form and Attached the LOV's to the form fields.But I am getting the below mentioned error when i choose a value in the First LOV and the Second LOV is not Populated. I tried the same thing with the scott.dept and scott.emp table which is working fine.but when i try the same on my tables it is throwing me error.
    Can Anyone Suggest me what is the problem of these LOVs
    FYI
    1)I am writing SELECT on Views in the LOVs and the views are created on tables of a different of database.
    2)Below Mentioned LINK_TIT is my DB Link.
    Error Message:
    An unexpected error occurred: ORA-01722: invalid number
    ORA-02063: preceding line from LINK_TIT (WWV-16016)
    Error displaying form : ORA-01722: invalid number
    ORA-02063: preceding line from LINK_TIT (WWV-16408)
    Error displaying block : ORA-01722: invalid number
    ORA-02063: preceding line from LINK_TIT (WWV-16406)
    Error displaying item : ORA-01722: invalid number
    ORA-02063: preceding line from LINK_TIT (WWV-16404)
    Error ORA-01722: invalid number
    ORA-02063: preceding line from LINK_TIT, displaying DUMMY_FRM_BLEND.DEFAULT.SKU5ID.01, combobox (WWV-16405)
    The preference path does not exist: ORACLE.WEBVIEW.PARAMETERS.16172911255 (WWC-51000)

    Hi Everyone,
    This was a known Issue in Oracle 9i AS Portal.I referred to the
    metalink note ID 174116.1 which talks about the BUG No:1584284.and it gives some workaround to come across the BUG.
    I got my dependent LOV work after changing the NUMBER datatype in the Procedure to VARCHAR2.(Then in the PL i converted the character to number by the SQL function.)
    Regds
    Rajesh Kanna.V

  • Dynamic sql and bind variables

    Hi,
    I have a stored procedure which filters a table on 5 five columns. The filters come from the input parameters,
    and these 5 parameters can come in any combination, I mean some of them may be null and some of them may not be null.
    So I constructed the where filter of the query with IF blocks like the following:
    dynamic_query := 'select * from TESTTABLE where 1= 1';
    IF (P1 is not null) THEN
    dynamic_query := dynamic_query || ' AND column1 = :1';
    END IF;
    IF (P2 is not null) THEN
    dynamic_query := dynamic_query || ' AND column2 = :2';
    END IF;
    IF (P3 is not null) THEN
    dynamic_query := dynamic_query || ' AND column3 = :3';
    END IF;
    IF (P4 is not null) THEN
    dynamic_query := dynamic_query || ' AND column4 = :4';
    END IF;
    IF (P5 is not null) THEN
    dynamic_query := dynamic_query || ' AND column5 = :5';
    END IF;
    OPEN CUR_OUT FOR dynamic_query USING P1, P2, P3, P4, P5;
    The problem is how can I construct the USING and bind parameters, I cannot use "USING P1, P2, P3, P4, P5" because some of bind variables
    may not be in dynamic query if the input parameters are null. Is there a way to overcome this problem without writing all the 2 ^ 5 combinations?
    Any help is greatly appreciated.

    here it is in the Tomer Cohen way:
    IF (P1 is not null) THEN
    dynamic_query := dynamic_query || ' AND column1 = :1';
    ELSE
    dynamic_query := dynamic_query || ' AND  :1 IS NULL';
    END IF;
    IF (P2 is not null) THEN
    dynamic_query := dynamic_query || ' AND column2 = :2';
    ELSE
    dynamic_query := dynamic_query || ' AND  :2 IS NULL';
    END IF;
    IF (P3 is not null) THEN
    dynamic_query := dynamic_query || ' AND column3 = :3';
    ELSE
    dynamic_query := dynamic_query || ' AND  :3 IS NULL';
    END IF;
    IF (P4 is not null) THEN
    dynamic_query := dynamic_query || ' AND column4 = :4';
    ELSE
    dynamic_query := dynamic_query || ' AND  :4 IS NULL';
    END IF;
    IF (P5 is not null) THEN
    dynamic_query := dynamic_query || ' AND column5 = :5';
    ELSE
    dynamic_query := dynamic_query || ' AND -1 = :5';
    END IF;
    OPEN CUR_OUT FOR dynamic_query USING P1, P2, P3, P4, P5;Amiel Davis

  • Forms: sql with bind variable as default value to form field?

    I am using a form as a way to show details of already entered rows in a table. A report passes an id parameter to the form to get it to show the right data. No insert or update is possible. (I am using a form rather than a report because I need to display BLOBs, something reports can't do.)
    I need to execute sql statements based on the passed id to fill certain fields in the form with data from other tables. For example: The table the form is based on includes a category_id, whereas I want to display the category name which resides in another table.
    How would I best achieve this?

    Hi,
    You do this in the additional plsql code section of the form by getting the value of the category_id from the session variables and then getting its name from the cateogory table and assigning it to the category name column.
    Sample Code
    declare
    catid number;
    catnm varchar2(30);
    blk varchar2(10) := 'DEFAULT';
    begin
    catid := p_session.get_value_as_varchar2(
    p_block_name => blk,
    p_attribute_name => 'A_CATEGORY_ID');
    select category_name
    into catnm
    from cat_table
    where category_id = cat_id;
    p_session.set_value(
    p_block_name => blk,
    p_attribute_name => 'A_CATEGORY_NAME',
    p_value => catnm
    end;
    Thanks,
    Sharmila

Maybe you are looking for

  • Cannot install lion. "The Disk is Locked" help please!

    I was hoping someone could help me.  I just downloaded the Mac OS X Lion and when trying to install, it says there is an error with the disk and that it is locked and cannot install.  Does someone know how I can fix this?  Now I am also worried that

  • IPod Shuffle (2nd Gen) not being recognised or playing

    My iPod shuffle is not being recognised by iTunes, and I cannot play any music through it. When I connect it to my iMac the green LED flashes 3 times, then goes out, and nothing else happens. Can anyone help? Thanks

  • Badi for changing Stock type in MIGO transaction

    Hello ALL, IS there any BADI or EXIT for changing stock type in MIGO when we are doing GR against PO then we need to change the Stock type  to Unrestricted stock. Please let me know.Awarded if useful. Regards

  • Blend: An internal error occurred. Please try again.

    Hello, I am running Blend 1.1.0.17, connecting to a Q5 running 10.3.1, and am having problems maintaining a connection to the device.  When running Blend, I get the following: It initially connects, and I can see emails, text and BBM's.  I seem to be

  • Convert Rawstring to Binary

    Hi All, I have a field of type rawstring and it should be converted to binary format. Please let me know how to convert? is there any function module available? Thanks & Regards, Moni