Solved

Printing Values of Bind Variables

Posted on 2009-04-01
10
795 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

How to Create User-Defined Aggregates in Oracle Before we begin creating these things, what are user-defined aggregates?  They are a feature introduced in Oracle 9i that allows a developer to create his or her own functions like "SUM", "AVG", and…
From implementing a password expiration date, to datatype conversions and file export options, these are some useful settings I've found in Jasper Server.
This video shows setup options and the basic steps and syntax for duplicating (cloning) a database from one instance to another. Examples are given for duplicating to the same machine and to different machines
This video shows how to Export data from an Oracle database using the Original Export Utility.  The corresponding Import utility, which works the same way is referenced, but not demonstrated.

717 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