How to define spool file with substitution variable - plsql

I'm using Oracle sql plus to execute a script PL/SQL.
Here’s my script:
set serveroutput on
spool "Svincoli_giornalieri.csv"
declare
v_id_rapporto number(20);
begin
v_id_rapporto := &id_rapporto;
select id_rapporto into v_id_rapporto from tb_ag_rapporti where id_rapporto = v_id_rapporto;
for c in (
select r.intestazione,vn.id_rapporto,vn.IMPORTO_SVINCOLABILE,vn.IMPORTO_ORIG,svn.ID_OP_VNC,svn.importo,svn.DATA_INS,svn.STATO_CSE ,svn.DATA_ESECUZIONE
from tb_op_vnc vn, TB_OP_SVN svn, tb_ag_rapporti r
where vn.ID_OP_VNC=svn.ID_OP_VNC
and r.id_rapporto =vn.id_rapporto
and svn.DATA_INS>trunc(sysdate)
)
loop
dbms_output.put_line( c.intestazione||' idr: '||c.id_rapporto||' ID_Vincolo:'||c.ID_OP_VNC||' '||c.stato_cse||' Data Esec: '||c.DATA_ESECUZIONE||' Importo orig:'||c.IMPORTO_ORIG||' Svincolo Residuo:'||c.IMPORTO_SVINCOLABILE||' Importo Svincolato:'||c.importo);
end loop;
end;
/
spool off
I want to pass the output in a spool file using the substitution variable but when I launch my script I get this error:
ERRORE alla riga 6:
ORA-06550: riga 6, colonna 26:
PLS-00103: Trovato il simbolo "OFF" anziche uno dei seguenti:
. ( * # %
& = - + ; < / > at in is mod remainder not rem
<un esponente (**)> <> o != o ~= >= <= <> and or like like2
like4 likec between || multiset member submultiset
Il simbolo "." e stato sostituito per permettere a "OFF" di continuare.
I can’t figure out how to solve this problem. Could someone help me? Thanks

To dynamically specify a spool file in SQL*Plus you migt want to use something like:
column filename new_val filename
select 'file_' || to_char(sysdate, 'yyyymmdd' ) filename from dual;
spool &filename
Unfortunately I do not understand how this relates to the (Italian) error message you also posted.
If you could show the complete output including the line numbers and an english error text this might be better to understand.
Is the error in the line with "spool off" and did you try to add a blank line before the spool off?

Related

Conversion failed when converting the nvarchar value 'B' to data type int

Getting this error when ever I tried to execute the entire procedure. Below is the piece of code from the procedure.
SELECT
DISTINCT SUBSTRING (a.GL06001,19,10) as ProjectNo,
--PR01040 UD_10,
SUBSTRING (a.GL06001,1,8) as AccountNo,
a.GL06002 as TransNo,
a.GL06004 as TransAmount,
a.GL06003 as TransDate,
a.GL06005 as TransDesc,
'GL' as SourceType,
' ' as ResourceCode,
' ' as TransLine,
0 as CostPR,
'000000' as PRTransNo,
a.GL06027 as SubprojNo,
a.GL06028 as ActiLineNo,
a.GL06012 as TransType,
a.GL06016 as Counter
from ScalaMX.dbo.GL06PA17 a where a.GL06003 between '2017-02-21 00:00:00.000' and '2017-03-01 00:00:00.000'
There are actually 18000+ rows and 15 columns. Any hint on how to track which column has B value?
I downloaded the result in Excel and ctrl+f 'B' But still no clue and I couldn't find it.
convert into stored procedure, see below sample
declare
n integer;
m integer;
begin
for i in ( select primarykeycolumn, a,b from table )
loop
begin
n := i.a;
m := i.b;
exception
when others then
dbms_output.put_line(i.primarykeycolumn);
end
end loop;
end;
When conversion error happens it will catch the exception hence find the primary key.

Why this PL/SQL doesn't work?

Im trying to learn PL/SQL. I am struck with this code. Please notify me where im going wrong. I use Oracle 10g in a command line .
declare
grade char(1):='&v_grade';
app varchar(15);
begin
app:=case v_grade
when 'a' then
dbms_output.put_line('Excellent');
when 'b' then
dbms_output.put_line('Good');
else
dbms_output.put_line('Bad');
end case;
dbms_output.put_line('Grade'||grade||' Appraisal:'||app);
end;
/
It shows
Enter value for v_grade: a
old 2: grade char(1):='&v_grade';
new 2: grade char(1):='a';
dbms_output.put_line('Excellent');
*
ERROR at line 7:
ORA-06550: line 7, column 34:
PLS-00103: Encountered the symbol ";" when expecting one of the following:
. ( * % & = - + < / > at else end in is mod remainder not rem
when <an exponent (**)> <> or != or ~= >= <= <> and or like
LIKE2_ LIKE4_ LIKEC_ between || multiset member SUBMULTISET_
The symbol ";" was ignored.
ORA-06550: line 9, column 29:
PLS-00103: Encountered the symbol ";" when expecting one of the following:
. ( * % & = - + < / > at else end in is mod remainder not rem
when <an exponent (**)> <> or != or ~= >= <= <> and or like
LIKE2_ LIKE4_ LIKEC_ between ||
ORA-06550: line 11, column 28:
PLS-00103: Encountered the symbol ";" when expecting one of the following:
. ( * % & = - + < / > at end in is mod remainder not rem
<an exponent (**)> <> or != or ~= >= <= <> and or like LIKE2_
LIKE4_ LIKEC_ between || multiset
please notify me where i am going wrong.
T.J. Crowder is sort of right, you shouldn't have a semicolon within a case statement in SQL, but this is the PL/SQL version so it's slightly different.
You are currently trying to assign the (non-existent) return from the dbms_output.put_line procedure (not function) call to your app variable.
For the assignment to work you need the case to evaluate to a string, so you can just use text literals; but the assignment needs to be in each branch:
declare
grade char(1):='&v_grade';
app varchar(15);
begin
case grade
when 'a' then app:= 'Excellent';
when 'b' then app:= 'Good';
else app := 'Bad';
end case;
dbms_output.put_line('Grade '||grade||' Appraisal: '||app);
end;
/
Which when run gets:
Enter value for v_grade: a
Grade b Appraisal: Excellent
PL/SQL procedure successfully completed.
Or use a query to do the assignment, though this is not quite as efficient:
declare
grade char(1):='&v_grade';
app varchar(15);
begin
select case grade
when 'a' then 'Excellent'
when 'b' then 'Good'
else 'Bad'
end case
into app
from dual;
dbms_output.put_line('Grade '||grade||' Appraisal: '||app);
end;
/
Enter value for v_grade: b
Grade b Appraisal: Good
PL/SQL procedure successfully completed.
Or you can do the output directly in each branch:
declare
grade char(1):='&v_grade';
begin
dbms_output.put('Grade '||grade||' Appraisal: ');
case grade
when 'a' then
dbms_output.put_line('Excellent');
when 'b' then
dbms_output.put_line('Good');
else
dbms_output.put_line('Bad');
end case;
end;
/
Enter value for v_grade: c
Grade c Appraisal: Bad
PL/SQL procedure successfully completed.
The first part of your output is before the case now.
You're basically mixing up the different approaches.
You might want to change the first line to:
grade char(1):= upper('&v_grade');
... and then make the case look for A,B,C instead of a,b,c - then it won't matter what case the input is in.
You can read more about the PL/SQL case statemnt here and here.

Simple Procedure raise ORA-06502

There's the simplified version of my code who keep raise me ORA-06502:
declare
p_filter varchar2(300) := '2012';
p_value varchar2(300) := '12345.000';
w_new_value number(13,3) := null ;
w_count number(4) := null ;
BEGIN
SELECT count(*)
INTO w_count
FROM dual
where p_filter = p_filter;
--- more filters
if w_count != 0 then
w_new_value := p_value / w_count;
else
w_new_value := p_value;
end if;
-- do something
end;
/
Someone can give me a help?
DataBase Details
nls_language = italian
nls_territory = italy
nls_currency = �
nls_iso_currency = italy
nls_numeric_characters = ,.
nls_calendar = gregorian
nls_date_format = dd-mon-rr
nls_date_language = italian
nls_characterset = we8iso8859p15
nls_sort = west_european
nls_time_format = hh24:mi:ssxff
nls_timestamp_format = dd-mon-rr hh24:mi:ssxff
nls_time_tz_format = hh24:mi:ssxff tzr
nls_timestamp_tz_format = dd-mon-rr hh24:mi:ssxff tzr
nls_dual_currency = �
nls_nchar_characterset = al16utf16
nls_comp = binary
nls_length_semantics = byte
nls_nchar_conv_excp = false
First, this is always going return a value of 1.
SELECT count(*)
INTO w_count
FROM dual
It doesn't matter what the qualifier is.
Lastly, I just ran your simplified code example in Oracle 11R2 and it didn't throw an exception.
I added the following statement in place of your "do something" comment:
dbms_output.put_line('w_new_value: ' || w_new_value || '. w_count: ' || w_count);
The result was:
w_new_value: 12345. w_count: 1
So, I think you've simplified your example into oblivion. You need to provide something that actually shows the error.
Good luck.
I found myself the ansewer and i think is useful for other know.
The real problem of the script for my DB is the language.
The italian "version" of Oracle accept , instead of the . for translate the VARCHAR2 into NUMBER unlike the most of other country.
For make the code running well the solution is
w_new_value := replace(p_value,'.',',') / w_count;
This trick finally allows the DB use my VARCHAR2 param like a NUMBER

PLS-00103: Encountered the symbol "&" when expecting one of the following: ( - + case mode new not null

declare
v_empno empdab.eno%type:=&n;
v_empdab empdab%rowtype;
begin
select eno, ename,doj,position,salary,comm,address into v_empdab from empdab where eno=v_empno;
dbms_output.put_line(v_empdab.eno||v_empdab.ename||v_empdab.doj||v_empdab.position||v_empdab.salary||v_empdab.comm||v_empdab.address);
end ;
SQL*Plus named parameters can be used in PL/SQL. I think you have not send us the real code.
Script start cc.sql
accept n prompt 'Enter value for n :'
set serveroutput on
declare
v_v test.v%type := &n;
r_v test%rowtype;
begin
dbms_output.put_line(v_v);
select v into v_v from test where rownum = 1;
dbms_output.put_line(v_v);
dbms_output.put_line(&n);
end;
/
End Script cc.sql
   
SQL> #cc
Enter value for n :'abc'
old 2: v_v test.v%type := &n;
new 2: v_v test.v%type := 'abc';
old 9: dbms_output.put_line(&n);
new 9: dbms_output.put_line('abc');
abc
a
abc
PL/SQL procedure successfully completed.
SQL>

PLS-00103 when running script in APEX

I'm trying to run the following PLSQL script in APEX:
DECLARE
total NUMBER;
BEGIN
SELECT COUNT(*)
INTO total
FROM one_table;
FOR i IN 0:100 --This is line 8
LOOP
INSERT INTO other_table(id, column_a) SELECT id + i * total, column_a FROM one_table;
END LOOP;
COMMIT;
END;
And I get this error:
ORA-06550: line 8, column 15: PLS-00103: Encountered the symbol "" when expecting one of the following: * & - + / at mod remainder rem .. <an exponent (**)> || multiset The symbol ".." was substituted for "" to continue.'
I don't think this is legal syntax:
FOR i IN 0:100
I think you mean:
FOR i IN 0..100

Resources