Define variable in Oracle SQL developer - oracle

I'm trying to define standard values in variables in ORACLE SQL Developer, but it keeps asking me to enter a value. How can I avoid that and put as default value for v_mode ='X1','X2' and set COB_DATE to 14 July so that there is NO popup?
variable COB_DATE date
variable v_mode varchar(20);
exec :COB_DATE := '14-JUL-2016';
exec :v_mode := 'MAG';
select *
FROM DF_RISK_SIT2_OWNER.recon_ts_rs
WHERE SRC_HUB = 'DBRS'
AND TRD_SRC_SYS in :v_mode
AND DSET_COB_DT = :COB_DATE
but I get the error: Bind Variable "COB_DATE" is NOT DECLARED

SQL> help var
VARIABLE
--------
Declares a bind variable that can be referenced in PL/SQL, or
lists the current display characteristics for a single variable
or all variables.
VAR[IABLE] [variable [type]]
where type represents one of the following:
NUMBER CHAR CHAR (n [CHAR|BYTE])
NCHAR NCHAR (n) VARCHAR2 (n [CHAR|BYTE])
NVARCHAR2 (n) CLOB NCLOB
REFCURSOR BINARY_FLOAT BINARY_DOUBLE
As you can see there is no DATE type here. I guess the whole
variable COB_DATE date
is ignored.
As a workaround you can define COB_DATE as varchar2 and convert it to DATE in the sql
variable COB_DATE varchar2(30)
variable v_mode varchar2(20)
exec :COB_DATE := '14-JUL-2016';
exec :v_mode := 'MAG';
select *
FROM DF_RISK_SIT2_OWNER.recon_ts_rs
WHERE SRC_HUB = 'DBRS'
AND TRD_SRC_SYS in :v_mode
AND DSET_COB_DT = TO_DATE(:COB_DATE, 'DD-MON-YYYY')
or rely on implicit conversion using your original query

You have to use "Run Script (F5)" not "Run Statement (Control+Enter") - I have circled the toolbar icon in red:

for Oracle SQL Developer:
define defVar= 'AA%'
Select...
where somefield like '&&defVar';

Related

oracle stored procedure using date parameter

Is it possible to use a date parameter for a stored procedure?
for example, date 20171201 I need to execute a case A in a dateparameter.prc
and date 20171202 execute a case B in a dateparameter.prc which is the same procedure above.
I am googling and investigating some books but I still haven't found a solution.
Can anyone know about it?
Thanks
Yes, it is possible.
SQL> set serveroutput on
SQL> create procedure dt_demo(p_d date) as
2 begin
3 dbms_output.put_line('p_d = ' || p_d);
4 end;
5 /
Procedure created
SQL> exec dt_demo(date '2017-12-02');
p_d = 02.12.17
PL/SQL procedure successfully completed
Is it possible to use a date parameter for a stored procedure?
Yes. A simple example that takes a date as an IN date parameter and passes it directly to an OUT date parameter is:
CREATE PROCEDURE your_procedure(
in_value IN DATE,
out_return OUT DATE
)
IS
BEGIN
out_return := in_value;
END;
/
Your procedure is functioning correctly. But
The expression "DATE '2017-12-02'" represents the ISO date standard.
The expression "dbms_output.put_line('p_d = ' || p_d)" represents the regular Oracle date processing, which precedes the ISO specification.
How the date is formatted (displayed) during dbms_ouput converts the date to a string. Since in this case there is an implicit conversion the resulting format is controlled by the NLS_DATA_FORMAT setting. It looks like yours is set to "dd-mm-yy". To see the difference insert/run the following before your exec statement:
alter session set nls_date_format = 'yyyy-mm-dd" ;
Also see Oracle Date Format for Oracle 11g or as appropriate for your version.

How to set Oracle bind variables when using SQLPlus?

How do I set Oracle bind variables when using SQLPlus?
Example:
SELECT orders.order_no FROM orders WHERE orders.order_date BETWEEN :v1 AND :v2
How do I set the dates of :v1 and :v2?
Notice the following:
VARIABLE is a SQLPlus command. You don't end it with a semicolon (;).
In the VARIABLE command, you do not precede the variable name with
colon (:).
Bind variable can't be of data type "date" - they are some sort of
character value.
For that reason, IN YOUR CODE you must use to_date() with the
proper format model, or some other mechanism, to convert string to
date. That is currently missing in your code. NEVER compare dates to
strings!
Brief demo below.
SQL> variable v1 varchar2(20)
SQL> exec :v1 := '2015/12/22';
PL/SQL procedure successfully completed.
SQL> select 1 as result from dual where to_date(:v1, 'yyyy/mm/dd') < sysdate;
RESULT
----------
1
In common
you may use define and use variable with &
define x = 12 ;
select &x from dual;
Or varable
variable x refcursor;
begin
open :x for select * from dual connect by level < 11;
end;
/
print x

How to obtain the hexa code of a NCHAR into a VARCHAR2

I'm working on a conversion of T-SQL script into pl/sql. And i need your help about a conversion type.
My t-sql script :
set #cust_name_hex = convert(VARCHAR(max),convert(varbinary(max), #cust_name),2)
My conversion, but i'm not really sure...
set cust_name_hex = TO_CHAR(cust_name);
I've to obtain the hexa code of the 'cust_name' variable. I search on the web and every where, and i found the WARTOHEX function.
I missed tu say you that the variable cust_name is a NCHAR. So i understand in t-sql, the schema of conversion : NVARCHAR -> VARBINARY -> VARCHAR.
In PL/SQL, i try to make the same conversion, but i don't obtain the good result.. I don't know how to convert a NCHAR in VARCHAR2, to give me the Hexa value...
Combination of UTL_RAW.CAST_TO_RAW and RAWTOHEX functions should do the job:
SELECT RAWTOHEX(UTL_RAW.CAST_TO_RAW(N'unicode text')) FROM DUAL;
or using PL/SQL
DECLARE
cust_name_hex VARCHAR2(255);
BEGIN
cust_name_hex := RAWTOHEX(UTL_RAW.CAST_TO_RAW(N'unicode text'));
DBMS_OUTPUT.PUT_LINE(cust_name_hex);
END;

SQL*Plus Input: How do you get input from user into varchar2 variable?

I got a table with strings from varchar2(50) and I want to find all tuples with a name which equals to a given name.
My input part of code is
name:=&k;
(k is not declared)
but when I give a string like "Jhon" it actually uses it as value and not a string (Jhon and not 'Jhon')
so my question is how to perform a string(varchar2) input?
Assuming you're running an anonymous PL/SQL block from SQL*Plus (or SQL Developer), you just need to enclose the substitution variable in quotes:
name := '&k';
For example:
set verify off
set serveroutput on
declare
name varchar2(10);
begin
name := '&k';
dbms_output.put_line('Name is: ' || name);
end;
/
anonymous block completed
Name is: jhon

ORA-12714: invalid national character set specified

I got a problem with oracle database ,i created a stored procedure and i wanted to pass an array of items' ids to this procedure to select the data according to array of items using "in" clause,the available solution to this as i found was to create a function and pass a string value with all item's ids seperated by a comma ,and this function will return a datatble with a row for each item id.this approach works fine when i try it in toad in a select statement,,but when i use it in the stored procedure i get a strange error
"ORA-12714: invalid national character set specified"
after searching about the reason of that error i found that it is a bug in that version of oracle according to this page and it was fixed in a 10.2.0.4 oracle patch and the exact reason is to declare a cursor for the function that returns a data table
As it is impossible to me to let the users who work on a live production environment to stop the servers to apply the update patch ,I was wondering if any Oracle expert can help me to declare a cursor and return that cursor instead of returning the table.
my Oracle function,Thanks in Advance
create or replace
FUNCTION SplitIDs(
v_List IN VARCHAR2)
RETURN RtnValue_Set PIPELINED
AS
SWV_List VARCHAR2(2000);
v_RtnValue Dt_RtnValue := Dt_RtnValue(NULL);
BEGIN
SWV_List := v_List;
WHILE (instr(SWV_List,',') > 0)
LOOP
FOR RetRow IN
(SELECT ltrim(rtrim(SUBSTR(SWV_List,1,instr(SWV_List,',') -1))) SelectedValue
FROM dual
)
LOOP
v_RtnValue.SelectedValue := RetRow.SelectedValue;
PIPE ROW(v_RtnValue);
END LOOP;
SWV_List := SUBSTR(SWV_List,instr(SWV_List,',')+LENGTH(','),LENGTH(SWV_List));
END LOOP;
FOR RetRow IN
(SELECT ltrim(rtrim(SWV_List)) SelectedValue FROM dual
)
LOOP
v_RtnValue.SelectedValue := RetRow.SelectedValue;
PIPE ROW(v_RtnValue);
END LOOP;
RETURN;
END;
Oracle says this about the error:
Error: ORA-12714 (ORA-12714)
Text: invalid national character set specified
Cause: Only UTF8 and AL16UTF16 are allowed to be used as the national character set
Action: Ensure that the specified national character set is valid
Check your NLS_NCHAR_CHARACTERSET which is set using:
select value from NLS_DATABASE_PARAMETERS where parameter = 'NLS_NCHAR_CHARACTERSET';
Try using NCHAR, NVARCHAR2 or NCLOB

Resources