Solved

Printing Values of Bind Variables

Posted on 2009-04-01
10
771 Views
Last Modified: 2013-12-18
I have PL/SQL package where in I am executing the dynamic insert as shown below
--code snippet

v_ins_sql:=

' INSERT INTO tablea 

(col1, col2)

VALUES

(:1,

:2)
 

--p_col1_value, p_col2_value values are passed as parameters to procedure

EXECUTE IMMEDIATE v_ins_sql

USING (p_col1_value, p_col2_value);
 

DBMS_OUTPUT.PUT_LINE(v_ins_sql);

--------------------------------------------------
 

--problem statement

Now, I have log table where in I have to insert the v_ins_sql string along with values.

But I am not getting values of p_col1_value, p_col2_value when I print DBMS_OUTPUT.PUT_LINE(v_ins_sql);
 

I get this 

INSERT INTO tablea (col1, col2) VALUES (:1, :2)
 

I wish to see the values of :1 and :2 and store in log table?

Is this doable in PL/SQL?

Open in new window

0
Comment
Question by:ajexpert
  • 5
  • 4
10 Comments
 
LVL 25

Expert Comment

by:lwadwell
ID: 24045801
Hi ajexpert,

try doing

DBMS_OUTPUT.PUT_LINE(':1=" || p_col1_value || ' :2=' || p_col2_value);

lwadwell
0
 
LVL 25

Expert Comment

by:lwadwell
ID: 24045804
sorry ... quotes not quite right

DBMS_OUTPUT.PUT_LINE(':1=' || p_col1_value || ' :2=' || p_col2_value);
0
 
LVL 40

Expert Comment

by:mrjoltcola
ID: 24045817
Rather than peppering log statements all over your code, and doing your own log/audits, why not use the builtin Oracle features? You can configure Oracle to log statements as well as bind variable values, and you can do it on a fine-grained per table basis.
The danger is using your own logging is unless you catch exceptions, a log statement may cause your overall procedure / transaction to fail.

0
 
LVL 14

Author Comment

by:ajexpert
ID: 24045819
I want to insert the sql statement not using DBMS_OUTPUT.PUT_LINE.
something like
 

' INSERT INTO tablea 

(col1, col2)

VALUES

(:1,

:2)

-----------------------

I wish to store the entire string along with bind variables values and not printing bind variable values separately

 

Open in new window

0
 
LVL 14

Author Comment

by:ajexpert
ID: 24045826
@mrjoltcola
This is one of the package we are developing which requires this logging.
I cannot alter oracle configuration because of this package.
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 25

Expert Comment

by:lwadwell
ID: 24045862
How do you want it look in your log table?  Although I support mrjoltcola's suggestion of using Oracle's built in features.

Using bind variable's (which is good for performance) - the actual values are only resolved at time of execution and will never be part of the executed SQL string.

It would be possible to put replace() or similar functions in place to do the substitution for logging ... or you could concatenate them onto the end ... what is your requirement?
0
 
LVL 14

Author Comment

by:ajexpert
ID: 24045877
My requirement is
suppose I get parameter values as '
p_col1_value = 'test1'
p_col2_value = 100000  
My insert statement should go in log table like this

INSERT INTO tablea 

(col1, col2)

VALUES

('test1',

100000)

Open in new window

0
 
LVL 25

Accepted Solution

by:
lwadwell earned 125 total points
ID: 24045910
REPLACE(REPLACE(sql_str,':1',p_col1_value),':2',p_col2_value) would get you close ... except 'test1' would not be quoted.

REPLACE(REPLACE(sql_str,':1','''' || p_col1_value || ''''),':2',p_col2_value) would have the quotes included.
0
 
LVL 14

Author Comment

by:ajexpert
ID: 24045918
too much of homework I think...
is there any simple way?
I am ok even if I have to just log the statement after EXECUTE IMMEDIATE;
0
 
LVL 14

Author Closing Comment

by:ajexpert
ID: 31565615
Though I didnt implented it, but I got another idea.  Thanks anyways
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

Suggested Solutions

How to Unravel a Tricky Query Introduction If you browse through the Oracle zones or any of the other database-related zones you'll come across some complicated solutions and sometimes you'll just have to wonder how anyone came up with them.  …
Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
This video explains at a high level with the mandatory Oracle Memory processes are as well as touching on some of the more common optional ones.
This video shows how to copy an entire tablespace from one database to another database using Transportable Tablespace functionality.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now