Solved

Printing Values of Bind Variables

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
dates - loop 12 65
sql query 9 45
Oracle - Create Procedure with Paramater 16 63
Oracle SQL Select unique values from two columns 4 53
Note: this article covers simple compression. Oracle introduced in version 11g release 2 a new feature called Advanced Compression which is not covered here. General principle of Oracle compression Oracle compression is a way of reducing the d…
Background In several of the companies I have worked for, I noticed that corporate reporting is off loaded from the production database and done mainly on a clone database which needs to be kept up to date daily by various means, be it a logical…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
This video shows how to copy an entire tablespace from one database to another database using Transportable Tablespace functionality.

776 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