PLS-00103: Encountered the symbol "EXECUTE" when expecting one of the following: - oracle

can someone let me know where is an issue in my code?
SET SERVEROUTPUT ON;
daclare
sql_stmt varchar(500);
n number(3);
n :=0;
begin
FOR vori IN (select ri from voucher_p1 where vchstate=chr(4))
LOOP
sql_stmt := 'select count(sernum) from voucher_p1 where ENCPIN in ( select ENCPIN from voucher_p1 where ri=' || vori.ri || ')'
EXECUTE immediate sql_stmt into n;
DBMS_OUTPUT.PUT_LINE( 'Column Variable: ' || n );
END LOOP;
end;
/
i run it in oracle 9.2.0 and it retuns this message as below:
EXECUTE immediate sql_stmt into n;
*
ERROR at line 5:
ORA-06550: line 5, column 1:
PLS-00103: Encountered the symbol "EXECUTE" when expecting one of the
following:
. ( * # % & = - + ; < / > at in is mod not rem
<an exponent (**)> <> or != or ~= >= <= <> and or like
between ||

i Run ok. thank everyone.
SET SERVEROUTPUT ON;
declare
sql_stmt varchar2(500);
n number(3);
begin
FOR vori IN (select ri from voucher_p1 where vchstate=chr(4))
LOOP
DBMS_OUTPUT.PUT_LINE('select count(*) from voucher_p1 where ENCPIN in ( select ENCPIN from voucher_p1 where ri=' || vori.ri || ')');
sql_stmt := 'select count(*) from voucher_p1 where ENCPIN in ( select ENCPIN from voucher_p1 where ri=' || vori.ri || ')';
EXECUTE immediate sql_stmt into n;
DBMS_OUTPUT.PUT_LINE(n);
END LOOP;
end;
/

Related

How to generate dynamic SQL statement with generic condition

I am trying to generate a dynamic SQL statement with a generic condition.
Depending some conditions, it will be added different other conditions in where clause.
I was trying to have something like this:
declare
v_sql varchar2(500);
a number;
v_dummy number := 1;
begin
v_sql := 'delete tab_1 where v_dummy = 1 ';
if a = 1
then v_sql := v_sql || ' and col_1 = 1';
else v_sql := v_sql || ' and col_2 = 3';
end if;
execute immediate v_sql;
dbms_output.put_line(v_sql);
end;
The error raised is:
ORA-00904: "V_DUMMY": invalid identifier
Can anyone, please, guide me how to handle this situation?
The problem is with the definition of first condition (v_dummy = 1) that I need to add in order to use the "and" operand for the second condition.
Thank you,
If there is a possibility that you need multiple conditions (not in your code) you can set initial condition simply putting col_1 = col_1. No need for dummy variable at all and leaves you options to add some more conditions:
declare
v_sql varchar2(500);
a number;
begin
v_sql := 'delete tab_1 where col_1 = col_1 ';
if a = 1 then
v_sql := v_sql || ' and col_1 = 1';
else
v_sql := v_sql || ' and col_2 = 3';
end if;
execute immediate v_sql;
dbms_output.put_line(v_sql);
end;
Use a bind variable:
declare
v_sql varchar2(500);
a number;
v_dummy number := 1;
begin
v_sql := 'delete tab_1 where :v_dummy = 1 ';
if a = 1
then v_sql := v_sql || ' and col_1 = 1';
else v_sql := v_sql || ' and col_2 = 3';
end if;
execute immediate v_sql USING v_dummy;
dbms_output.put_line(v_sql);
end;
/
or since it evaluates to 1 = 1, you can omit it:
declare
v_sql varchar2(500);
a number;
begin
v_sql := 'delete tab_1';
if a = 1
then v_sql := v_sql || ' where col_1 = 1';
else v_sql := v_sql || ' where col_2 = 3';
end if;
execute immediate v_sql;
dbms_output.put_line(v_sql);
end;
/

How to ignore tab/column not exist error in oracle?

I have the following code:
declare
var_cdb varchar2(3);
var_eleven varchar2(2);
...
..
begin
SELECT to_number(substr(version,1,2)) into var_eleven FROM V$INSTANCE;
if var_eleven > 11
then
select cdb into var_cdb from v$database;
if var_cdb = 'YES'
then
....
But executing the PL/SQL code in Oracle 11g I get the following error:
ERROR at line 12:
ORA-06550: line 12, column 9:
PL/SQL: ORA-00904: "CDB": invalid identifier
ORA-06550: line 12, column 2:
PL/SQL: SQL Statement ignored
ORA-06550: line 15, column 43:
PL/SQL: ORA-00942: table or view does not exist
ORA-06550: line 15, column 19:
PL/SQL: SQL Statement ignored
ORA-06550: line 17, column 248:
...
How can I ignore if table/column not exist in this version? I need create a script that execute successfully in 11g up and verify if is cdb and has or not pdbs if the database 12c up.
There are 3 standard ways to do this:
You can use conditional compilation and dbms_db_version package:
declare
v_version pls_integer:= dbms_db_version.version;
v_release pls_integer:= dbms_db_version.release;
v_version_full varchar2(12);
var_cdb varchar2(3);
begin
$IF dbms_db_version.ver_le_11 $THEN
var_cdb := 'NO';
select version into v_version_full from v$instance;
$ELSE
select cdb into var_cdb from v$database;
select version_full into v_version_full from v$instance;
$END
dbms_output.put_line(v_version||'.'||v_release);
dbms_output.put_line(v_version_full);
dbms_output.put_line(var_cdb);
end;
/
Example output from 18.3:
18.0
18.3.0.0.0
YES
To use xmltype(cursor({your query with *})) and parse its' output:
select
nvl(version_full, version) as db_version
from xmltable(
'/'
passing xmltype(cursor(select * from v$instance))
columns
version varchar2(12) path '/ROWSET/ROW/VERSION',
version_full varchar2(12) path '/ROWSET/ROW/VERSION_FULL'
);
So if your query doesn't return version_full column to xml, you get null in version_full without errors.
To check if this query really exists in the table/view and use dynamic sql to return only required data:
DECLARE
v_sql varchar2(32000);
select_list varchar2(100);
c NUMBER;
d NUMBER;
col_cnt INTEGER;
f BOOLEAN;
rec_tab DBMS_SQL.DESC_TAB;
col_num NUMBER;
val varchar2(100);
cursor_status INTEGER;
PROCEDURE print_rec(i int, rec in DBMS_SQL.DESC_REC) IS
BEGIN
DBMS_OUTPUT.PUT_LINE(i || '. ' || rec.col_name);
--DBMS_OUTPUT.PUT_LINE('col_type = ' || rec.col_type);
--DBMS_OUTPUT.PUT_LINE('col_maxlen = ' || rec.col_max_len);
--DBMS_OUTPUT.PUT_LINE('col_name = ' || rec.col_name);
--DBMS_OUTPUT.PUT_LINE('col_name_len = ' || rec.col_name_len);
--DBMS_OUTPUT.PUT_LINE('col_schema_name = ' || rec.col_schema_name);
--DBMS_OUTPUT.PUT_LINE('col_schema_name_len = ' || rec.col_schema_name_len);
--DBMS_OUTPUT.PUT_LINE('col_precision = ' || rec.col_precision);
--DBMS_OUTPUT.PUT_LINE('col_scale = ' || rec.col_scale);
--DBMS_OUTPUT.PUT_LINE('col_null_ok = ' || case when rec.col_null_ok 'true' else 'false' end);
END;
BEGIN
c := DBMS_SQL.OPEN_CURSOR;
select
listagg(column_name, ',') within group(order by column_id) as select_list
into select_list
from all_tab_columns tc
where tc.owner='SYS'
and table_name='V_$INSTANCE'
and column_name like 'VERSION%';
v_sql := 'select '|| select_list || ' from v$instance';
DBMS_SQL.PARSE(c, v_sql, DBMS_SQL.NATIVE);
d := DBMS_SQL.EXECUTE(c);
DBMS_SQL.DESCRIBE_COLUMNS(c, col_cnt, rec_tab);
for i in 1..col_cnt loop
dbms_sql.define_column(c,i,val,100);
end loop;
/*
* Following loop could simply be for j in 1..col_cnt loop.
* Here we are simply illustrating some of the PL/SQL table
* features.
*/
col_num := rec_tab.first;
IF (col_num IS NOT NULL) THEN
LOOP
print_rec(col_num, rec_tab(col_num));
col_num := rec_tab.next(col_num);
EXIT WHEN (col_num IS NULL);
END LOOP;
END IF;
LOOP
cursor_status := dbms_sql.fetch_rows(c);
for i in 1..col_cnt loop
dbms_sql.column_value(c,i,val);
DBMS_OUTPUT.PUT_LINE(rec_tab(i).col_name||':'||val);
end loop;
EXIT WHEN cursor_status != 100;
END LOOP;
DBMS_SQL.CLOSE_CURSOR(c);
END;
/
Result:
1. VERSION
2. VERSION_LEGACY
3. VERSION_FULL
VERSION:18.0.0.0.0
VERSION_LEGACY:18.0.0.0.0
VERSION_FULL:18.3.0.0.0

Oracle PLSQL invalid cursor error I don't understand

I'm still a relatively newbe when it comes to PL/SQL.
Using Oracle 12c on Linux RHEL 6.8, the following shell script will attempt to activate all RI constraints in a collection of tables, and if they fail with parent key failures, it will dump the first 100 rows (or less) of the offending data. Or at least that is the goal. Since the script deals mostly with system tables on 12c (with only a small user table list that is unique to my installation), I'm including the whole thing exactly from my environment.
The main work occurs in the exception handling where the system tables are queried for the constraint, and user queries are formed from those data.
As a extra goal, the output is rather messy and I want to clean it up, but first it has to work :)
The output / error I get for my tables is the following:
ERROR Handling here for table NRNG_MTC_VST Constraint Name:
SYS_C0011790 Final SQL = SELECT DISTINCT NRNG_MTC_VST.LOG_CRT_DT ,
NRNG_MTC_VST.NRRNG_MTC_LG_ID FROM ODB_PRIMARY.NRNG_MTC_VST WHERE NOT
EXISTS (SELECT 1 FROM ODB_PRIMARY.NRNG_MTC_LOG WHERE
NRNG_MTC_VST.LOG_CRT_DT = NRNG_MTC_LOG.LOG_CRT_DT AND
NRNG_MTC_VST.NRRNG_MTC_LG_ID = NRNG_MTC_LOG.NRRNG_MTC_LG_ID) FETCH
FIRST 100 rows only
---xxx End SQL DECLARE
* ERROR at line 1: ORA-01001: invalid cursor ORA-06512: at line 111 ORA-02298: cannot validate (ODB_PRIMARY.SYS_C0011790) - parent keys
not found
The output SQL from my print_line is correct, and would work if pasted directly into a SQLDeveloper session. There is just something silly about how the cursor is defined I don't understand.
The full text of the script. BYW, if you see other bonehead changes that should be made unrelated to the error, please suggest them as well.
cd $OGGHOME/scripts
export ORACLE_SID=odbod07 $ORACLE_HOME/bin/sqlplus <<-EOF / as sysdba
alter session set container=p01_odbod07;
set echo on set feedback on
set heading off
set serveroutput on size 10000
DECLARE finalsql varchar2(2048);
part1sql varchar2(1024) ;
part2sql varchar2(1024) := ' ';
cownername varchar2(1024);
ctablename varchar2(1024);
pownername varchar2(1024);
ptablename varchar2(1024);
cnt number := 0;
-- Weak cursor defs
my_cursor sys_refcursor;
BEGIN FOR i in (
select owner, table_name, constraint_name
from dba_constraints
where constraint_type = 'R'
and status = 'DISABLED'
and owner = 'ODB_PRIMARY'
and TABLE_NAME in
-- enter user tables with RI constraints here
('RRNG_MTC_STN_CPLY',
'NRNG_MTC_VST_MTRL_USG',
'NRNG_MTC_VST',
'CAR_CORE',
'NRNG_MTC_LOG'))
-- end user table definitions, rest of code should rely only on system tables
LOOP BEGIN
dbms_output.put_line('alter table '||i.owner|| '.' ||
i.table_name || ' enable constraint '||i.constraint_name);
execute immediate 'alter table '||i.owner|| '.' ||
i.table_name || ' enable constraint '||i.constraint_name;
EXCEPTION
-- exception handling - dump offending data
WHEN OTHERS THEN -- take all exceptions for now
dbms_output.put_line ('ERROR Handling here for table ' ||
i.table_name || ' Constraint Name: ' ||i.constraint_name);
finalsql := 'SELECT DISTINCT ';
part1sql := '';
part2sql := ' ';
cnt := 0;
for constraint in (
SELECT ucc1.OWNER as childowner,
ucc1.TABLE_NAME as childtable,
ucc1.column_name as childcolumn,
ucc2.OWNER as parentowner,
ucc2.TABLE_NAME as parenttable,
ucc2.column_name as parentcolumn,
utc1.data_type as childdatatype,
utc1.data_length as childdatalen
FROM all_constraints uc ,
all_cons_columns ucc1 ,
all_cons_columns ucc2,
all_tab_columns utc1
WHERE
uc.constraint_name = ucc1.constraint_name
AND uc.r_constraint_name = ucc2.constraint_name
AND ucc1.POSITION = ucc2.POSITION
AND ucc1.table_name = utc1.table_name
AND ucc1.column_name = utc1.column_name
AND uc.constraint_type = 'R'
AND uc.constraint_name = i.constraint_name
ORDER BY ucc1.TABLE_NAME , uc.constraint_name)
loop
cownername := constraint.childowner;
ctablename := constraint.childtable;
pownername := constraint.parentowner;
ptablename := constraint.parenttable;
if cnt > 0 then
part1sql := part1sql || ' , ';
part2sql := part2sql || ' AND ';
end if;
part1sql := part1sql || constraint.childtable ||
'.'||constraint.childcolumn || ' ';
part2sql := part2sql || constraint.childtable || '.'
|| constraint.childcolumn || ' = '
|| constraint.parenttable || '.' ||
constraint.parentcolumn;
cnt := cnt + 1;
end loop;
finalsql := finalsql || part1sql ||
' FROM ' || ' ' || cownername ||
'.' || ctablename ||
' WHERE NOT EXISTS (SELECT 1 FROM ' ||
pownername || '.' || ptablename ||
' WHERE ' || part2sql || ') FETCH FIRST 100 rows only';
dbms_output.put_line ('Final SQL = ' || finalsql);
dbms_output.put_line ('---xxx End SQL');
open my_cursor for finalsql;
dbms_sql.return_result(my_cursor);
close my_cursor;
-- EXECUTE IMMEDIATE finalsql;
END;
end loop; end;
/
EOF
Many thanks for any help provided.
Brian
Just to narrow this down to a simple test case, I think this is the error you are seeing:
declare
my_cursor sys_refcursor;
begin
open my_cursor for 'select ''Hello, world'' as message from dual';
dbms_sql.return_result(my_cursor);
close my_cursor; -- << Remove this line
end;
/
ERROR at line 1:
ORA-01001: invalid cursor
ORA-06512: at line 6
This is because you attempted to close the cursor when you have already passed it to dbms_sql for processing. Remove the line with close my_cursor.
declare
my_cursor sys_refcursor;
begin
open my_cursor for 'select ''Hello, world'' as message from dual';
dbms_sql.return_result(my_cursor);
end;
/
PL/SQL procedure successfully completed.
ResultSet #1
MESSAGE
------------
Hello, world
1 row selected.
I had same kind of issue when i tried to print Ref_cursor directly. Then i created a Record type variable and then fetched field values in that variable and then i used DBMS_OUTPUT for that record type variable.
Please see if below code and scenario can help you-
set serveroutput on;
declare
v_sql varchar2(1000);
v_cursor sys_refcursor;
type myrec is record(col1 varchar2(100),col2 varchar2(1000));
rec myrec;
begin
v_sql:='select name,status from t_employee where user_id in (''C001117'',''C001122'')';
open v_cursor for v_sql;
loop
fetch v_cursor
into rec;
exit when v_cursor%notfound;
dbms_output.put_line( rec.col1||':status '||rec.col2 );
end loop;
end;
/
The following is my semi-complete script. Given a table list, it will attempt to activate the RI Constraints, and if they fail it will print out the FK data records in the child table that prevent it from being applied.
The hardest part of this project was the fact that the FKs can be any number of columns and of any type, so the print the results of the select in this case was very tricky (IMO).
Thanks for the help people provided.
cd $OGGHOME/scripts
. ./functions.sh
$ORACLE_HOME/bin/sqlplus ${ORACLE_USERID}/${ORACLE_PASSWORD}#${ORACLE_SID} << EOF
set echo on
set feedback on
set heading off
set serveroutput on size unlimit
DECLARE
finalsql varchar2(2048);
part1sql varchar2(1024) ;
part2sql varchar2(1024) := ' ';
cownername varchar2(1024);
ctablename varchar2(1024);
pownername varchar2(1024);
ptablename varchar2(1024);
cnt number := 0;
desc_tab dbms_sql.desc_tab;
col_count INTEGER;
cursor_name INTEGER;
-- Weak cursor defs
my_cursor sys_refcursor;
col1 varchar2(50);
d number;
j number;
lineout varchar2(2048);
plineout varchar2(2048);
rows number;
eCount number := 0;
BEGIN
FOR i in (
select owner, table_name, constraint_name
from dba_constraints
where constraint_type = 'R'
and status = 'DISABLED'
and owner = '$DBSCHEMA'
and TABLE_NAME in (
'RRNG_MTC_STN_CPLY',
'NRNG_MTC_VST_MTRL_USG',
'NRNG_MTC_VST',
'MTC_TSK_HRHY'))
LOOP
BEGIN
dbms_output.put_line ('.');
dbms_output.put_line ('=====================================');
dbms_output.put('alter table '||i.owner|| '.' || i.table_name || ' enable constraint '||i.constraint_name);
execute immediate 'alter table '||i.owner|| '.' || i.table_name || ' enable constraint '||i.constraint_name;
dbms_output.put_line (' ... SUCCESS');
EXCEPTION -- exception handling - dump offending data
WHEN OTHERS THEN
eCount := eCount + 1;
dbms_output.put_line (' ... FAILED. Constraint Name: ' || i.constraint_name);
finalsql := 'SELECT DISTINCT ';
part1sql := '';
part2sql := ' ';
cnt := 0;
for constraint in (
SELECT ucc1.OWNER as childowner,
ucc1.TABLE_NAME as childtable,
ucc1.column_name as childcolumn,
ucc2.OWNER as parentowner,
ucc2.TABLE_NAME as parenttable,
ucc2.column_name as parentcolumn,
utc1.data_type as childdatatype,
utc1.data_length as childdatalen
FROM all_constraints uc ,
all_cons_columns ucc1 ,
all_cons_columns ucc2,
all_tab_columns utc1
WHERE
uc.constraint_name = ucc1.constraint_name
AND uc.r_constraint_name = ucc2.constraint_name
AND ucc1.POSITION = ucc2.POSITION
AND ucc1.table_name = utc1.table_name
AND ucc1.column_name = utc1.column_name
AND uc.constraint_type = 'R'
AND uc.constraint_name = i.constraint_name
ORDER BY ucc1.TABLE_NAME ,
uc.constraint_name)
loop
cownername := constraint.childowner;
ctablename := constraint.childtable;
pownername := constraint.parentowner;
ptablename := constraint.parenttable;
if cnt > 0 then
part1sql := part1sql || ' , ';
part2sql := part2sql || ' AND ';
end if;
part1sql := part1sql || constraint.childtable || '.' || constraint.childcolumn || ' ';
part2sql := part2sql || constraint.childtable || '.' || constraint.childcolumn || ' = '
|| constraint.parenttable || '.' || constraint.parentcolumn;
cnt := cnt + 1;
end loop;
finalsql := finalsql || part1sql || ' FROM ' || ' ' || cownername || '.' || ctablename || ' WHERE NOT EXISTS (SELECT 1 FROM ' ||
pownername || '.' || ptablename || ' WHERE ' || part2sql || ') FETCH FIRST 100 rows only';
dbms_output.put_line ('Final SQL = (' || finalsql || ')');
-- dbms_output.put_line ('---xxx End SQL');
lineout := 'Child Table: ' || ctablename || '(';
plineout := 'Parent Table: ' || ptablename;
cursor_name := dbms_sql.open_cursor;
dbms_sql.PARSE (cursor_name, finalsql, DBMS_SQL.NATIVE);
d := dbms_sql.execute (cursor_name);
dbms_sql.describe_columns (cursor_name, col_count, desc_tab);
for j in 1..col_count
LOOP
DBMS_SQL.DEFINE_COLUMN (cursor_name, j, col1, 30);
lineout := lineout || desc_tab(j).col_name || ' , ';
-- plineout := plineout || constraint.parentcolumn || ' ';
-- dbms_output.put_line ('Column 1: ' || j || ' is ' || desc_tab(j).col_name || ' type '
-- || desc_tab(j).col_type);
END LOOP j;
lineout := lineout || ')';
-- plineout := plineout || ')';
dbms_output.put_line (lineout);
dbms_output.put_line (plineout);
lineout := NULL;
for j in 1..col_count
LOOP
if j > 1 then
lineout := lineout || ' ';
end if;
lineout := lineout || desc_tab(j).col_name;
END LOOP;
dbms_output.put_line (lineout);
dbms_output.put_line ('----------------------------------------');
LOOP
rows := dbms_sql.fetch_rows (cursor_name);
EXIT WHEN rows = 0;
lineout := NULL;
for j in 1..col_count
LOOP
dbms_sql.column_value (cursor_name, j, col1);
if j > 1 then
lineout := ltrim(lineout || ' ' || col1);
else
lineout := col1;
END IF;
END LOOP;
dbms_output.put_line (lineout);
END LOOP;
dbms_sql.close_cursor (cursor_name);
END;
end loop;
end;
/
EOF
your FETCH FIRST 100 rows only would seem to be out of place.
This is part of the BULK COLLECT clause in a SELECT statement in PL/SQL; as far as I know, it is not part of a SQL statement you can pass into a cursor like this
This is resulting in the cursor statement being invalid

Where clause in Dynamic sql

I am trying to get all the tables where bank_id is 01.
I have written the following block
DECLARE
cursor cBankId is
select owner||'.'||table_name from all_tab_columns where column_name = 'BANK_ID';
v_table all_tab_columns.table_name%TYPE;
vcount varchar2(50);
BEGIN
open cBankId;
loop
fetch cBankId into v_table;
exit when cBankId%notfound;
execute immediate 'select count(*) from ' || v_table into vcount || ' where bank_id = 01';
IF vcount > 0 THEN
DBMS_OUTPUT.PUT_LINE (v_table);
END IF;
end loop;
close cBankId;
END;
I want to know how to put where clause in the execute immediate statement .
I am getting the error
ORA-06550: line 15, column 67:
PLS-00103: Encountered the symbol "|" when expecting one of the following:
. ( , % ; return returning using
You only need to change the order you're writing the parts of your query.
When using dynamic SQL, you need something like this:
SQL> declare
2 v_table varchar2(30);
3 vCount number;
4 begin
5 v_table := 'dual';
6 execute immediate 'select count(*) from ' || v_table || ' where 1=1' into vcount;
7 --
8 dbms_output.put_line('vCount: ' || vCount);
9 end;
10
11 /
vCount: 1
PL/SQL procedure successfully completed.
That is: execute immediate 'select ... from ... where ...' INTO ...;
You can't dynamically use a variable for the table name, instead use:
DECLARE
cursor cBankId is
select owner||'.'||table_name from all_tab_columns where column_name = 'BANK_ID';
v_table all_tab_columns.table_name%TYPE;
vcount varchar2(50);
v_sql varchar2(1000);
BEGIN
open cBankId;
loop
fetch cBankId into v_table;
exit when cBankId%notfound;
v_sql := 'select count(*) from ' || v_table || ' into vcount where bank_id = 01';
execute immediate v_sql;
IF vcount > 0 THEN
DBMS_OUTPUT.PUT_LINE (v_table);
END IF;
end loop;
close cBankId;
END;

ORA-06512: at .MAX_MIN_HIST", line 37 ORA-06512: at line 1

create or replace procedure MAX_min_hist
as
sql_stmt varchar2(4000);
sql_stmt2 varchar2(4000);
sql_stmt3 varchar2(4000);
var_date_of_trade hist_data.date_of_trade%type;
sql_stmt4 varchar2(4000);
cursor c1 is SELECT date_of_trade FROM hist_data where date_of_trade>TO_DATE('2015-07-01','YYYY-MM-DD');
begin
open c1;
Loop
fetch c1 into var_date_of_trade;
sql_stmt3 := 'CREATE OR REPLACE VIEW V_HIST_DATA AS SELECT * FROM HIST_DATA WHERE DATE_OF_TRADE < ' || var_date_of_trade;
sql_stmt := 'create table MAX_min_hist_data as ( select * from (
SELECT
CODE,
((CLOSE_PRICE-"Worst")/nullif("Worst",0))*100 Upper_from_down,
(("Best"-CLOSE_PRICE)/nullif(CLOSE_PRICE,0))*100 Down_from_Best,
"Worst",
"Best",
Close_Price
from (
SELECT
DISTINCT
CODE,
MIN(close_price) OVER (PARTITION BY CODE) "Worst",
MAX(close_price) OVER (PARTITION BY CODE) "Best",
first_value(close_price) over (PARTITION BY CODE order by date_of_trade desc) CLOSE_PRICE
FROM V_HIST_DATA
)
)
)';
sql_stmt2 := ' drop table MAX_min_hist_data';
execute immediate sql_stmt3;
execute immediate sql_stmt2;
execute immediate sql_stmt;
end loop;
end;
/
exec MAX_min_hist
Execution of above code gives me the following errors-
ORA-00933: SQL command not properly ended
ORA-06512: at MAX_MIN_HIST", line 37
ORA-06512: at line 1
DATE_OF_TRADE is in 'YYYY-MM-DD' FORMAT .
thanks in advance
Replace this line:
sql_stmt3 := 'CREATE OR REPLACE VIEW V_HIST_DATA AS SELECT * FROM HIST_DATA WHERE DATE_OF_TRADE < ' || var_date_of_trade;
with this one:
sql_stmt3 :=
'CREATE OR REPLACE VIEW V_HIST_DATA AS '
|| 'SELECT * FROM HIST_DATA '
|| 'WHERE DATE_OF_TRADE < to_date( '''
|| to_char( var_date_of_trade, 'yyyy-mm-dd' )
|| ''', ''yyyy-mm-dd'' ) ';

Resources