Is int an undocumented alias for numeric? - oracle

This code runs without any errors:
DECLARE
files int(5);
BEGIN
SELECT count(*)
INTO files
FROM o.DEPART;
DBMS_OUTPUT.put_line (files);
END;
/
Is int an undocumented alias for numeric?

int is a synonym for integer - ANSI SQL data type. If you look at the declaration of the standard package, you'll see that the integer is simply a subtype of number data type declared as
subtype INTEGER is NUMBER(38,0);
Simply put, int is a constrained number data type, which allows you, as it name implies, to store integers only.
Oracle ANSI SQL data types

Related

is it possible to pass arrays as parameters in procedures?

i'm trying to pass an array as a parameter in my procedure but i keep getting a command unknown error
code
SET SERVEROUTPUT ON;
TYPE pourcentage_remise IS TABLE OF NUMBER INDEX BY commandeproduit.ref_produit%type;
CREATE OR REPLACE PROCEDURE remise_produit( pourcent IN pourcentage_remise,
ref_comm IN commande.ref_commande%type,
c_ht OUT commandeproduit.prix_ht%type,
c_ttc OUT commandeproduit.prix_ttc%type)
IS
CURSOR p_curs IS
SELECT ref_produit, prix_ttc, prix_ht FROM commandeproduit WHERE concerne = ref_comm ;
ref commandeproduit.ref_produit%type;
ttc commandeproduit.prix_ttc%type;
ht commandeproduit.prix_ht%type;
BEGIN
open p_curs;
LOOP
FETCH p_curs into ref, ttc, ht;
EXIT WHEN p_curs%notfound;
dbms_output.put_line(ref, ' ',ht, ' ', ttc);
IF pourcent(ref) THEN
ttc := ttc - ttc * pourcent(ref);
ht := ht - ttc * pourcent(ref);
INSERT INTO commandeproduit(prix_ht, prix_ttc) VALUES(ht, ttc) WHERE concerne = ref_comm AND ref_produit = ref;
END IF;
dbms_output.put_line(ref, ' ',ht, ' ', ttc);
END LOOP;
close p_curs;
END remise_produit;
/
procedure call
DECLARE
pourcentage pourcentage_remise;
reference commande.ref_commande%type :=1;
BEGIN
pourcentage('A01') :=0.15;
pourcentage('B15') :=0.2;
remise_produit(pourcentage, reference);
END;
/
the table
the error in french which means command unknown
please help
Your syntax error is on the declaration of your type so the rest of your code isn't really needed.
TYPE pourcentage_remise IS TABLE OF NUMBER INDEX BY commandeproduit.ref_produit%type;
Several problems
If you are trying to declare a type in SQL, you'd need to use a CREATE TYPE so you're missing the CREATE.
If you are trying to declare a table type in SQL, you can't use an associative array. You'd realistically want a nested table instead.
If you are trying to declare a PL/SQL type, your statement would need to be in a PL/SQL block. You could declare a package that contains an associative array type.
If you want to declare a nested table type in SQL
CREATE TYPE pourcentage_remise IS TABLE OF NUMBER;
If you want to declare an associative array in a PL/SQL package
CREATE OR REPLACE PACKAGE my_collection_pkg
AS
TYPE pourcentage_remise IS TABLE OF NUMBER INDEX BY commandeproduit.ref_produit%type;
END;
If you want to use a nested table type, that changes how you need to initialize your associative array. It should change how you reference elements of that array, but I'm confused by your code. Your procedure appears to be using a numeric index to access an element of the associative array which doesn't make sense if the associative array uses a string as the index.

What does PLS stand for in PLS_INTEGER?

There's other data types in PL/SQL like BINARY_INTEGER. What does the PLS add in PLS_INTEGER? I was told that it stands for PL/SQL, but why would it be named that?
I doubt there is any deep rationale for using "PLS" in "PLS_INTEGER". When this datatype was added, it was a more efficient implementation of the "original" BINARY_INTEGER datatype. And a new name was needed.
Both of those types and other related subtypes have a constrained set of values compared to INTEGER. You can see this in the STANDARD package, in which the types are defined:
subtype BINARY_INTEGER is INTEGER range '-2147483647'..2147483647;
subtype NATURAL is BINARY_INTEGER range 0..2147483647;
subtype NATURALN is NATURAL not null;
subtype POSITIVE is BINARY_INTEGER range 1..2147483647;
subtype POSITIVEN is POSITIVE not null;
subtype SIGNTYPE is BINARY_INTEGER range '-1'..1; -- for SIGN functions
subtype pls_integer is binary_integer;

Define variable in Oracle SQL developer

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';

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;

Are these PL/SQL variable initializations equivalent?

So I know I can initialize variables in PL/SQL using either of the following:
The DEFAULT keyword
The := assignment operator
For example:
counter binary_integer DEFAULT 15;
counter binary_integer := 15;
Are these two methods exactly equivalent to the PL/SQL engine, or are there any slight differences?
Yes they are equivalent.
From Oracle documentation
You can use the keyword DEFAULT instead of the assignment operator to
initialize variables. You can also use DEFAULT to initialize
subprogram parameters, cursor parameters, and fields in a user-defined
record.
Use DEFAULT for variables that have a typical value. Use the
assignment operator for variables (such as counters and accumulators)
that have no typical value.
Example 2-8 Assigning Default Values to Variables with DEFAULT Keyword
SQL> DECLARE
2 blood_type CHAR DEFAULT 'O'; -- Same as blood_type CHAR := 'O';
3
4 hours_worked INTEGER DEFAULT 40; -- Typical value
5 employee_count INTEGER := 0; -- No typical value
6
7 BEGIN
8 NULL;
9 END;
10 /
PL/SQL procedure successfully completed.
SQL>
According to the documentation you can use either:
To specify the initial value, use either the assignment operator (:=) or the keyword DEFAULT, followed by an expression
which means they are equivalent.

Resources