Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What characters are allowed in Oracle bind param placeholders?

Could anyone please point me to where the characters allowed for a bind variable name are listed? I've spent several hours digging through Oracle SQL docs to no avail.

I mean ":id" in the following:

SELECT * FROM mytable WHERE id = :id

E.g. can a dot be used there like ":some.id"? Will it function exactly like the version without the dot?

like image 937
Alex Jenter Avatar asked Oct 13 '11 10:10

Alex Jenter


People also ask

What is bind variable in Oracle with examples?

You reference bind variables in PL/SQL by typing a colon (:) followed immediately by the name of the variable. For example :ret_val := 1; To change this bind variable in SQL*Plus, you must enter a PL/SQL block. For example. SQL> begin 2 :ret_val:=4; 3 end; 4 / PL/SQL procedure successfully completed.

What is Oracle bind values?

Bind variables reduce parsing and execution costs when statements are executed more than once with different data values. If you do not use bind variables, Oracle must reparse and cache multiple statements. When using bind variables, Oracle Database may be able to reuse the statement execution plan and context.

What is binding parameter in SQL?

Each parameter marker in an SQL statement must be associated, or bound, to a variable in the application before the statement can be executed. This is done by calling the SQLBindParameter function. SQLBindParameter describes the program variable (address, C data type, and so on) to the driver.

Can we use bind variables in Oracle stored procedure?

REFCURSOR bind variables can also be used to reference PL/SQL cursor variables in stored procedures. This allows you to store SELECT statements in the database and reference them from SQL*Plus. A REFCURSOR bind variable can also be returned from a stored function.


1 Answers

These pages both state bind variables must be "legal Oracle identifiers" The documentation I found doesn't specifically say that a dot can be part of a legal identifer. I was able to use a dot in both a table name and as a bind variable name, but it looks like it is not recommended.

PAGES THAT HAVE BIND VARIABLE NAMING CONVENTIONS (These pages state a bind variable must be a legal identifier):

http://www.utoug.org/i/doc/concept_bind_var.htm

http://docs.oracle.com/cd/E23903_01/doc.41/e21674/concept_ses_val.htm#BEIEGCCC

PAGE THAT DESCRIBES LEGAL IDENTIFIERS: http://docs.oracle.com/cd/B19306_01/server.102/b14200/sql_elements008.htm

I could not find anything on this page that says that a dot is a legal part of an identifier (E.G. table or bind variable name) except in a DB link. Even though $ and # are legal, they are not even recommended, so "." may work but is obviously not recommended (not even mentioned as legal on this page)

Bind variable names must correspond to an item name. Bind variable names are not case-sensitive. Bind variable names cannot be longer than 30 characters (that is, they must be a valid Oracle identifier).

I know that a valid ORACLE identifer (based on ORACLE's definition of a legal identifier) cannot start with a number, and can have SOME special characters like $ and . but if there are special characters the identifier MUST be in double quotes.

I was able to get an identifier with a dot to work in a bind variable, but I had to put double quotes around the bind variable when the bind variable had a dot in it.

create or replace function F0416B
RETURN VARCHAR2
is
    V_STMT    VARCHAR2(1999);
    V_RESULT  VARCHAR2(1999);
BEGIN 
    V_STMT := 'INSERT INTO TEST0411(FIELD1, FIELD2) VALUES ( :"A.1" , :"A.2")';
    EXECUTE IMMEDIATE V_STMT USING  'AS201', 'AS202';
    RETURN 'INSERT-OK';
    COMMIT;
EXCEPTION
WHEN OTHERS THEN RETURN SQLERRM;
END;    

#This may work but according to the above documentation a period/dot in a bind variable or other object name is not legal/recommended...

#This is the sentence on the ORACLE schema object naming page that is telling me this:

Nonquoted identifiers can contain only alphanumeric characters from your database character set and the underscore (_), dollar sign ($), and pound sign (#). Database links can also contain periods (.) and "at" signs (@). Oracle strongly discourages you from using $ and # in nonquoted identifiers.

like image 101
A B Avatar answered Dec 25 '22 06:12

A B