Procedure for truncate and insert in sql - sql

I have a table- Event_name.
select * from Event_name
I have to truncate the data and insert fresh data daily.
Can someone tell me how to write a stored procedure for truncating and inserting data into table-Event_name?

The generic approach is
Create procedure proc_name
as
Begin
Truncate table Event_name;
insert into Event_name(col_list)
select col_list from source_table;
End;

Try this:
create or replace procedure myProcedure
AS
BEGIN
execute immediate 'truncate table Event_name';
insert into Event_name select * from Event_name;
END;

Related

Using Alter Statement from a Table in oracle

I have a table like having one column containing this data
Dummy Column
Alter PACKAGE ABC COMPILE;
Alter PACKAGE CDE COMPILE;
Alter PROCEDURE ABC COMPILE;
Alter TRIGGER ABC COMPILE;
I want to make a script such that when i run that it will execute the alter statements line by line and perform the DDL operations.
Something like this should work, obviously assuming you have real DDL statements stored in that dummy column.
for loop over the column when the value is not null
replace the ; by nothing in order to use execute immediate
Example
declare
vsql table.dummy_column%type;
begin
for h in ( select dummy_column from table where dummy_column is not null )
loop
vsql := replace(h.dummy_column,';','');
execute immediate vsql;
end loop;
end;
/

TEMP table in stored procedure oracle 11g

Is there any way to create a temp table with the stored procedure and use the same in the ref cursor in the same stored procedure.
I wrote something like below, it's not working....
CREATE OR REPLACE PROCEDURE USP_TEST(
CUR_QUOTE OUT SYS_REFCURSOR) AS
BEGIN
CREATE GLOBAL TEMPORARY TABLE users1 ON COMMIT PRESERVE ROWS
AS
SELECT 'rb#bot.com' FROM DUAL;
OPEN CUR_QUOTE FOR
SELECT DISTINCT CREATEDBY
FROM QUOTE
WHERE TRUNC(DATEOFENQUIRY)=TRUNC(SYSDATE-1) AND CREATEDBY = users1.EMAIL;
END;
And delete the temp table at the end.
Please suggest with some sample code...
Keep coding :)
If you drop the table then the cursor is invalidated.
From 18c you can use private temporary tables:
create or replace procedure usp_test
( cur_quote out sys_refcursor )
as
begin
execute immediate
'create private temporary table ora$ptt_demo' ||chr(10)||
'on commit drop definition as' ||chr(10)||
'select sysdate -1 as dateofenquiry, ''rb#bot.com'' as createdby' ||chr(10)||
'from dual';
open cur_quote for
'select distinct createdby from ora$ptt_demo where trunc(dateofenquiry) = trunc(sysdate - 1)';
end;
Note that the table name must have the prefix defined by the PRIVATE_TEMP_TABLE_PREFIX parameter (default ORA$PTT_), and you must commit before calling the procedure a second time.

how to use trigger in sql

CREATE TABLE table_001(
Day_date date
);
CREATE TABLE table_002(
new_Day_date date
);
CREATE OR REPLACE TRIGGERS trigger
AFTER INSERT ON table_001
FOR EACH ROW
BEGIN
INSERT INTO table_002 VALUES(SYSDATE)
END;
You have a few syntax errors. It's not "TRIGGERS". Also, give a valid trigger name. A semicolon was missing after the insert.
CREATE OR REPLACE TRIGGER trigger_name
AFTER INSERT ON table_001
FOR EACH ROW
BEGIN
INSERT INTO table_002(new_Day_date) VALUES(SYSDATE);
END;
/
CREATE OR REPLACE TRIGGER trigger_name
AFTER INSERT ON table_001
FOR EACH ROW
BEGIN
INSERT INTO table_002 VALUES(SYSDATE);
END;

Can't create a table and select from it while in a plsql procedure in oracle

create or replace procedure sp_test as
begin
CREATE TABLE T AS SELECT col1,col2 FROM t1;
FOR N IN (SELECT * FROM T) LOOP
UPDATE t1 SET t1.col1='value' where col2='value2';
END LOOP;
drop table T;
end;
/
I need to select data into t table from a t1 table in order to apply some modifications, and merge those modifications in t1 table (origin table) before deleting table t.
I m getting this error : PLS-00103
You need to use execute immediate for any ddl inside any pl/sql block. Try the below code:
create or replace procedure sp_test
is
begin
Execute Immediate 'CREATE TABLE T AS SELECT col1,col2 FROM t1';
FOR N IN (SELECT * FROM T) LOOP
Execute immediate 'UPDATE t1 SET t1.col1=''value'' where col2=''value2''';
END LOOP;
execute immediate 'drop table T';
end;
/
Thanks everybody for your contribution, I tried something that works :
A cursor for update.
Thanks for help :)

Stored Procedure: only commit when successful

I would like to build a stored procedure that:
1. truncates Table A
2. truncates Table B
3. inserts (lots) of rows in table A
4. inserts (lots) of rows in table B
The stored procedure should only commit the statements after step 4 so that the tables are not locked and experience no down time.
If an error occurs (for instance in step 4) all changes must be rolled back. I tried writing it myself but it committed after each statement.
create or replace PROCEDURE upall as
BEGIN
execute immediate 'truncate table MAIN.SET';
insert into MAIN.SET select * from MAIN.SET_STAG;
execute immediate 'truncate table MAIN.TYPE';
insert into MAIN.TYPE select * from MAIN.TYPE_STAG;
COMMIT;
EXCEPTION WHEN OTHERS THEN
ROLLBACK;
RAISE;
END;
In Oracle, TRUNCATE TABLE is a DDL statement that cannot be used in a transaction (or, more accurately, cannot be rolled back).If there is a transaction in progress when the statement is executed, the transaction is committed and then the TRUNCATE is executed and cannot be undone.
Try DELETE FROM YourTable and finally update stats of your table (since DELETE will outdate it)
It will looks like :
CREATE or REPLACE PROCEDURE upall as
BEGIN
delete from MAIN.SET;
insert into MAIN.SET select * from MAIN.SET_STAG;
delete from MAIN.TYPE;
insert into MAIN.TYPE select * from MAIN.TYPE_STAG;
COMMIT;
EXEC DBMS_STATS.GATHER_TABLE_STATS ('MAIN', 'SET');
EXEC DBMS_STATS.GATHER_TABLE_STATS ('MAIN', 'TYPE');
EXCEPTION WHEN OTHERS THEN
ROLLBACK;
RAISE;
END;