The use of bind variables (in &; out) with sql dynamic
I have a table that contains code snippets to make postings on a set of pl/sql database. what the code does is basically receives an ID and returns a number of errors found.To run the code, I use dynamic sql with two bind variables.
When codes consists of a simpel query, it works like a charm, for example with this code:
BEGIN
SELECT COUNT (1)
INTO :1
FROM articles atl
WHERE ATL.CSE_ID = :2 AND cgp_id IS NULL;
END;
However when I get to post more complexes that must perform calculations or run several queries I run into trouble.I have boiled down the problem into that:
DECLARE
counter NUMBER;
my_id NUMBER := 61;
BEGIN
EXECUTE IMMEDIATE ('
declare
some_var number;
begin
select 1 into some_var from dual
where :2 = 61;
:1 := :2;
end;
')
USING OUT counter, IN my_id;
DBMS_OUTPUT.put_line (counter || '-' || my_id);
END;
This code is not really make sense, but it's just to show you what is the problem. When I run this code, I get the errorORA-6537 ON bind variable linked to a position IN
The error doesn't seem wise,: 2 is the only one IN bind variable and it is only used in a where clause clause.
As soon as I remove this where clause, the code works again (giving me 61-61, in case you want to know).
Any idea what goes wrong? I just use bind variables in a way that you're not supposed to use it?
I'm using Oracle Database 11 g Enterprise Edition Release 11.2.0.3.0 - 64 bit
Correction. With immediate execution , the binding is in position, but binds do not need to be repeated. My statement above is incorrect...
You must link only once - but bind by position. And the connection must correspond to the use of the variable binding.
If the connection never variable assigns a value in the code, link by in.
If the binding variable assigns a value in the code, link as OUTPUT.
If the binding variable assigns a value and is used a variable in another statement in the code, link as IN OUT.
For example
SQL> create or replace procedure FooProc is
2 cnt number;
3 id number := 61;
4 begin
5 execute immediate
6 'declare
7 n number;
8 begin
9 select
10 1 into n
11 from dual
12 where :var1 = 61; --// var1 is used as IN
13
14 :var2 := n * :var1; --// var2 is used as OUT and var1 as IN
15 :var2 := -1 * :var2; --// var2 is used as OUT and IN
16 end;
17 '
18 using
19 in out id, in out cnt; --// must reflect usage above
20
21 DBMS_OUTPUT.put_line ( 'cnt='||cnt || ' id=' || id);
22 end;
23 /
Procedure created.
SQL>
SQL> exec FooProc
cnt=-61 id=61
PL/SQL procedure successfully completed.
SQL>
Tags: Database
Similar Questions
-
Ensure this soft analysis the use of Bind variables
Hi Experts,
I have a request when the query is prepared dynamically in Java code and if I take the query and run it from SQL Navigator, it takes a long time to prepare the statement and then execute it quickly. I think we can use dynamic SQL statements and bind variable to improve performance here. However, I tried to do a PDS to check my understanding. I created 2 procedures
1. mode of operation
2. mode of operationCREATE OR REPLACE PROCEDURE sp_static (id IN NUMBER, name in VARCHAR2) IS TYPE r_curs_type is REF CURSOR; C1 r_curs_type; BEGIN OPEN C1 FOR SELECT * FROM TABLE WHERE columname = id CLOSE C1; END;
To my amazement, there is no difference in the duration of execution of these procedures when I ran the with multiple entries.CREATE PROCEDURE sp_dyanmic (id IN NUMBER, name in VARCHAR2) IS TYPE r_curs_type is REF CURSOR; C1 r_curs_type; BEGIN OPEN C1 FOR 'SELECT * FROM TABLE WHERE columnanme = :U1' USING ID; CLOSE C1; END;
Please note-
1. I have not access to trace files :-(
2. I want to just make sure there will be improved performance before starting the real work, given that some efforts provided by moving the logic of all its activities inside and the use of bind variables.
If you can suggest a strategy to ensure performance gains, it will be extremely useful...
Thanks in advance!
ConcerningHello
(1) it's true, procedure 1 also uses a variable binding
(2) you seem to be confused about the very basic concepts hereDynamic SQL is something like this:
create or replace FUNCTION f (l_table_name VARCHAR2) RETURN NUMBER IS l_result NUMBER; BEGIN EXECUTE IMMEDIATE 'SELECT COUNT(*) FROM ' || l_table_name INTO l_result; return l_result; END;
In this example, since you do not know the name of the table to the execution table, everything you can
is dynamic SQL. As you can see, neither of your two procedures is like that.Dynamic SQL is used when you do not know what columns you want to select, or who
etc. to the execution table. You can also use dynamic SQL statements to force analysis. You can not use
to avoid parsing, binding or not binding.Best regards
Nikolai -
The use of bind variables with XMLTABLE?
I tried to use bind variables with xmltable statement. Here, my unit test:
create or replace function wsdltest return xmltype as
l_dummy xmltype.
CLOB l_stt;
L_Name varchar2 (500);
CLOB l_xml;
BEGIN
l_xml: =.
' < name of definitions = 'F1' targetNamespace = "http://xmlns.oracle.com/orawsv/XFILES/F1" xmlns = "http://schemas.xmlsoap.org/wsdl/" xmlns:tns = "http://xmlns.oracle.com/orawsv/XFILES/F1" container = "http://www.w3.org/2001/XMLSchema" xmlns:soap = "http://schemas.xmlsoap.org/wsdl/soap/" >
< types >
< xsd: Schema targetNamespace = "http://xmlns.oracle.com/orawsv/XFILES/F1" elementFormDefault = "qualified" >
< xsd: element name = "SVARCHAR2-F1Input" >
< xsd: complexType >
< xsd: SEQUENCE >
< xsd: element name = "A VARCHAR2 IN" type = "xsd: String" / >
< / xsd: SEQUENCE >
< / xsd: complexType >
< / xsd: element >
< xsd: element name = "F1Output" >
< xsd: complexType >
< xsd: SEQUENCE >
< xsd: element name = "RETURN" type = "xsd: String" / >
< / xsd: SEQUENCE >
< / xsd: complexType >
< / xsd: element >
< / xsd: Schema >
< / types >
< name of message = "F1InputMessage" >
< name of part = "parameters" element = "tns:SVARCHAR2 - F1Input" / >
< / message >
< name of message = "F1OutputMessage" >
< name of part = "parameters" element = "tns:F1Output" / >
< / message >
< portType name = "F1PortType" >
< operation name = "F1" >
< input message = "tns:F1InputMessage" / >
< output message = "tns:F1OutputMessage" / >
< / operation >
< / portType >
< connection name = "F1Binding" type = "tns:F1PortType" >
< style: binding soap = transport "document" = "http://schemas.xmlsoap.org/soap/http" / >
< operation name = "F1" >
< soap: operation soapAction = "F1" / >
< input >
< soap body parts: = 'settings' use = "literal" / >
< / Entry >
< output >
< soap body parts: = 'settings' use = "literal" / >
< / output >
< / operation >
< / binding >
< service name = "F1Service" >
< documentation > Oracle Web Service < / documentation >
< name of port = "F1Port" binding = "tns:F1Binding" >
< soap: address location = "http://localhost: 8080/orawsv/XFILES/F1" / >
< / port >
< / service >
< / definitions > ';
-OK
l_stt: = ' select * from xmltable (XMLNAMESPACES ("http://www.w3.org/2001/XMLSchema" AS "XSD", default "http://schemas.xmlsoap.org/wsdl/"),)
[' / / definitions/types/XSD:schema/XSD:element[@name="SVARCHAR2-F1Input ']"
by the way xmltype(:1)
columns
path of xmltype AB ".") you;
--
EXECUTE IMMEDIATE l_stt INTO l_dummy using l_xml;
-ERROR ORA-01006
L_Name: = ' 'SVARCHAR2-F1Input "";
l_stt: = ' select * from xmltable (XMLNAMESPACES ("http://www.w3.org/2001/XMLSchema" AS "XSD", default "http://schemas.xmlsoap.org/wsdl/"),)
[/ / definitions/types/XSD:schema/XSD:element[@name=:2] "
by the way xmltype(:1)
columns
path of xmltype AB ".") you;
--
EXECUTE IMMEDIATE l_stt INTO l_dummy using l_xml, l_name;
Return l_dummy;
END;
Any idea?
Thanks in advance
CyrylYou can try something like
declare l_dummy xmltype; l_stt clob; l_name varchar2(500); l_xml clob; BEGIN l_xml := '
Oracle Web Service -
The use of bind variables in dynamic query created for Ref Cursor
Hello
I'm in a situation where there is a Ref cursor to which the query is built execution based on a loop. This is why the number of links would be known until the program runs.
The application is currently using literals instead of bind variables.
code snippet of the above is
strSql: = "select * from emp where 1 = 1 and ().
loop cursor1
If cond is true then
strSql = strSql | "ename = ' |" Cursor1.ColumnName;
end loop;
Open cursor2 for strSql;
How to use links in the example above.sb92075 wrote:
user13019948 wrote:
HelloHere is the code I have my trying to change literal-based link to the base.
What do you mean by "based bind?
who, what, how determines the values to be 'bound '?
He's referring to the coding style. He is currently using concatenated literal, and the goal is to change it to use the bindings.
If I understand this it is known as method 4 dynamic SQL and requires DBMS_SQL. There are examples autour but they vary according to the type of statement being generated - SELECT statements require column lists to be parsed, unlike the INSERT/UPDATE/DELETE.
This came up recently on my current project and I hit a demo. Here a table of names and values accepted procedure and had to build these in a single WHERE clause along the lines of
AND t_names(i) = t_values(i)
for an undetermined number of elements in the array. For this demonstration, I used a table that we called "attribute" (don't ask) which has columns including 'attribute_id' and 'name', and I need to build a query along the lines of
select description from attribute where attribute_id = :b1 and name = :b2
by the way '1012' and 'ISIN' respectively. (I use a table better and after a CREATE statement for her but I have to rush right now, sorry).
declare k_sql_base constant varchar2(500) := 'select description from attribute'; t_names constant varchar2_t := varchar2_t('attribute_id', 'name'); t_values constant varchar2_t := varchar2_t('1012', 'ISIN'); l_sql varchar2(500) := k_sql_base; l_rows_fetched integer := 0; l_value varchar2(4000); l_cursor_handle integer; begin -- Construct the SQL statement with column names and bind variables e.g. -- 'select description from mars.attribute where attribute_id = :b1 and name = :b2' for i in t_names.first .. t_names.last loop l_sql := l_sql || case i when t_names.first then ' where ' else ' and ' end || t_names(i) || ' = :b' || i; end loop; dbms_output.put_line('SQL statment = ' || l_sql); -- Parse the statement we built above (the remaining steps require a parsed cursor): l_cursor_handle := dbms_sql.open_cursor; dbms_sql.parse(l_cursor_handle, l_sql, dbms_sql.native); -- Associate the 1st column of output with variable l_value - required for SELECT statements: -- (actually the 3rd param here 'column' seems to be only used to get a datatype, in this case we want a string - -- dbms_sql.column_value actually extracts the value into a specified variable, which can be different. -- All examples in the documentation pass a local variable without further comment, so not entirely clear what this does other than set the output datatype.) dbms_sql.define_column(l_cursor_handle, 1, l_value, 4000); -- Now go through values array binding actual values to :bn variables in the cursor (similar to USING clause of EXECUTE IMMEDIATE) for i in t_values.first .. t_values.last loop dbms_sql.bind_variable(l_cursor_handle, ':b'||i, t_values(i)); dbms_output.put_line('Bound :b'||i || ' as ' || t_values(i)); end loop; -- Open the cursor and fetch the result (no loop here because we are expecting a single-row result): l_rows_fetched := dbms_sql.execute_and_fetch(l_cursor_handle); -- 'Returns value of the cursor element for a given position in a cursor' -- Copy the value of column 1 to variable l_value (has to match -- dbms_sql.column_value(l_cursor_handle, 1, l_value); dbms_sql.column_value(l_cursor_handle, 1, l_value); dbms_output.put_line('Result = ''' || l_value || ''''); dbms_sql.close_cursor(l_cursor_handle); end;
Hope that helps...
-
The use of bind variables in the application process
Bind variables refer to elements of session state supported in queries in an application request process?
All the examples I see show how to pass client side values using the $v () function and the htmldb_get object addParam method and refer to them using the wwv_flow.g_xNN variables, but already available in session state values, I should be able to use them directly in the process, right?
Thank youYes, they are supported.
Denes Kubicek
-------------------------------------------------------------------
http://deneskubicek.blogspot.com/
http://www.Opal-consulting.de/training
http://Apex.Oracle.com/pls/OTN/f?p=31517:1
http://www.Amazon.de/Oracle-Apex-XE-Praxis/DP/3826655494
------------------------------------------------------------------- -
The use of bind variables in the HTML source region
< nl >
workspace http://apex.Oracle.com/pls/apex/f?p=49417:1: SVK demo/demo
I have problems integrating an element variable in an embedded HTML string that should popup a video mode when the button is pressed.
Element 1 works very well and it's what I want to accomplish with point 2, but when I pass: p1_url, it does not work even if the content of the equivalent of two text boxes.svk1965 wrote:
Hi fac586:You who modifies the code to use * $('_#foo').html ($v ('P1_SAVED_ITEM2')); * ?? If so, I think that solved my problem here. I tested it and it seems to work everytime now. Thank you
Yes. It was very late and I could not spend more time on a detailed explanation. foo is the static region ID, I assigned to the masked area of the HTML to use as a jQuery selector.
-
The use of bind variable in this case
Hello
I want to use the variable binding": x' implicit cursor like this:
I'm in
("select object_name
to all_object where object_id =: x' using the I)
loop
...
end loop;
While the engine has no need to analyze each time training?
Kind regards
IgorIgorKSCon wrote:
I have two tables that's BILL is another LINE that is connected. One Bill have more lines.My files are like this
BILL LINE CASH CARD OTHER
18___3___0___10___12
18___4___30___0___0
18___5___0___0___0In the loop or something I would insert records in another table like this
BILL LINE CASH CARD OTHER
18___3___0___10___0
18___3___0___0___12
18___4___30___0___0
18___5___0___0___0What is the best solution for this scenario?
I am assuming that you want to insert a record in the other table for each no zero value of one of the card, the species, the other columns. If all three are 0, then insert a line anyway. Note the code below does not account for the possibility of NULL values in the data, you may need to adjust if it's a possibility.
SQL> SELECT * FROM bill1; BILL LINE CARD CASH OTHER ---------- ---------- ---------- ---------- ---------- 18 3 0 10 12 18 4 30 0 0 18 5 0 0 0 SQL> INSERT ALL 2 WHEN card != 0 THEN 3 INTO bill2 VALUES (bill, line, card, 0, 0) 4 WHEN cash != 0 THEN 5 INTO bill2 VALUES (bill, line, 0, cash, 0) 6 WHEN other != 0 THEN 7 INTO bill2 VALUES (bill, line, 0, 0, other) 8 WHEN other = 0 and card = 0 and cash = 0 THEN 9 INTO bill2 VALUES (bill, line, card, cash, other) 10 SELECT bill, line, card, cash, other 11 FROM bill1; 4 rows created. SQL> SELECT * FROM bill2; BILL LINE CARD CASH OTHER ---------- ---------- ---------- ---------- ---------- 18 4 30 0 0 18 3 0 10 0 18 3 0 0 12 18 5 0 0 0
John
-
I want to execute a dynamic SQL code something like this:
immediate execution
' insert into tgt
Select DECODE(:1,'A','A','B'), SUBSTR(:1,1,1)
the CBC
where code =: 1
and id =: 2' using p1, p2;
I want to use p1 in several places. My restriction is that I can not change using the clause. He always "will operate p1, p2 ' because it's generic program.
How can I do without change using my article.
the error it gives is ORA-01008: not all variables ORA-06512Hello
Something like this maybe:
execute immediate 'insert into tgt WITH data_tab AS ( SELECT :1 col1, :2 col2 FROM DUAL) select DECODE(data_tab.col1,'A','A','B') , SUBSTR(data_tab.col1,1,1) from src CROSS JOIN data_tab where code = data_tab.col1 and id = data_tab.col2' using p1, p2;
Not tested
-
Question about the use of constant variables in forms
As I am still very new to forms, please forgive my ignorance, if the answer to my question is very simple. I'm trying to understand how to use constant variables in my application Forms. For example, if I want the return code of constants for the configuration application (not, mind these are examples):
RC_SUCCESS CONSTANT PLS_INTEGER: = 1;
RC_FAILURE CONSTANT PLS_INTEGER: = 0;
RC_YEAR_DATA_NOT_FOUND: = 50;
Then in a module I created, if I wanted to check the return code against one of the constants I would do:
DECLARE
RC PLS_INTEGER;
BEGIN
GET_YEAR_DATA ('2000 ', rc);
IF rc = RC_YEAR_DATA_NOT_FOUND THEN
-Make an error handling
END IF;
END;
I know that you can declare constants within the individual procedures or packages, but I can't find information on how to do it in the world for the application. I read on the use of global variables in forms, but only the CHAR data type and the fact that the value can be modified doesn't really fit for this purpose have I missed something? Is there a config file or something for the Web server that can be used to configure these?
Any help would be appreciated.
Thank youTo declare constants create a Package specification in a pll and deifne all your vitals, something like:
PACKAGE PK_CONSTANTS IS RC_SUCCESS CONSTANT PLS_INTEGER := 1; RC_FAILURE CONSTANT PLS_INTEGER := 0; RC_YEAR_DATA_NOT_FOUND := 50; END;
Then attach this pll for all your forms-modules and use it as
IF rc = PK_CONSTANTS.RC_YEAR_DATA_NOT_FOUND THEN .. END IF;
A word on exceptions or errors: to me more clear (and in the case of exceptions even better) to throw an exception only to hide behind return-codes.
-
Use of bind variables in the repository
All,
Our client has a requirement that they need the sales this year of sales vs. last year passing year as a guest.
I can't use BI built in function to get early start or this year last year because our calendar dates are not standard. So I have to this day of dim query table to see e.g. for whats 2010 is the start date and so on...
To do this, I created a variable repository to hold these values using SQL. I want to know is can I use a binding to this SQL variable so that when the user passes year I can feed that year in this SQL and it will provide me with correct start and the end date for this year and last year.
Thanks in advanceYes. We can pass a value to what the user selects the guest. To do this you have create a session variable with "enable any user to set the value". Prompt properties for the year column choose "Ask the Variable" for whole Variable. And the use of content filtering year = ' valueof (NQ_SESSION.myVariable).
Let me know if it worked.
Kind regards
Jay -
Use of Bind Variable in LOV Jdev 11.1.2
Hello:
I have a lookup_codes table that has values for different columns used in multiple LOV. I created the object based on reading view single SQL statement for the table with a variable binding. Based on the column where I want to set the LOV, I would like to change the variable binding, so the LOV shows that codes of research for this particular column. Essentially, I want to change the binding variable during execution. How can I accomplish this?
Thanks for the help.
Published by: 862658 on July 28, 2011 10:51Hello
You can use the VO API and call vo.setWhereClauseParams ();
http://download.Oracle.com/docs/CD/E12839_01/apirefs.1111/e10653/Oracle/JBO/Server/ViewObjectImpl.htmlKind regards
Brano -
Link to ' effective use of bind variables, cursor_sharing and related ' KO
If I try to access to "the effective use of the bind variables, cursor_sharing and parameters related cursor" doc with link http://www.oracle.com/technetwork/database/features/performance/whitepapers-098560.html http://www.oracle.com/technology/deploy/performance/pdf/cursor.pdf I get
>
We're sorry, the page you requested was not found.
We recorded this error (404) to help us solve the problem.
You can try again using one of the tools below.
Back to previous Page
Site map
Index of products
To find your page, try our search function.
Refine your searchThis PDF file does not exist if the link has been deleted.
-
Getting the error for bind variables
Hello Experts;
SQL > DECLARE
2 l_eid number;
3 l_name varchar2 (30);
4 BEGIN
l_eid 5: = 1000;
6 select name from l_name tab1 where it = l_eid;
7 l_eid: = 9999;
8 select name from l_name tab1 where it = l_eid;
9 l_eid: = 299999.
10 select name from l_name tab1 where it = l_eid;
11 * END;
/
PL/SQL procedure successfully completed.
When you create variable bind I get the error message:
SQL > variable number b1;
SQL > exec: b1: ='select name from which no = tab1: b1';
START: b1: ='select name from which no = tab1: b1'; END;
* ERROR at line 1:
ORA-06502: PL/SQL: digital or value error: character of number conversion error
ORA-06512: at line 1
If you want to use variable bind you:
number variable b1
VARCHAR2 (30) variable b2
exec: b1: = 1000;
Start
Select name
in: b2
of tab1
where = none: b1;
end;
/
print: b2
For example:
SQL > variable b1 number
SQL > b2 variable varchar2 (30)
SQL > exec: b1: = 7839;PL/SQL procedure successfully completed.
SQL > start
2. Select ename
3 in: b2
4 of PEM
5 where empno =: b1;
6 end;
7.PL/SQL procedure successfully completed.
SQL > print: b2
B2
--------------------------------
KINGSQL >
SY.
-
How to find the query select bind variables
How to extract the variable name to bind to AS suite of sql queries
' SELECT NAME IN: NAME OF THE DOUBLE '
or
"SELECT * FROM WHERE DOUBLE (: NAME = NAME)".
or
"SELECT * FROM WHERE DOUBLE: NAME IS NULL'."
or
"SELECT * FROM WHERE DOUBLE: NAME | NAME = LAST_NAME'
or
' SELECT * FROM DUAL WHERE: a = 10'
or
"SELECT * from where DOUBLE: A > = 10'.
or
"SELECT * from where DOUBLE: A < = 10 '.
Give a solution using SUBSTR, REGEXP_SUBSTR, please answer quickly
best answer will be marked correct/good...Search: and then search for the following word breaker then have the substr for them: and the separator.
You can build an own function to find the same. -
What is the correct syntax for the use of a variable in an ad-hoc query?
Hi all
I'm a casual user of the DB and right now need to update the records about 1000 + so that a certain column gets a unique value.
So I thought I'd use a variable for this.
Then, I built this type of SQL statement for only a small subset of records:
----------
variable number recNumber;
exec: recNumber: = 1;
UPDATE TABLE_TO_BE_UPD
SET COL_TO_BE_UPD = COL_TO_BE_UPD + recNumber
WHERE COL_TO_BE_UPD IN ('VAL_A', 'VAL_B');
----------
I get invalid SQL statement error when you try to run above (except for the guest who asks for a value I want to omit).
In any case, I also tried this one:
----------
CREATE SEQUENCE seqCounter;
UPDATE TABLE_TO_BE_UPD
SET COL_TO_BE_UPD = COL_TO_BE_UPD + seqCounter.NEXTVAL
WHERE COL_TO_BE_UPD IN ('VAL_A', 'VAL_B');
----------
Of it, I got the error ORA-01722: invalid number... I guess it's because seqCounter is of type number and the COL_TO_BE_UPD is of type character... (?)
Then I want to ask is what is the correct way to define and use a counter variable type to add a number at the end of a string?
Also another question I would ask is that are variables that are used in queries ad hoc, also called "bind variables"?
Thank you muchlyIf you want to add a unique number to a column, then it would be:
UPDATE TABLE_TO_BE_UPD SET COL_TO_BE_UPD = COL_TO_BE_UPD ||to_char(rownum) WHERE COL_TO_BE_UPD IN ('VAL_A','VAL_B');
Maybe you are looking for
-
How can I make my default Ethernet instead of wifi only iMac
My default wifi network preferences. How do for the ethernet connection either by default and no Wifi
-
He is about to be a death in the family. I need a list of all members of the immediate family, but must not contain * EACH * entry in my personal address book, or my collected addresses. Is there something like an installation drag - move or copy - p
-
Best use of a memory of 8 GB of iphone 4S
How can I reduce the 169 request for documents and data of MD for the application of PodCast to iPod? It is without him producing anything?
-
Question micro SDHC card?
My wife fills its Fuze 4 GB and I think to have him pick up a 8 GB micro SDHC for her... I just want to know how much the card sticks on the player. Dose anyone have a photo of the player with the card inside? I want to just make sure that she can us
-
Get messages "Malicious URL blocked" avast all few seconds
I have Vista Home Premium, 32-bit, Service Pack 2. Since last week, I get the message from Avast: 'Malicious blocked URL' popping up every few seconds. 2 Commons are "hxxp://oldschoolzz.ocm/x/" and "hxxp://newgenerationp.com/x/". I ran all the sc