I have table p_it_people with structure as below and trigger created on it.
CREATE TABLE "P_IT_PEOPLE"
( "PERSON_ID" NUMBER NOT NULL ENABLE,
"PERSON_NAME" VARCHAR2(255) NOT NULL ENABLE,
"PERSON_EMAIL" VARCHAR2(255) NOT NULL ENABLE,
"PERSON_ROLE" VARCHAR2(30) NOT NULL ENABLE,
"USERNAME" VARCHAR2(255) NOT NULL ENABLE,
"ASSIGNED_DEPT" NUMBER,
"CREATED_ON" DATE NOT NULL ENABLE,
"CREATED_BY" VARCHAR2(255) NOT NULL ENABLE,
"MODIFIED_ON" DATE,
"MODIFIED_BY" VARCHAR2(255),
"PERSON_PASSWORD" VARCHAR2(100),
"APPROVER" VARCHAR2(50),
CONSTRAINT "P_IT_PEOPLE_PK" PRIMARY KEY ("PERSON_ID")
USING INDEX ENABLE,
CONSTRAINT "P_IT_PEOPLE_NAME_UK" UNIQUE ("PERSON_NAME")
USING INDEX ENABLE,
CONSTRAINT "P_IT_PEOPLE_USERNAME_UK" UNIQUE ("USERNAME")
Existing trigger on the table:
CREATE OR REPLACE EDITIONABLE TRIGGER "P_IT_PEOPLE_BIU"
before insert or update on p_it_people
for each row
begin
if inserting then
if :NEW.PERSON_ID is null then
:NEW.PERSON_ID := it_api.gen_pk;
end if;
:NEW.CREATED_ON := sysdate;
:NEW.CREATED_BY := nvl(v('APP_USER'),USER);
end if;
if updating then
:NEW.MODIFIED_ON := sysdate;
:NEW.MODIFIED_BY := nvl(v('APP_USER'),USER);
end if;
end;
Apart from this i want to create another trigger which would send email whenever new entry is made .
I deployed this trigger:
CREATE OR REPLACE EDITIONABLE TRIGGER "P_IT_ISSUES_AIU_NEW_PASSWORD"
AFTER
insert on P_IT_PEOPLE
for each row
DECLARE
v_person_id number;
v_username varchar2(50);
v_Email varchar2(255);
Begin
select person_id,username,person_email into v_person_id,v_username,v_email from p_it_people where person_id=v_person_id;
APEX_MAIL.SEND(
p_to => v_email,
p_from => v_email,
p_body => 'Your account has been created ' ||chr(10)||
' Username'|| v_username||chr(10)||
' Password'||v_username ,
p_subj => 'New User');
end;
Now when i try inserting row, it is throwing error-p_it_people is mutating. How can i counter this?
You are selecting from the same table as the row-level trigger is firing on - exactly what causes "table is mutating". But in this case you don't even need to. Just use the :NEW pseudo-record like this:
CREATE OR REPLACE EDITIONABLE TRIGGER "P_IT_ISSUES_AIU_NEW_PASSWORD"
AFTER
insert on P_IT_PEOPLE
for each row
Begin
APEX_MAIL.SEND(
p_to => :NEW.person_email,
p_from => :NEW.person_email ,
p_body => 'Your account has been created ' ||chr(10)||
' Username'|| :NEW.username||chr(10)||
' Password'||:NEW.username ,
p_subj => 'New User');
end;
I'm creating Update Stored Procedure in Toad. When I'm executing the query it returns me an error like this.
1/51 PLS-00103: Encountered the symbol "(" when expecting one of the following:
:= . ) , # % default character
Heres my Code:
I've tried to change the AS to IN but still same error
Create Or Replace Procedure SP_EMPLOYEE_UPDATE(P_EMPLOYEEID VARCHAR2(20 CHAR),
P_EMPLOYEENAME VARCHAR2(100 CHAR),
P_STATUS VARCHAR2(50 CHAR),
P_SUPERIORID VARCHAR2(20 CHAR),
P_MANAGERSUPERIORID VARCHAR2(20 CHAR),
P_BFG VARCHAR2(10 CHAR),
P_DATEHIRED Date,
P_DESIGNATION VARCHAR2(100 CHAR),
P_JOBGRADE NUmber,
P_EMAILADDRESS VARCHAR2(100 CHAR),
P_PRODUCTLINE VARCHAR2(80 BYTE),
P_STATION VARCHAR2(80 BYTE),
P_MACHINE VARCHAR2(80 BYTE),
P_OPERATIONGROUP VARCHAR2(80 BYTE),
P_ISACTIVE VARCHAR2(1 BYTE))
AS
Begin
Update REFEMPLOYEES set REFEMPLOYEES.EMPLOYEENAME = P_EMPLOYEENAME , REFEMPLOYEES.STATUS= P_STATUS,REFEMPLOYEES.SUPERIORID = P_SUPERIORID,REFEMPLOYEES.MANAGERSUPERIORID= P_MANAGERSUPERIORID,REFEMPLOYEES.BFG = P_BFG,
REFEMPLOYEES.DATEHIRED = P_DATEHIRED,REFEMPLOYEES.DESIGNATION = P_DESIGNATION,REFEMPLOYEES.JOBGRADE = P_JOBGRADE,REFEMPLOYEES.EMAILADDRESS = P_EMAILADDRESS,REFEMPLOYEES.PRODUCTLINE = P_PRODUCTLINE,REFEMPLOYEES.STATION = P_STATION,
REFEMPLOYEES.MACHINE = P_MACHINE,REFEMPLOYEES.OPERATIONGROUP = P_OPERATIONGROUP,REFEMPLOYEES.ISACTIVE = P_ISACTIVE
Where REFEMPLOYEES.EMPLOYEEID = P_EMPLOYEEID;
End
Your code should look like following ideally:
CREATE OR REPLACE PROCEDURE SP_EMPLOYEE_UPDATE (
P_EMPLOYEEID VARCHAR2,
P_EMPLOYEENAME VARCHAR2,
P_STATUS VARCHAR2,
P_SUPERIORID VARCHAR2,
P_MANAGERSUPERIORID VARCHAR2,
P_BFG VARCHAR2,
P_DATEHIRED DATE,
P_DESIGNATION VARCHAR2,
P_JOBGRADE NUMBER,
P_EMAILADDRESS VARCHAR2,
P_PRODUCTLINE VARCHAR2,
P_STATION VARCHAR2,
P_MACHINE VARCHAR2,
P_OPERATIONGROUP VARCHAR2,
P_ISACTIVE VARCHAR2
) AS
BEGIN
UPDATE REFEMPLOYEES
SET
REFEMPLOYEES.EMPLOYEENAME = P_EMPLOYEENAME,
REFEMPLOYEES.STATUS = P_STATUS,
REFEMPLOYEES.SUPERIORID = P_SUPERIORID,
REFEMPLOYEES.MANAGERSUPERIORID = P_MANAGERSUPERIORID,
REFEMPLOYEES.BFG = P_BFG,
REFEMPLOYEES.DATEHIRED = P_DATEHIRED,
REFEMPLOYEES.DESIGNATION = P_DESIGNATION,
REFEMPLOYEES.JOBGRADE = P_JOBGRADE,
REFEMPLOYEES.EMAILADDRESS = P_EMAILADDRESS,
REFEMPLOYEES.PRODUCTLINE = P_PRODUCTLINE,
REFEMPLOYEES.STATION = P_STATION,
REFEMPLOYEES.MACHINE = P_MACHINE,
REFEMPLOYEES.OPERATIONGROUP = P_OPERATIONGROUP,
REFEMPLOYEES.ISACTIVE = P_ISACTIVE
WHERE
REFEMPLOYEES.EMPLOYEEID = P_EMPLOYEEID;
END SP_EMPLOYEE_UPDATE;
/
Cheers!!
I want to extract data from a table on a daily basis and save it in my local machine in a excel sheet.
I want to extract data from a table on a daily basis
You could use DBMS_SCHEDULER to create a job and schedule to run daily.
For example, the below job will run every hour:
SQL> BEGIN
2 DBMS_SCHEDULER.DROP_JOB (JOB_NAME => 'test_full_job_definition');
3 END;
4 /
PL/SQL procedure successfully completed.
SQL>
SQL> BEGIN
2 DBMS_SCHEDULER.create_job (
3 job_name => 'test_full_job_definition',
4 job_type => 'PLSQL_BLOCK',
5 job_action => 'BEGIN my_job_procedure; END;',
6 start_date => SYSTIMESTAMP,
7 repeat_interval => 'freq=hourly; byminute=0; bysecond=0;',
8 end_date => NULL,
9 enabled => TRUE,
10 comments => 'Job defined entirely by the CREATE JOB procedure.');
11 END;
12 /
PL/SQL procedure successfully completed.
SQL>
SQL> SELECT JOB_NAME, ENABLED FROM DBA_SCHEDULER_JOBS where job_name ='TEST_FULL_JOB_DEFINITION'
2 /
JOB_NAME ENABL
---------------------------------------- -----
TEST_FULL_JOB_DEFINITION TRUE
SQL>
save it in my local machine in a excel sheet
This is a frequently asked question, and people often get confused between a CSV file and a Microsoft Excel file. Generating a CSV file could be as easy as to spool it using SQL*Plus. For writing the output into an Excel file, you need to do it programmatically.
You could use the following code developed by Anton Scheffer:
CREATE OR REPLACE package as_xlsx
is
/**********************************************
**
** Author: Anton Scheffer
** Date: 19-02-2011
** Website: http://technology.amis.nl/blog
** See also: http://technology.amis.nl/blog/?p=10995
**
** Changelog:
** Date: 21-02-2011
** Added Aligment, horizontal, vertical, wrapText
** Date: 06-03-2011
** Added Comments, MergeCells, fixed bug for dependency on NLS-settings
** Date: 16-03-2011
** Added bold and italic fonts
** Date: 22-03-2011
** Fixed issue with timezone's set to a region(name) instead of a offset
** Date: 08-04-2011
** Fixed issue with XML-escaping from text
** Date: 27-05-2011
** Added MIT-license
** Date: 11-08-2011
** Fixed NLS-issue with column width
** Date: 29-09-2011
** Added font color
** Date: 16-10-2011
** fixed bug in add_string
** Date: 26-04-2012
** Fixed set_autofilter (only one autofilter per sheet, added _xlnm._FilterDatabase)
** Added list_validation = drop-down
** Date: 27-08-2013
** Added freeze_pane
**
******************************************************************************
******************************************************************************
Copyright (C) 2011, 2012 by Anton Scheffer
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
******************************************************************************
******************************************** */
--
type tp_alignment is record
( vertical varchar2(11)
, horizontal varchar2(16)
, wrapText boolean
);
--
procedure clear_workbook;
--
procedure new_sheet( p_sheetname varchar2 := null );
--
function OraFmt2Excel( p_format varchar2 := null )
return varchar2;
--
function get_numFmt( p_format varchar2 := null )
return pls_integer;
--
function get_font
( p_name varchar2
, p_family pls_integer := 2
, p_fontsize number := 11
, p_theme pls_integer := 1
, p_underline boolean := false
, p_italic boolean := false
, p_bold boolean := false
, p_rgb varchar2 := null -- this is a hex ALPHA Red Green Blue value
)
return pls_integer;
--
function get_fill
( p_patternType varchar2
, p_fgRGB varchar2 := null -- this is a hex ALPHA Red Green Blue value
)
return pls_integer;
--
function get_border
( p_top varchar2 := 'thin'
, p_bottom varchar2 := 'thin'
, p_left varchar2 := 'thin'
, p_right varchar2 := 'thin'
)
/*
none
thin
medium
dashed
dotted
thick
double
hair
mediumDashed
dashDot
mediumDashDot
dashDotDot
mediumDashDotDot
slantDashDot
*/
return pls_integer;
--
function get_alignment
( p_vertical varchar2 := null
, p_horizontal varchar2 := null
, p_wrapText boolean := null
)
/* horizontal
center
centerContinuous
distributed
fill
general
justify
left
right
*/
/* vertical
bottom
center
distributed
justify
top
*/
return tp_alignment;
--
procedure cell
( p_col pls_integer
, p_row pls_integer
, p_value number
, p_numFmtId pls_integer := null
, p_fontId pls_integer := null
, p_fillId pls_integer := null
, p_borderId pls_integer := null
, p_alignment tp_alignment := null
, p_sheet pls_integer := null
);
--
procedure cell
( p_col pls_integer
, p_row pls_integer
, p_value varchar2
, p_numFmtId pls_integer := null
, p_fontId pls_integer := null
, p_fillId pls_integer := null
, p_borderId pls_integer := null
, p_alignment tp_alignment := null
, p_sheet pls_integer := null
);
--
procedure cell
( p_col pls_integer
, p_row pls_integer
, p_value date
, p_numFmtId pls_integer := null
, p_fontId pls_integer := null
, p_fillId pls_integer := null
, p_borderId pls_integer := null
, p_alignment tp_alignment := null
, p_sheet pls_integer := null
);
--
procedure hyperlink
( p_col pls_integer
, p_row pls_integer
, p_url varchar2
, p_value varchar2 := null
, p_sheet pls_integer := null
);
--
procedure comment
( p_col pls_integer
, p_row pls_integer
, p_text varchar2
, p_author varchar2 := null
, p_width pls_integer := 150 -- pixels
, p_height pls_integer := 100 -- pixels
, p_sheet pls_integer := null
);
--
procedure mergecells
( p_tl_col pls_integer -- top left
, p_tl_row pls_integer
, p_br_col pls_integer -- bottom right
, p_br_row pls_integer
, p_sheet pls_integer := null
);
--
procedure list_validation
( p_sqref_col pls_integer
, p_sqref_row pls_integer
, p_tl_col pls_integer -- top left
, p_tl_row pls_integer
, p_br_col pls_integer -- bottom right
, p_br_row pls_integer
, p_style varchar2 := 'stop' -- stop, warning, information
, p_title varchar2 := null
, p_prompt varchar := null
, p_show_error boolean := false
, p_error_title varchar2 := null
, p_error_txt varchar2 := null
, p_sheet pls_integer := null
);
--
procedure list_validation
( p_sqref_col pls_integer
, p_sqref_row pls_integer
, p_defined_name varchar2
, p_style varchar2 := 'stop' -- stop, warning, information
, p_title varchar2 := null
, p_prompt varchar := null
, p_show_error boolean := false
, p_error_title varchar2 := null
, p_error_txt varchar2 := null
, p_sheet pls_integer := null
);
--
procedure defined_name
( p_tl_col pls_integer -- top left
, p_tl_row pls_integer
, p_br_col pls_integer -- bottom right
, p_br_row pls_integer
, p_name varchar2
, p_sheet pls_integer := null
, p_localsheet pls_integer := null
);
--
procedure set_column_width
( p_col pls_integer
, p_width number
, p_sheet pls_integer := null
);
--
procedure set_column
( p_col pls_integer
, p_numFmtId pls_integer := null
, p_fontId pls_integer := null
, p_fillId pls_integer := null
, p_borderId pls_integer := null
, p_alignment tp_alignment := null
, p_sheet pls_integer := null
);
--
procedure set_row
( p_row pls_integer
, p_numFmtId pls_integer := null
, p_fontId pls_integer := null
, p_fillId pls_integer := null
, p_borderId pls_integer := null
, p_alignment tp_alignment := null
, p_sheet pls_integer := null
);
--
procedure freeze_rows
( p_nr_rows pls_integer := 1
, p_sheet pls_integer := null
);
--
procedure freeze_cols
( p_nr_cols pls_integer := 1
, p_sheet pls_integer := null
);
--
procedure freeze_pane
( p_col pls_integer
, p_row pls_integer
, p_sheet pls_integer := null
);
--
procedure set_autofilter
( p_column_start pls_integer := null
, p_column_end pls_integer := null
, p_row_start pls_integer := null
, p_row_end pls_integer := null
, p_sheet pls_integer := null
);
--
function finish
return blob;
--
procedure save
( p_directory varchar2
, p_filename varchar2
);
--
procedure query2sheet
( p_sql varchar2
, p_column_headers boolean := true
, p_directory varchar2 := null
, p_filename varchar2 := null
, p_sheet pls_integer := null
);
Note : It is a huge code with more than 90k PL/SQL lines of code, so I can't post the entire code here due to restriction of 30k characters. Only posting the important procedures, functions and the package details. Please follow the code link to get the entire code.
the least demanding way would be to daly insert table data into some temp table by some scheduled job
create table myexportable as (select sysdate exportdate, q.* from mytable q)
resp.
insert into myexportable (select sysdate exportdate, q.* from mytable q)
and then export that data using SQL Plus
set serveroutput on;
set linesize 10000;
set pagesize 10000;
set heading off;
set feed 0;
set trimspool on;
set colsep '|';
spool exptable.out;
select * from myexportable where trunc(exportdate) = trunc(sysdate)
/
quit;
and them at least import pipe delimited text file into excel.
I was trying to run the following script:
SET serveroutput ON
DECLARE
v_nbr_part NUMBER := 2;
v_nbr_subpart NUMBER := 2;
v_table_name VARCHAR2(100) := 'XPTO';
v_table_shortname VARCHAR2(100) := 'XPTO';
v_part_first NUMBER;
v_part_last NUMBER;
v_subpart_first NUMBER;
v_subpart_last NUMBER;
v_sql CLOB := '';
v_sql_part CLOB := '';
v_sql_subpart CLOB := '';
v_part_dynam CLOB := '';
v_part_keys CLOB := '';
BEGIN
IF v_nbr_part > 0 THEN
IF v_nbr_subpart > 0 THEN
v_subpart_first := 0;
v_subpart_last := v_nbr_subpart-1;
v_part_dynam:='';
FOR i IN v_subpart_first .. v_subpart_last
LOOP
v_part_dynam := v_part_dynam || ' SUBPARTITION SP' || i ||' VALUES ('||i||')';
IF i <> v_subpart_last THEN
v_part_dynam := v_part_dynam || ',';
END IF;
END LOOP;
v_sql_subpart:= where_to_save('LDR_SUBTYPE', v_part_dynam);
v_part_keys:= ' , "LDR_SUBTYPE" NUMBER NOT NULL ENABLE ';
END IF;
v_part_first := 1;
v_part_last := v_nbr_part;
v_sql_part:= to_clob(' PARTITION BY RANGE (PART_KEY) interval(numtodsinterval(1,''day'')) ')|| v_sql_subpart ||
to_clob(' ( partition EMPTY values less than (to_date(''01-01-2013'',''DD-MM-YYYY'') ) ) ');
v_part_keys:= ', "PART_KEY" TIMESTAMP (6) NOT NULL ENABLE ' || v_part_keys;
END IF;
v_sql:=to_clob(
'CREATE OR REPLACE TABLE "'||v_table_name||'" (
"SEQUENCENUMBER" NUMBER NOT NULL ENABLE,
"LDRID" VARCHAR2(100),
"LDRTYPE" VARCHAR2(50),
"LDR_SUBTYPE" VARCHAR2(50),
"CREATIONTIMESTAMP" TIMESTAMP (6),
"CORRELATIONID" VARCHAR2(4000),
"PARENTLDRID" VARCHAR2(100),
"NUMBEROFCHILDREN" NUMBER,
"PROTOCOLSESSIONID" VARCHAR2(100),
"ACTIVITYID" VARCHAR2(100),
"ACTIVITYINITIALTIMESTAMP" TIMESTAMP (6),
"ACTIVITYELAPSEDTIME" NUMBER,
"CANCELED" CHAR(1),
"REPLACED" CHAR(1),
"INVALIDATIONTIMESTAMP" TIMESTAMP (6),
"INVALIDATEDBYLDRID" VARCHAR2(100),
"INVALIDATESLDRID" VARCHAR2(100),
"OPERATIONID" VARCHAR2(256),
"ACCOUNTID" VARCHAR2(100),
"TOPLEVELID" VARCHAR2(100),
"RESULTCODE" VARCHAR2(100),
"VERSION" NUMBER,
"LDROBJECT" BLOB,
"OBJMETADATA" BLOB,
"OBJKEY" BLOB,
"OBJKEYHASH" VARCHAR2(200) ') ||
v_part_keys ||
to_clob('
)
LOB (OBJKEY) STORE AS SECUREFILE
LOB (LDROBJECT) STORE AS SECUREFILE
LOB (OBJMETADATA) STORE AS SECUREFILE
') ||
v_sql_part || to_clob(',TABLESPACE OCS LOGGING NOCOMPRESS NOCACHE NOPARALLEL');
dbms_output.put_line(dbms_lob.substr(v_sql,4000,1)); //this is line 84!!!!!!!!!
EXECUTE IMMEDIATE v_sql;
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.Put_Line(DBMS_UTILITY.Format_Error_Backtrace);
DBMS_OUTPUT.Put_Line(SQLERRM);
END;
/
CREATE OR REPLACE FUNCTION where_to_save(LDR_SUBTYPE VARCHAR2, v_part_dynam OUT CLOB)
RETURN CLOB
IS
BEGIN
CASE
WHEN LDR_SUBTYPE = ('IEC') THEN v_part_dynam := v_part_dynam || 'SUBPARTITION SP0 VALUES (IEC)';
ELSE v_part_dynam := v_part_dynam || 'SUBPARTITION SP1 VALUES (OTHER)';
END CASE;
RETURN v_part_dynam;
END where_to_save;
The script look good to me, but every time I try to run it I get the following errors:
ORA-06512: line 84
ORA-00922: missing or invalid option
I've searched a lot and the only thing I've found about fixing this was "contact your DBA".
What is wrong with my script? How can I fix this?
P.S.: I've commented line 84 !
Tables can't be created or replaced, try dropping it first or build a pl that drops table if exists.
'CREATE TABLE "'||v_table_name||'" (
Create or replace table
Create or replace view --> yes
but not create or replace table. This Option does not exist.
I have oracle stored procedure where I pass an input parameter and get a cursor as an output parameter:
CREATE OR REPLACE PROCEDURE OS_DIVISIONDDL(
p_dept_name IN VARCHAR2,
p_cursor_division OUT SYS_REFCURSOR
) IS
tmpVar NUMBER;
BEGIN
tmpVar := 0;
OPEN p_cursor_division FOR
select Division_code,
Division_name
from DIVISION_TBL
where dept_code=p_dept_name;
EXCEPTION
WHEN NO_DATA_FOUND THEN
NULL;
WHEN OTHERS THEN
-- Consider logging the error and then re-raise
RAISE;
END OS_DIVISIONDDL;
/
The input parameter I am able to see using dbms print, but where condition is not executed by server Am i going wrong anywhere
DIVISION_CODE CHAR (10 Char)
DIVISION_NAME VARCHAR2 (30 Char)
DEPT_CODE CHAR (10 Char)
COMPANY_CODE CHAR (3 Char)
LOCATION_CODE CHAR (3 Char)
CREATED_DATE TIMESTAMP(6)
CREATED_BY VARCHAR2 (50 Char)
MODIFIED_DATE TIMESTAMP(6)
MODIFIED_BY VARCHAR2 (50 Char)
Perhaps you have a problem of implicit convertions:
declare
a char(10) := '123';
b varchar2(100) := '123';
dummy integer;
begin
select null
into dummy
from dual
where a = b;
end;
this code raises "no data found" but looks totally the same.
The problem here is that char(10) is not equal "123" but equal to "123<7 spaces here>". And varchar2(100) remains "123".
Check for char and varchar2 differences e.g. in Oracle docs