Please let me know if you can provide the detail of “number of times” a procedure has been called for that particular day, for the all the valid procedures.
You can use your own logging technic. For example first you can create a table under the same schema of your desired procedure. Then after begin statement end before end statement in that invoked procedure, you can insert logs to newly created log table of yours.
CREATE TABLE SCHEMA.LOGTABLE
(
DATECOLUMN DATE DEFAULT SYSTIMESTAMP,
PROCNAME VARCHAR2 (200 CHAR),
TABLENAME VARCHAR2 (200 CHAR),
MESSAGE VARCHAR2 (1000 CHAR),
LOGSEQUENCE NUMBER
);
CREATE OR REPLACE PROCEDURE SCHEMA.PROCNAME IS
BEGIN
INSERT INTO SCHAME.LOGTABLE(DATECOLUMN,
PROCNAME,
TABLENAME,
MESSAGE,
LOGSEQUENCE)
VALUES (SYSTIMESTAMP,
'SCHEMA.PROCNAME',
'SCHEMA.TABLENAME',
'Proc STARTED',
NULL,
SCHEMA.SEQ_SISTEM_LOG.NEXTVAL);
COMMIT;
.....
INSERT INTO SCHAME.LOGTABLE(DATECOLUMN,
PROCNAME,
TABLENAME,
MESSAGE,
LOGSEQUENCE)
VALUES (SYSTIMESTAMP,
'SCHEMA.PROCNAME',
'SCHEMA.TABLENAME',
'Proc ENDED',
NULL,
SCHEMA.SEQ_SISTEM_LOG.NEXTVAL);
COMMIT;
END;
Related
I have the following procedure (insert_mapping):
create or replace procedure p_insert_mapping
(header_id in number,
position in number,
xml_mapping in varchar2,
id out number,
result_code out number
)
is
l_id number;
begin
-- check for errors
if header_id not in (select log_push_readouts_headers.id from log_push_readouts_headers) then
result_code := 9302;
raise_application_error(-RESULT_CODE, 'Foreign key constraint violated for headers');
end if;
-- if there are no errors, do insert
if result_code is null then
-- fetch sequence number
id := mapping_seq.nextval;
insert into log_push_readouts_mappings
(id, position, xml_mapping)
values
(id, position, xml_mapping);
end if;
commit;
end;
In the following line:
if header_id not in (select log_push_readouts_headers.id from log_push_readouts_headers) then
I need to check if foreign key exists in main table.
How to do that?
Could someone give me an example on how to check if foreign key is in a table with primary key?
I am getting the following error: Compilation errors for PROCEDURE
AMM_MDM.P_INSERT_MAPPING
Error: PLS-00405: subquery not allowed in this context
Line: 12
Text: if header_id not in (select log_push_readouts_headers.id from log_push_readouts_headers) then
Error: PL/SQL: Statement ignored
Line: 12
Text: if header_id not in (select log_push_readouts_headers.id from log_push_readouts_headers) then
regarding the compilation error, please refer
Using a subquery within PLSQL conditional logic; error PLS-00405
and for checking foreign key constraint on a column of a table, we can query from all_constrainsts, where CONSTRAINT_TYPE can be checked
doc ref: https://docs.oracle.com/cd/B14117_01/server.101/b10755/statviews_1037.htm#i1576022
What you can do is fetch the matching count of sub-query into a number type variable and then use that in if-else statement to compute the flow of your code
select count(1) into <some variable> from log_push_readouts_headers a where log_push_readouts_headers.id = header_id
Now check this count against your desired value in if-else statement.
i ve got some database with customer id, names, surnames. I made procedure with some parameters, which includes cursor with parameters. And i want with that cursor show customer names, surnames, id. And i want show that, where i call procedure, and i for example write as parameters in procedure for example NULL NULL, it show me all record. Next if i write NULL, Surname, it show me records with that surnames. If i write firstname,NULL, it show record only records with that first name and if i write Firstname,lastname, it show me only that record which matching this. So my procedure works fine, but i need right where clause n cursor. This is my cursor
CREATE OR REPLACE PROCEDURE test_two(c_f_name VARCHAR2,c_l_name VARCHAR2) IS
CURSOR c2(f_name VARCHAR2,l_name VARCHAR2) IS
SELECT cus_id,cus_l_name,cus_f_name
FROM CUSTOMER
WHERE (cus_f_name IS NOT NULL AND (cus_f_name = f_name orcus_l_name
IS NULL))
AND (cus_l_name IS NOT NULL AND (cus_l_name = l_name or cus_f_name IS
NULL));
v_complex c2%ROWTYPE;
lv_show VARCHAR2(20);
f_name VARCHAR2(20) := c_f_name;
l_name VARCHAR2(20) := c_l_name;
BEGIN
open c2(f_name,l_name);
LOOP
FETCH c2 INTO v_complex;
dbms_output.put_line(v_complex.cus_id|| ' ' ||v_complex.cus_f_name|| ' '
||v_complex.cus_l_name);
EXIT WHEN c2%NOTFOUND;
END LOOP;
CLOSE c2;
END;
EVERYTHING IS IFNE IN THAT PROCEDURE BUT I NEED ONLY RIGHT WHERE CLAUSE! THANK YOU
You could use a combination of LIKE and NVL.
WHERE cus_f_name LIKE NVL(c_f_name, '%')
AND cus_l_name LIKE NVL(c_l_name, '%');
Explanation:
The function NVL returns the first parameter if it is not NULL.
Otherwise it returns the second parameter.
Lets say I have a table as follows--
create table employees
(
eno number(4) not null primary key,
ename varchar2(30),
zip number(5) references zipcodes,
hdate date
);
And I'm trying to create a trigger with--
CREATE OR REPLACE TRIGGER TWELVE_ONE
BEFORE INSERT OR UPDATE
ON EMPLOYEES
FOR EACH ROW
DECLARE
V_DATE VARCHAR2 (10);
BEGIN
SELECT TO_CHAR (SYSDATE, 'hh24:mi:ss') INTO V_DATE FROM DUAL;
IF (V_DATE >= '12:00:01' AND V_DATE < '13:00:00')
THEN
INSERT INTO TABLE ?????
ELSE
ROLLBACK? TERMINATE TRANSACTION?
END IF;
END;
Purpose of the trigger is to allow an insertion/update during 12:00-13:00 and prevent the insertion at any other time. The trigger construction (thanks to #Melkikun) is seems ok. However now I'm facing the following issues--
How is it possible to pass the values here? I mean lets say my create statement is:
Insert into employees Values (1, 'someone', 11111, '17-12-2015')
And lets say the time is 12:30:01 now. How would the trigger perform the insertion without knowing the values?
And lets say the time is now 13:00:1 now. How would the trigger stop/prevent the insertion?
I'm using Oracle SQL Developer 4.02.15
Many Thanks
You just have to do it the other way.
If the time is not correct,then you raise an exception, so the insert won't be done.
CREATE OR REPLACE TRIGGER TWELVE_ONE
BEFORE INSERT OR UPDATE
ON EMPLOYEES
FOR EACH ROW
DECLARE
V_DATE VARCHAR2 (10);
MyException exception;
BEGIN
SELECT TO_CHAR (SYSDATE, 'hh24:mi:ss') INTO V_DATE FROM DUAL;
IF (V_DATE < '12:00:01' OR V_DATE > '13:00:00')
THEN
raise MyException;
END IF;
EXCEPTION
When MyException then
ROLLBACK;
//output message ...
END;
How would the trigger perform the insertion without knowing the values?
The trigger knows the value thanks to :NEW and :OLD.
You normally use the terms in a trigger using :old to reference the old value and :new to reference the new value.So you will have :NEW.eno ,:NEW.ename ...
Here is an example from the Oracle documentation :
CREATE OR REPLACE TRIGGER Print_salary_changes
BEFORE DELETE OR INSERT OR UPDATE ON Emp_tab
FOR EACH ROW
WHEN (new.Empno > 0)
DECLARE
sal_diff number;
BEGIN
sal_diff := :new.sal - :old.sal;
dbms_output.put('Old salary: ' || :old.sal);
dbms_output.put(' New salary: ' || :new.sal);
dbms_output.put_line(' Difference ' || sal_diff);
END;
In Oracle DB, I have a large select staetment with lots of joins and cases that is stored in a CLOB field in one of my tables.
How can i execute this statement from the CLOB?
Look into the EXECUTE IMMEDIATE syntax.
Example table:
CREATE TABLE test(id number, largedata clob);
INSERT INTO test VALUES (1, 'select name from v$database');
commit;
select * from test;
DECLARE
l_sql clob;
l_result VARCHAR2(50);
BEGIN
SELECT LARGEDATA INTO l_sql FROM TEST;
EXECUTE IMMEDIATE l_sql INTO l_result;
dbms_output.put_line(l_result);
END;
/
Output is the DB name.
I need to automate the following set of instructions with a pl/sql procedure:
SET DEFINE OFF;
TRUNCATE TABLE EMP.dept;
INSERT INTO EMP.dept values....;
Commit;
Also,I need to log the activity(sucess/failure) into a table EMP.Log.
Can someone help me in this ?
Depending on what the columns are in the EMP.dept table I would use a procedure defined as:
--The parameters to this procedure depend on the fields in EMP.dept
--This example assumes EMP.dept has only 2 VARCHAR fields, but
--the parameter list can easily be modified:
--
CREATE OR REPLACE PROCEDURE PROC_NAME1(INPUT1 IN VARCHAR2, INPUT2 IN VARCHAR2) AS
V_FAILURE INTEGER;
BEGIN
V_FAILURE := 0;
BEGIN
EXECUTE IMMEDIATE 'TRUNCATE TABLE EMP.dept';
INSERT INTO EMP.dept (FIELD1, FIELD2) VALUES (INPUT1, INPUT2);
COMMIT;
EXCEPTION
WHEN OTHERS THEN
V_FAILURE := 1;
ROLLBACK;
END;
BEGIN
--DEPENDING ON THE COLUMNS IN YOUR LOG_TABLE
--
INSERT INTO LOG_TABLE(STATUS, RUN_DATE)
VALUES (V_FAILURE, SYSDATE);
COMMIT;
EXCEPTION
WHEN OTHERS THEN NULL;
END;
END;