Can i use insert into tables in a procedure (on oracle) ? example:
procedure my_procedure (aa1 number ,aa2 number ) is
begin
insert into lam_table values(aa1,aa2,null) ;(*ofcourse depending on the tables )
...
...
end ;
** note
i tried it and it worked but there were a message in the bottom that said (successfully compiled not modified )
Yes, you can. Just be aware of the difference between creating the procedure and executing it. Once the procedure is created, you can execute it with:
begin
my_procedure(aa1, aa2);
end;
where aa1 and aa2 are the supplied values for the args.
Just as dpbradley says.
Also, any insert performed by your insert statement will only be visible in that session unless you do a
commit;
Related
I am trying to insert a records into my table CAT_BOM_ITEM from table TMP_BOM_STEEL8. If there are new records from the source I want the target table to be updated.
I have a procedure created and I am using a merge query inside it.
create or replace PROCEDURE SP_LOAD_CAT_BOM_MATERIALS AS
BEGIN
DELETE FROM BI_ODS.CAT_BOM_ITEM;
INSERT INTO BI_ODS.CAT_BOM_ITEM
(
ID_BOM_ITEM,
PT_PART,
PS_COMP,
IPD_PART,
SEPARADOR,
ORG_ID,
DB_ID,
LOADDATE
)
SELECT
A.ID_SEPARADOR,
A.PT_PART,
A.PS_COMP,
A.IPD_PART,
A.SEPARADOR,
A.ORG_ID,
A.DB_ID,
A.LOADDATE
FROM TMP_BOM_STEEL8 A;
COMMIT;
EXECUTE IMMEDIATE
'
Merge Into BI_ODS.CAT_BOM_ITEM B
USING
(SELECT
ID_SEPARADOR,
PT_PART,
PS_COMP,
IPD_PART,
SEPARADOR,
ORG_ID,
DB_ID,
LOADDATE
FROM TMP_BOM_STEEL8 ) A
ON (A.ID_SEPARADOR = B.ID_BOM_ITEM
AND A.DB_ID = B.DB_ID
AND A.ORG_ID = B.ORG_ID)
WHEN MATCHED THEN UPDATE SET
A.PT_PART = B.PT_PART
A.PS_COMP= B.PS_COMP
A.IPD_PART= B.IPD_PART
A.SEPARADOR = B.SEPARADOR
WHEN NOT MATCHED THEN INSERT (
ID_SEPARADOR,
PT_PART,
PS_COMP,
IPD_PART,
SEPARADOR,
ORG_ID,
DB_ID,
LOADDATE)
VALUES (
A.ID_SEPARADOR,
A.PT_PART,
A.PS_COMP,
A.IPD_PART,
A.SEPARADOR,
A.ORG_ID,
A.DB_ID,
A.LOADDATE);
';
COMMIT;
When i compile the procedure this is the error:
ORA-00933: SQL command not properly ended
ORA-06512: at "BI_ODS.SP_LOAD_CAT_BOM_MATERIALS", line 28
ORA-06512: at line 2
Can someone help in solving this.
Thanks in advance.
You need to:
Remove the ; semi-colon from the end of the string you are passing to EXECUTE IMMEDIATE;
Add commas at the end of each assignment in the UPDATE clause of the MERGE statement;
Swap the left- and right-terms in the UPDATE assignments as you are updating B from A (rather than vice versa);
Change INSERT ( ID_SEPARADOR, to INSERT ( ID_BOM_ITEM,; and
Add END; to terminate the stored procedure.
You also don't need to use EXECUTE IMMEDIATE and you shouldn't COMMIT in a stored procedure (as it prevents you from being able to ROLLBACK multiple statements; instead, use COMMIT in the PL/SQL block you are using to call the procedure that way you can control when the COMMIT occurs and can chain several procedures together and potentially roll them all back if needed).
db<>fiddle here
You should have an END statement at the end of your procedure, but I don't see anything else particularly wrong. You don't need EXECUTE IMMEDIATE and that seems to be the line it's objecting to, so try removing that.
I'm trying to create a procedure named "Greetings" in the oracle apex. The Procedure Greetings runs with some error throwing up called "Success with compilation error". Is there anything wrong with my below code.
Code:
create table tb_Animals (
txt_Name varchar(20) Primary Key,
int_Weight number
);
insert into tb_Animals values ('Dog',30);
insert into tb_Animals values ('Cat',15);
CREATE OR REPLACE PROCEDURE greetings
AS
BEGIN
select * from tb_Animals;
END;
In PL/SQL, you have to insert those values into something, usually a variable, However, if you're selecting the whole table contents, then it has to be something else; one option might be a refcursor. For example:
SQL> CREATE OR REPLACE PROCEDURE greetings
2 AS
3 rc sys_refcursor;
4 BEGIN
5 open rc for
6 select * from tb_Animals;
7 END;
8 /
Procedure created.
SQL>
It is now correct as far as compilation is concerned, but - actual "solution" depends on what you really want to do.
Two problems I see here:
The code you provided is a SQL script that will create the procedure, not run the procedure. The error is telling you that the procedure has been created, but that it has an error. You can see the precise error by entering "SHOW ERRORS" at the sqlplus prompt after the create command completes.
The problem with the procedure itself is that you have to do something with the data you've selected. It needs to be processed into variables, or used in a for/next loop for some purpose. Simply selecting data into nothing won't work - PL/SQL is a programming language, not a scripting language. See here for a beginner's guide: https://oracle-base.com/articles/misc/introduction-to-plsql. I can't offer any more specific guidance than that since you've not provided any info on what your procedure is actually trying to do.
If you just want your script to return the data you just inserted into the table as a confirmation, just run the select statement without the procedural stuff, like this (and don't forget to commit your changes!):
create table tb_Animals (
txt_Name varchar(20) Primary Key,
int_Weight number
);
insert into tb_Animals values ('Dog',30);
insert into tb_Animals values ('Cat',15);
commit;
select * from tb_Animals;
I'm doing something very similar to dnoeth's second comment on this question:
Insert Into table Teradata dynamic stored procedure SQL
I need to run it multiple times to loop the same insert statement but with different values for the "?" and I'm not sure how to go about that.
The dynamic value in my version is a date span. I can't run a massive insert without spooling out so I've broken the data into segments.
Thanks.
William,
As you are aware that you need to use cursor to iterate through each value you want to process.
For that purpose store dynamically created INSERT statement into a variable.
Final step: Use EXECUTE IMMEDIATE command to run insert statement stored in teradata variable
Here is the working sample that you may refer
REPLACE PROCEDURE td_user.sp_dynamic_insert( OUT proc_msg VARCHAR(5000) )
BEGIN
DECLARE lv_insert_txt VARCHAR(20000);
DECLARE EXIT HANDLER FOR SQLEXCEPTION
BEGIN
/* Error handling code if required */
END;
L0:
FOR insert_cursor AS select_list
CURSOR FOR
SELECT Col2
FROM test_1
DO
SET lv_insert_txt = 'INSERT INTO test_2(Col1,Col2) VALUES('||TRIM(insert_cursor.Col2)||','||TRIM(insert_cursor.Col2)||')';
EXECUTE IMMEDIATE lv_insert_txt;
END FOR L0;
SET proc_msg = 'Procedure completed successfully';
END;
I'm trying to wirte a little log procedure for my database. I create a procedure with this statment:
create procedure prc_wirte_log (
in p_schema varchar(255),
in p_item varchar(255),
in p_message varchar(255)
)
begin
insert into weather.log (`schema`, item, message) values (p_schema, p_item, p_message);
end;
I get the error Error Code: 1064. 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 '' at line 7 0.063 sec
Why? The MySQL Workbench means Incomplet Statment: excepting ; after the insert query.
What could I do?
Multistatement procedures (assumed when BEGIN...END is present) require delimiter overrides to prevent the statements they contain from terminating the procedure definition prematurely.
Typically, you need to do something like:
DELIMITER //
CREATE PROCEDURE blah()
BEGIN
statements;
END//
DELIMITER ;
The first example on the documentation here demonstrates this (though the last two on that page seem to repeat your mistake.
If you are using WorkBench or similar tool just right click on StoredProcedures and click Create stored procedure the tool will create default structure like below and you could write your logic and hit on apply. Ensure to use semicolon at the end of the last statement (just before END).
CREATE PROCEDURE `new_procedure` ()
BEGIN
select * from tasks;
END
I have one create procedure query i m trying to execute it in Oracle Database.Below is the query :
CREATE OR REPLACE PROCEDURE TEST_PROC IS
TYPE TESTTABLE IS TABLE OF a.TEST102%ROWTYPE;
Synatax of the query seems to be fine but when i am executing it is throwing below sql exception.
Encountered the symbol "end-of-file" when expecting one of the following:
. ( # % ; not null range alter character
Trying to find out the issue trying all possible ways from past two days but i did not get any idea wheres the issue is.Can anyone please suggest what is wrong with the query???? Would be great if some one can help me out here.
Procedures must have BEGIN..END block.
Here is a procedure that does not do anything.
CREATE OR REPLACE PROCEDURE TEST_PROC
IS
TYPE TESTTABLE IS TABLE OF dual%ROWTYPE;
BEGIN
NULL;
END;
the following will work:
CREATE TABLE TEST102(id number);
CREATE OR REPLACE PROCEDURE TEST_PROC IS
TYPE TESTTABLE IS TABLE OF TEST102%ROWTYPE;
BEGIN
NULL; -- insert procedure body here
END;