dbTalk Databases Forums  

Execute Immediate

comp.databases.oracle.misc comp.databases.oracle.misc


Discuss Execute Immediate in the comp.databases.oracle.misc forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
The Magnet
 
Posts: n/a

Default Execute Immediate - 09-20-2009 , 01:15 PM






Hi,

I am still working on this one. I have a dynamic statement that looks
like this:

EXECUTE IMMEDIATE 'SELECT count(*) FROM customers_lookup WHERE
customer_id = :customer_id' INTO v_data USING v_customer_id;

That works great, but the actual statement looks more like this:

EXECUTE IMMEDIATE 'SELECT count(*) FROM ' || v_table_name || ' WHERE
customer_id = :customer_id ' INTO v_data USING v_customer_id;

No matter how hard I play with this and the quotes and such, it seems
to not like the fact that I'm putting the statement together in pieces
and complains that CUSTOMER_ID bind variable is not defined. I've
played with a number of quote combinations also with no luck.

I'm still playing with it, but maybe someone else can help me reach
the answer faster.

Many thanks!

Reply With Quote
  #2  
Old   
Maxim Demenko
 
Posts: n/a

Default Re: Execute Immediate - 09-20-2009 , 01:32 PM






The Magnet wrote:
Quote:
Hi,

I am still working on this one. I have a dynamic statement that looks
like this:

EXECUTE IMMEDIATE 'SELECT count(*) FROM customers_lookup WHERE
customer_id = :customer_id' INTO v_data USING v_customer_id;

That works great, but the actual statement looks more like this:

EXECUTE IMMEDIATE 'SELECT count(*) FROM ' || v_table_name || ' WHERE
customer_id = :customer_id ' INTO v_data USING v_customer_id;

No matter how hard I play with this and the quotes and such, it seems
to not like the fact that I'm putting the statement together in pieces
and complains that CUSTOMER_ID bind variable is not defined. I've
played with a number of quote combinations also with no luck.

I'm still playing with it, but maybe someone else can help me reach
the answer faster.

Many thanks!
SQL> create table customers
2 as
3 select empno,ename,deptno customer_id from emp
4 /

Table created.

SQL> declare
2 v_data number;
3 v_customer_id number := &customer_id;
4 v_table_name varchar2(30) :='customers';
5 begin
6 EXECUTE IMMEDIATE 'SELECT count(*) FROM ' || v_table_name || ' WHERE
7 customer_id = :customer_id ' INTO v_data USING v_customer_id;
8 dbms_output.put_line('v_data: '||v_data);
9 dbms_output.put_line('v_customer_id: '||v_customer_id);
10 end;
11 /
Enter value for customer_id: 10
old 3: v_customer_id number := &customer_id;
new 3: v_customer_id number := 10;
v_data: 3
v_customer_id: 10

PL/SQL procedure successfully completed.

SQL> /
Enter value for customer_id: 20
old 3: v_customer_id number := &customer_id;
new 3: v_customer_id number := 20;
v_data: 5
v_customer_id: 20

PL/SQL procedure successfully completed.

SQL> /
Enter value for customer_id: 30
old 3: v_customer_id number := &customer_id;
new 3: v_customer_id number := 30;
v_data: 6
v_customer_id: 30

PL/SQL procedure successfully completed.


Best regards

Maxim

Reply With Quote
  #3  
Old   
The Magnet
 
Posts: n/a

Default Re: Execute Immediate - 09-20-2009 , 01:47 PM



On Sep 20, 1:32*pm, Maxim Demenko <mdeme... (AT) gmail (DOT) com> wrote:
Quote:
The Magnet wrote:
Hi,

I am still working on this one. *I have a dynamic statement that looks
like this:

EXECUTE IMMEDIATE 'SELECT count(*) FROM customers_lookup WHERE
customer_id = :customer_id' INTO v_data USING v_customer_id;

That works great, but the actual statement looks more like this:

EXECUTE IMMEDIATE 'SELECT count(*) FROM ' || v_table_name || ' WHERE
customer_id = :customer_id ' INTO v_data USING v_customer_id;

No matter how hard I play with this and the quotes and such, it seems
to not like the fact that I'm putting the statement together in pieces
and complains that CUSTOMER_ID bind variable is not defined. *I've
played with a number of quote combinations also with no luck.

I'm still playing with it, but maybe someone else can help me reach
the answer faster.

Many thanks!

SQL> create table customers
* *2 *as
* *3 *select empno,ename,deptno customer_id from emp
* *4 */

Table created.

SQL> declare
* *2 *v_data number;
* *3 *v_customer_id number := &customer_id;
* *4 *v_table_name varchar2(30) :='customers';
* *5 *begin
* *6 *EXECUTE IMMEDIATE 'SELECT count(*) FROM ' || v_table_name || ' WHERE
* *7 *customer_id = :customer_id ' INTO v_data USING v_customer_id;
* *8 *dbms_output.put_line('v_data: '||v_data);
* *9 *dbms_output.put_line('v_customer_id: '||v_customer_id);
* 10 *end;
* 11 */
Enter value for customer_id: 10
old * 3: v_customer_id number := &customer_id;
new * 3: v_customer_id number := 10;
v_data: 3
v_customer_id: 10

PL/SQL procedure successfully completed.

SQL> /
Enter value for customer_id: 20
old * 3: v_customer_id number := &customer_id;
new * 3: v_customer_id number := 20;
v_data: 5
v_customer_id: 20

PL/SQL procedure successfully completed.

SQL> /
Enter value for customer_id: 30
old * 3: v_customer_id number := &customer_id;
new * 3: v_customer_id number := 30;
v_data: 6
v_customer_id: 30

PL/SQL procedure successfully completed.

Best regards

Maxim
Many Many thanks.......

Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.