Keep getting an error and do not understand why - plsql

sales_heads will compile with no errors but when I try to compile sales_lines it comes up with 2 errors which are:
Error(3,1): PL/SQL: SQL Statement ignored
Error(3,111): PL/SQL: ORA-02289: sequence does not exist
could someone tell me where I am going wrong.
drop sequence nsale_seq;
CREATE SEQUENCE nsale_seq
START WITH 1000000000
INCREMENT BY 1
NOCACHE
NOCYCLE;
create or replace PROCEDURE sale_heads (staffID_new number, customerID_new number)
is begin
insert into SALE_HEAD (sale_num, sale_date, status, staff_id, customer_id) values (nsale_seq.NEXTVAL, sysdate, 'P', staffID_new, customerID_new);
end sale_heads;
/
create or replace PROCEDURE sales_lines (productCode_new number, quantity_new number, actualPrice_new number) is
begin
insert into SALE_LINE (actual_price, quantity, sale_num, product_code) values (actualPrice_new, quantity_new, nsale_seg.CURRVAL, productCode_new);
end sales_lines;
/

You have placed / this after your first procedure as well as after second procedure. / is used to show the end of the file or the statements to be executed!Hence, it's unable to find second procedure!!!
That's why your second procedure is getting ignored.
Please remove the / first slash after first procedure sale_heads.
I believe it'll work perfectly!

Related

What is causing the syntax error in my sql stored procedure?

I'm pretty new at stored procedures and I get the following syntax error in my code:
You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'DELIMITER //CREATE PROCEDURE sp_create_probe(IN matrix_id INT, IN oligo_id IN...' at line 2
Here's the code for my procedure:
DELIMITER //
CREATE PROCEDURE sp_create_probe(IN matrix_id INT, IN oligo_id INT)
BEGIN
SET FOREIGN_KEY_CHECKS=0;
INSERT INTO Probes (oligo, microarray) VALUES (oligo_id, matrix_id);
SET FOREIGN_KEY_CHECKS=1;
END //
DELIMITER ;
Hope someone can help me out
EDIT:
Well I ended up fixing it. Classic noob mistake.
I had this line before the code you see above:
DROP PROCEDURE IF EXISTS sp_create_probe
Didn't copy that line into the post for some reason but it's missing the ";" at the end. That fixed it.

Issue with cursor retrieving values

I am trying to use a cursor for the following block of code, but it is not compiling and giving error saying the following:
PL/SQL: SQL Statement ignored
PL/SQL: ORA-00947: not enough values
All the trxn table values are being fetched, Does anyone have faced similar error before? Any way this can be fixed or am I missing something?
I am trying to use a cursor for the following block of code, but it is not compiling and giving error saying the following:
PL/SQL: SQL Statement ignored
PL/SQL: ORA-00947: not enough values
All the trxn table values are being fetched, Does anyone have faced similar error before? Any way this can be fixed or am I missing something?
cursor bulk_select is
with trxn as (
select --+ materialize
-- Wire info
wt.id
,wt.fl
,wt.abc
,wt.xyz
from
wt_temp_t wt
where
<condition>
)
select a
,b
,c
from
(
select
w.aa
,w.bb
,w.cc
trxn w
where
<condition>
)included_trxn
-- Left joins in case the acct is null or external
left outer join table
acct a_cp,
cust c_cp
where
<condition>
type tab_bulk_select is table of bulk_select%rowtype;
cur_bulk_select tab_bulk_select;
BEGIN
EXECUTE IMMEDIATE 'truncate table AML_CAMBRS6_HUB_SPOKE_TMP';
commit;
open bulk_select;
LOOP
fetch bulk_select bulk collect into cur_bulk_select limit 50000;
FORALL i IN 1..cur_bulk_select.COUNT
insert /*+ append */ into AML_CAMBRS6_HUB_SPOKE_TMP (a,b,c)
values cur_bulk_select(i);
commit;
EXIT When bulk_select%NOTFOUND;
END LOOP;
CLOSE bulk_select;
Its because in trxn you are fetching only 2 values wt.fo_trxn_seq_id ,wt.pass_thru_fl.
But in select clause select w.aa ,w.bb, w.cc trxn w where <condition> you are using aa, bb, cc and also missing from in statement.

Column sizes sometimes do not fit when I move data from table 'TABLE_1' to table 'TABLE_2'. oracle is sending an error as follows

I want to move data from one table to a table.
I wrote one plsql code for this process.
Column sizes sometimes do not fit when I move data from table 'TABLE_1' to table 'TABLE_2'.
oracle is sending an error as follows.
I have identified one exception to get rid of this error.
but this exception is in a static state.
I want to make this exception part dynamic.
that is, if this error occurs in other columns, I want to automatically increase the size of the column in those columns.
the following is the plsql code I wrote.
for example, an oracle insert operation may give the same error in the 'explanation' column.
I want to modify the size of the 'explanation' column.
if this error occurs,
I want to perform these operations automatically for each column.
DECLARE
SAYAC INTEGER;
SAYAC_2 INTEGER;
extension_already_exists EXCEPTION;
PRAGMA EXCEPTION_INIT(extension_already_exists, -12899);
BEGIN
SAYAC:=0;
FOR XX IN (SELECT
FILE_NO,
NAME,
EV_ADRESI,
explanation
FROM TABLE_1
WHERE NOT EXISTS (SELECT *
FROM TABLE_2
WHERE TABLE_2.FILE_NO = TABLE_1.FILE_NO )
)LOOP
BEGIN
SAYAC:=SAYAC+1;
EXECUTE IMMEDIATE 'INSERT INTO
TABLE_2(
FILE_NO,
NAME,
EV_ADRESI,
explanation
)
VALUES
(
:A,
:B,
:C,
:D
) '
using XX.FILE_NO,XX.NAME,XX.EV_ADRESI,xx.explanation
;
IF MOD(SAYAC,1000)=0 THEN
COMMIT;
END IF;
EXCEPTION WHEN extension_already_exists THEN
dbms_output.put_line('seviye cok buyuk = '||sqlcode||' FILE_NO = '||XX.FILE_NO||','||xx.EV_ADRESI);--this error may occur in other columns
EXECUTE IMMEDIATE 'ALTER TABLE TABLE_2 MODIFY EV_ADRESI,explanation VARCHAR2(100)'; --forexample xx.explanation
NULL; --I want to make this place dynamic
end;
END LOOP;
COMMIT;
END;
ORA-12899: value too large for column "HOSPITAL"."table_2"."EV_ADRESI" (actual: 34, maximum: 20)
ORA-06512: at line 20
That won't work anyway. Even if you capture the error and enlarge the column, you'd miss the offending row as you should repeat the previous insert (which failed).
From my point of view, you should first match columns' datatpyes and then move data from here to there in a simple manner.
By the way, why did you use dynamic SQL for insert? There's nothing dynamic there.

Make SQLite abort on first error (and sing songs)

I wanted to name this post Make SQLite abort on first error but StackOverflow's AI overlords decided it doesn't fit their conception of intelligent human behavior. For the record, I was googling exactly that, but perhaps even Google AI considered my question unworthy and didn't bother to help me. Mods, feel free to change the title according to what your AI bosses desire (if you can figure it out).
I have this script
create if not exists table entries (
id integer primary key,
start datetime not null,
end datetime not null
);
delete from entries;
insert into entries values (1, '2018-08-01 10:00', '2018-08-01 15:00');
insert into entries values (2, '2018-08-01 17:00', '2018-08-01 20:00');
insert into entries values (1, '2018-08-02 19:00', '2018-08-02 00:00');
insert into entries values (1, '2018-08-03 00:00', '2018-08-03 04:00');
insert into entries values (1, '2018-08-03 14:00', '2018-08-03 18:00');
There is a mistake in create statement. When I run the script I get
% sqlite3 db.sqlite3 <ddl.sql
Error: near line 1: near "if": syntax error
Error: near line 7: no such table: entries
Error: near line 8: no such table: entries
Error: near line 9: no such table: entries
Error: near line 10: no such table: entries
Error: near line 11: no such table: entries
Error: near line 12: no such table: entries
How do I make SQLite exit executing the script on first error it encounters? I'm looking for equivalent of set -e in Bash.
From the documentation, it looks like you can turn on the dot command .bail.
.bail on|off Stop after hitting an error. Default OFF
See also - O'Reilly Using Sqlite
Edit
To exit, you can use the .exit dot command.

ORA-00900: invalid SQL statement when using loop to insert rows

I'm trying to insert multiple rows into an Oracle 11g table by running the following in Aqua Data Studio (version 15.0.11), using this:
BEGIN
FOR i IN 1..700 LOOP
INSERT INTO lims.stock_template (
stock_template_id,
name,
group_id,
version,
version_status,
workflow_id,
amount,
stock_type_id,
aliquot_template_id,
auto_authorise,
reorder_amount
)
VALUES (
lims.sq_stock_template.nextval,
lims.sq_stock_template.currval,
21,
1,
'A',
51881,
0,
103,
2362,
'F',
0
);
END LOOP;
END;
/
but I get the following error:
>[Error] Script lines: 30-30 ------------------------
ORA-00900: invalid SQL statement
Script line 30, statement line 1, column 0
If I run just the INSERT statement by itself, it works just fine, but I want to be able to insert multiple rows in one operation.
I'm sure that I've run something similar in the past, but I can't see what the problem is.
Any help much appreciated.
Thanks
In your example, you included a BEGIN and END clause, which turns this into a "Anonymous PL/SQL Block"
http://docstore.mik.ua/orelly/oracle/prog2/ch15_03.htm
Within a PL/SQL block you can't execute a SQL statement directly. You have to use only PL/SQL. See attached example.
Now you can create a stored procedure to execute your insert and use Script Object to Window As -> Execute or Execute Bind. See below screenshot.
In a stored procedure or a stored procedure within a package you can do whatever you like!
create or replace procedure testMe as
begin
FOR i IN 1..700 LOOP
INSERT INTO lims.stock_template (
stock_template_id,
name,
group_id,
version,
version_status,
workflow_id,
amount,
stock_type_id,
aliquot_template_id,
auto_authorise,
reorder_amount
)
VALUES (
lims.sq_stock_template.nextval,
lims.sq_stock_template.currval,
21,
1,
'A',
51881,
0,
103,
2362,
'F',
0
);
END LOOP;
end;
This is EXACTLY analogous to typing every line in at the SQL prompt form the word begin to the word end;.
What is more then likely tripping you up is the slash at the bottom. In the CLI sqlPlus the end of the line with the semicolon ( ; ) causes it to run the process, hence :
select 'A' from dual;
The semicolon tells it to execute.
IF on the other hand you type in the word begin then it shifts mode and will accept semicolons anywhere and just keep adding your strings to the buffer. ON A BLANK LINE the type the / and return and it will try and execute your code.
I think if you remove the slash it will execute quite nicely.

Resources