This question already has answers here:
Declaring & Setting Variables in a Select Statement
(5 answers)
Declare bind variables in SQL*Plus
(2 answers)
Closed 3 years ago.
I started a new job and needing to learn Oracle for the job. I am trying to convert this simple code so I can build more complex queries down the road
DECLARE #NPI = VARCHAR(20)
SET #NPI = '123456789'
SELECT *
FROM AFFILIATION
WHERE NPI = #NPI
I am trying to figure out to set parameters in Oracle and then use them in the WHERE statement or other places within the code.
I think I have figured out part of my question but not sure of the full conversion
DECLARE NPI1 varchar(20):= '123456789'
I am looking to set verables/parameters and use them later in the code. YES I dont need it in this query but If i know how to use it for the query I can build much more complex Oracle queries.
Use a bind variable:
VARIABLE npi VARCHAR2(20);
Then use PL/SQL to set its value:
BEGIN
:npi = '1234567890';
END;
/
or EXEC:
EXEC :npi = '1234567890';
Then you can use it in your query:
SELECT *
FROM AFFILIATION
WHERE NPI = :npi
Related
This question already has answers here:
NLS_NUMERIC_CHARACTERS setting for decimal
(4 answers)
Closed 9 months ago.
I have the number 123.45, but in portuguese, which is actually the locale that Oracle is currently using in our environment, this number would be 123,45, so when I try to run
SELECT TO_NUMBER('123.45') FROM DUAL;
I get ORA-06512 error.
Replace dot for comma is not an option because besides seeming a hacky solution, the oracle driver considers my java application locale and pass it on to the Oracle database session.
Is there anyway to consider both dots and commas as decimal separator in this function?
Use the D format model, passed as the second argument of TO_NUMBER, to represent the sessions current decimal character:
Which, if you were in America/England/etc. then you can use:
SELECT TO_NUMBER('123.45', '999D99') FROM DUAL;
And in Portugal/Germany/etc.
SELECT TO_NUMBER('123,45', '999D99') FROM DUAL;
If you want to always use . then explicitly use . in the format model:
SELECT TO_NUMBER('123.45', '999.99') FROM DUAL;
Or specify the NLS_NUMERIC_CHARACTERS setting that you want to use as the third argument to the TO_NUMBER function:
SELECT TO_NUMBER('123.45', '999D99', 'NLS_NUMERIC_CHARACTERS=''.,''') AS value FROM DUAL;
db<>fiddle here
You can try REFEXP, REPLACE Function as per your version of Database. There concept is repalce . with , and hope this will help.
https://docs.oracle.com/cd/B19306_01/server.102/b14200/functions130.htm
https://www.techonthenet.com/oracle/functions/regexp_replace.php
This question already has answers here:
PL/SQL, how to escape single quote in a string?
(5 answers)
Closed 1 year ago.
I have the below regular expression which works perfectly fine using sql, but the same cannot be included in dynamic sql qyery, have tried escaping all the character but it still throws error.
here is what I have tried:
reg := 'select regexp_replace(data, ||'\.||(docx|pdf|msg)|| ', ||'.\1, ') from table where id=1'
Can you help to include this within dynamic sql?
select regexp_replace(data, '\.(docx|pdf|msg) ', '.\1, ') from table where id=1;
It looks that single quotes bother you.
If so, use the q-quoting mechanism, such as:
SQL> declare
2 reg varchar2(500);
3 begin
4 reg := q'[select regexp_replace(data, ||'\.||(docx|pdf|msg)|| ', ||'.\1, ') from table where id=1]';
5 end;
6 /
PL/SQL procedure successfully completed.
SQL>
However, we use dynamic SQL when there's something dynamic. There's nothing dynamic in your query, so - why do you use it? Are you sure you need it? What is the "real" problem you're trying to solve? Maybe it doesn't require dynamic SQL at all.
This question already has answers here:
ORACLE PL/Scope
(2 answers)
Closed 3 years ago.
The variables that we declare at the package level, which can be used within the package, where are these stored when the package is being executed.
Is their any table where we can check?
Checked a few websites including the oracle documentation, but could not find a precise answer to my question. Please advice.
Yes you can obtain it from *_IDENTIFIERS data dictionary view.
first set the PL/SQL compiler to analyze the identifiers of your program when it is compiled. See PL/Scope
ALTER SESSION SET
plscope_settings='IDENTIFIERS:ALL'
/
When PL/Scope is enabled and your program unit is compiled, the ALL_IDENTIFIERS view is populated with information about all the identifiers found in that unit.
If I want to see all the declared variables in a program unit, I can execute this query:
SELECT ai.object_name
, ai.object_type
, ai.name variable_name
, ai.name context_name
FROM all_identifiers ai
WHERE ai.owner = USER AND
ai.TYPE = 'VARIABLE' AND
ai.usage = 'DECLARATION'
ORDER BY ai.object_name,
ai.object_type, ai.usage_id
This question already has answers here:
Auto-increment in Oracle without using a trigger
(9 answers)
Closed 8 years ago.
I want the first column of a table in PLSQL to be auto-increment.That column will be the primary key for that table. I heard something called serialize but i didn't get proper description about that. i was working in SQL Server. I am new to Oracle(PLSQL). Please help me to find out a proper solution.
Create a sequence
CREATE SEQUENCE name_of_sequence
START WITH 1
INCREMENT BY 1
CACHE 100;
Create a trigger
CREATE OR REPLACE TRIGGER trigger_name
BEFORE INSERT ON table_name
FOR EACH ROW
BEGIN
SELECT name_of_sequence.nextval
INTO :new.name_of_primary_key_column
FROM dual;
END;
The syntax of the trigger gets a little simpler in 11g since you can do a direct assignment to the :new.name_of_primary_key_column rather than selecting from dual. And I understand that there is some additional syntactic sugar in 12c that makes this even easier though I haven't played around with that.
This question already has answers here:
Getting a list of functions and procedure signature from oracle
(2 answers)
Closed 9 years ago.
How to go about retrieving a list of the parameters defined in the supplied stored procedure. The database is Oracle 11g.
Here's the sql statement you should use:
select argument_name, in_out, data_type
from all_arguments
where owner = 'schema_name' and
package_name = 'package_name or null' and
object_name = 'sp_name'
order by position