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.
Related
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.
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.
I have some tables and I want to insert into them by asking their name, then putting in the values for the columns. Thing is, Whenever I run this, it goes through all the inputs no matter what, even if I input an incorrect tables. Then I get an error that it expected = symbol instead of :=.
The code:
set serveroutput on;
declare
myTable varchar2;
begin
myTable = &input_table;
if myTable = 'Supervisor' then
insert into Supervisor values(&supID, &supName);
elsif myTable = 'Job' then
insert into Job values(&jobID, &jobName);
else dbms_output.put_line('Found no such table.');
end if;
end;
/
PL/SQL scripts (running in SQLPlus or SQLPlus emulators) are not interactive tools. When you run the script, Oracle first parses its text, then defines all &-variables, then ask you to fill them, and only then begins execution. Use any interactive tools instead (in fact, for your own task you have to write your own tool yourself).
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!
I have to use sqldeveloper 4.0. But i cant compile single package in it. It compiles in other programs, but i have to use sqldeveloper. I tryed to compile oracle tutorial package:
CREATE OR REPLACE PACKAGE emp_actions AS -- spec
TYPE EmpRecTyp IS RECORD (emp_id INT, salary REAL);
CURSOR desc_salary RETURN EmpRecTyp;
PROCEDURE hire_employee (
ename VARCHAR2,
job VARCHAR2,
mgr NUMBER,
sal NUMBER,
comm NUMBER,
deptno NUMBER);
PROCEDURE fire_employee (emp_id NUMBER);
END emp_actions;
CREATE OR REPLACE PACKAGE BODY emp_actions AS -- body
CURSOR desc_salary RETURN EmpRecTyp IS
SELECT empno, sal FROM emp ORDER BY sal DESC;
PROCEDURE hire_employee (
ename VARCHAR2,
job VARCHAR2,
mgr NUMBER,
sal NUMBER,
comm NUMBER,
deptno NUMBER) IS
BEGIN
INSERT INTO emp VALUES (empno_seq.NEXTVAL, ename, job,
mgr, SYSDATE, sal, comm, deptno);
END hire_employee;
PROCEDURE fire_employee (emp_id NUMBER) IS
BEGIN
DELETE FROM emp WHERE empno = emp_id;
END fire_employee;
END emp_actions;
And i get Error(14,1): PLS-00103: Encountered the symbol "CREATE" - on create package body line
I tryed to put "/" in front of it but i get Error(13,1): PLS-00103: Encountered the symbol "/".
I dont know the version of the database.
Thank you for help
If you want to run both statements together then you need to put a / after each of them, by itself on a new line:
CREATE OR REPLACE PACKAGE emp_actions AS -- spec
...
END emp_actions;
/
CREATE OR REPLACE PACKAGE BODY emp_actions AS -- body
...
END emp_actions;
/
... and then do 'Run Script' (F5, or the button with an icon of a green arrow over a document), rather than 'Run Statement' (Ctrl-Enter, or the button with just a green arrow). The output will go in the 'Script Output' pane.
You can't run multiple statements with 'Run Statement', although you can still select the text of one statement from a script and run that on its own - if it's a query then the output will still appear in the 'Query Result' pane.
If you've created a new package from the 'File->New' menu item, or by right-clicking the 'Package' header in the object browser and selecting 'New Package', then you can only enter the specification in the window that's displayed (the tab has the package name and an icon of a wrapped present). That does actually make more sense - I thought the line number on your second error was wrong, but that matches working in this window.
So enter just the package specification in that window and compile. Then back in the object browser, refresh the package list, right-click your new package name and select 'Create Body'. You'll get a second tab which looks very similar but the tab name will say ' body'. You can put the package body in there and compile it.
Using these views you always have the specification and body in separate tabs. Once both exist each has a button to open the other - the specification window has a button to open the body, and vice versa.