Solved

Printing Values of Bind Variables

Posted on 2009-04-01
10
785 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
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 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
 
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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
dbms_crypto.decrypt   errors out 6 33
Oracle Insert not working 10 30
ORA-02288: invalid OPEN mode 2 56
Database Design Dilemma 6 58
Truncate is a DDL Command where as Delete is a DML Command. Both will delete data from table, but what is the difference between these below statements truncate table <table_name> ?? delete from <table_name> ?? The first command cannot be …
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.  …
This video shows syntax for various backup options while discussing how the different basic backup types work.  It explains how to take full backups, incremental level 0 backups, incremental level 1 backups in both differential and cumulative mode a…
This video shows how to Export data from an Oracle database using the Datapump Export Utility.  The corresponding Datapump Import utility is also discussed and demonstrated.

808 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