Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
how to do the equivalent code in ORACLE
The code is made for Postgresql.
CREATE FUNCTION emp_stamp() RETURNS trigger AS emp_stamp
BEGIN
-- BODY
END;
$emp_stamp$ LANGUAGE plpgsql;
CREATE TRIGGER emp_stamp BEFORE INSERT OR UPDATE ON emp
FOR EACH ROW EXECUTE PROCEDURE emp_stamp();
I don't know Postresql. If you could explain what this piece of code does, it might be easier to assist. Meanwhile, see if this example helps.
Function accepts employee's EMPNO and returns its MAX salary from the salaries table (doesn't make much sense, but that replaces your --body comment).
create or replace function emp_stamp (par_empno in emp.empno%type)
return emp.sal%type
is
retval emp.sal%type;
begin
select max(sal)
into retval
from salaries
where empno = par_empno;
return retval;
end;
/
Trigger calls that function and sets employee's emp.sal column to value returned by the function.
create or replace trigger trg_emp_stamp
before insert or update on emp
for each row
begin
:new.sal := emp_stamp(:new.empno);
end;
/
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I am unable to do this assignment. I am new in this field. Please help.
That would be something like this:
set serveroutput on
declare
l_publisherid publisher.publisherid%type;
begin
-- 1: how is PUBLISHER_ID supposed to get its value?
-- Note that "MAX + 1" is a **bad idea** in a multiuser
-- environment, so I just hoped that today's date fits
l_publisherid := 10102020;
insert into publisher (publisher_id, publisher_name)
values (l_publisherid, 'TTK Publisher');
-- 2:
update book b set
b.publisherid = l_publisherid
where b.publisherid is null;
-- 3:
dbms_output.put_line('BOOKID...TITLE.....PUBLISHER....AUTHOR');
for cur_r in (select b.bookid,
substr(b.title, 1, 10) title,
p.publishername,
a.firstname ||' '|| a.lastname author
from book b join publisher p on p.publisherid = b.publisherid
join author a on a.authorid = b.author_id
where p.publisherid = l_publisherid
)
loop
dbms_output.put_line
(cur_r.book_id ||'.....'||
cur_r.title ||'.....'||
cur_r.publishername ||'.....'||
cur_r.author
);
end loop;
end;
/
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I have a compilation error in my PL/SQL code, The audit_client table will be updated if any update or delete occurs on client_master
create or replace trigger t1
after update or delete on client_master
for each row
DECLARE
v1 varchar2(10);
v2 varchar2(80);
v3 number(11);
oper varchar2(15);
BEGIN
v1:= :old.CLIENT_NO
v2:= :old.NAME
v3:= :old.BALANCE
if updating then
oper:='update'
insert into audit_client values(v1,v2,v3,oper,v4,v5);
end if;
if deleting then
oper:='delete'
insert into audit_client values(v1,v2,v3,oper,v4,v5);
end if;
end;
/
Why would you declare those v variables at all? Somewhat simplified:
create or replace trigger t1
after update or delete on client_master
for each row
declare
l_oper varchar2(15);
begin
if updating then
l_oper := 'update';
elsif deleting then
l_oper := 'delete';
end if;
insert into audit_client
(client_no, name, balance, oper, col4, col5)
values
(:old.client_no, :old.name, :old.balance, l_oper, null, null);
end;
/
Instead of v4 and v5 I inserted NULL as you never said what should be put in there.
Also, as a good practice, you should always specify column list you're working with. That's why I made up those column names as I don't know their real names, but you do and you should use them.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
Hey Im Looking at this Triggers for hours now can someone help me ?
These Triggers Are for a Database appenrently . I made sure all those tables exist.S
create or replace TRIGGER postleizahl_check
BEFORE
INSERT or UPDATE on Kunden
FOR EACH ROW
BEGIN
DECLARE PLZ varchar(5) ;
SET PLZ := (Select PLZ from PLZVERZEICHNIS where Ort = new.Ort) ;
if :new.Postleitzahl = PLZ then
INSERT INTO Kunden (KUNDEN_NUMMER,NACHNAME, VORNAME, STRAßE , POSTLEITZAHL, HAUSNUMMER,ORT )
VALUES (new.KUNDEN_NUMMER,new.NACHNAME,new.VORNAME, new.STRAßE ,new.POSTLEITZAHL, new.HAUSNUMMER,new.ORT );
else
RAISE_APPLICATION_ERROR
(-20001, 'PLZ Exestiert nicht. ');
end if ;
end;
CREATE OR REPLACE TRIGGER nachbestellen
AFTER INSERT ON Bestellungen
For Each Row
BEGIN
if Bestellungen.Datum = GETDATE THEN
INSERT INTO LIEFERANTENBESTELLUNG(Artikel_Nummer,LIEFERANTEN_NUMMER,DATUM,Artikel_Name)
VALUES(new.Artikel_Nummer,new.Artikel_Namen,new.DATUM,new.LIEFERANTEN_NUMMER);
END IF ;
END;
Your declare statement must come before your begin, and 'set' is a SQL*Server construct, not Oracle. All of your 'new' should be prefixed with a colon (:);
Additional comment: varchar is a valid type, but in Oracle you should be using varchar2. Currently they work the same way in Oracle, but Oracle reserves the right to change it in the future, most likely to conform to the ANSI standard for varchar.
CREATE OR REPLACE TRIGGER postleizahl_check
BEFORE INSERT OR UPDATE
ON kunden
FOR EACH ROW
DECLARE
plz VARCHAR (5);
BEGIN
plz := (SELECT plz
FROM plzverzeichnis
WHERE ort = :new.ort);
IF :new.postleitzahl = plz
THEN
INSERT INTO kunden (
kunden_nummer, nachname, vorname
, straße, postleitzahl, hausnummer
, ort
)
VALUES (
:new.kunden_nummer, :new.nachname, :new.vorname
, :new.straße, :new.postleitzahl, :new.hausnummer
, :new.ort
);
ELSE
raise_application_error (-20001, 'PLZ Exestiert nicht. ');
END IF;
END;
CREATE OR REPLACE TRIGGER nachbestellen
AFTER INSERT
ON bestellungen
FOR EACH ROW
BEGIN
IF bestellungen.datum = getdate
THEN
INSERT INTO lieferantenbestellung (
artikel_nummer, lieferanten_nummer, datum
, artikel_name
)
VALUES (
:new.artikel_nummer, :new.artikel_namen, :new.datum
, :new.lieferanten_nummer
);
END IF;
END;
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I'm looking for a way to return one case-insensitive varchar2 from a table given as a parameter.
The database is configured to have as the first column the one I'll look into.
This is what I got to:
create or replace function generic_return(nam varchar2, table_n varchar2)
return varchar2
as
case_insensitive varchar2(300);
case_ins varchar2(300);
colu varchar2(30);
t_nam varchar2(30):=upper(table_n);
cursor point is execute inmediate select colu from t_nam;
cursor col is select column_name from cols where table_name=t_nam;
non_existent_table exception;
begin
case_ins:=upper(rtrim(ltrim(nam)));
open col;
fetch col into colu;
close col;
select column_name from cols where table_name=upper(table_n);
if colu is null then
raise non_existent_table;
end if;
open point;
loop
fetch point into case_insensitive;
exit when point%notfound;
if upper(case_insensitive)=case_ins then
return case_insensitive;
end if;
end loop;
close point;
return null;
end;
/
The function receives what to look for, and the name of the table to look into, then the first variables are to compare them, t_nam is to use the uppercased version of the second parameter... and from that there's the huge mess: I get the column name from the table in col, from cols, and then I try to make a cursor that goes around checking if what the tuple, going by the same modifications, is the first parameter, or not, if it happens to be, then it should return the unmodified version of the one in the table. Otherwise, it should return "null", which I'll treat differently on each procedure that uses it.
By null I mean nothingness, not the string.
Yes, you are right. That's a pretty huge mess. :-)
For starters, you cannot declare an explicit cursor as an execute immediate statement.
Next, if you want the first column in the table, you need to specify
WHERE column_id = 1
and then you can just grab it via a SELECT-INTO, no need for an explicit cursor.
Then you could try something like:
my_cur SYS_REFCURSOR;
BEGIN
...
OPEN my_cur FOR 'SELECT ' || first_col || ' FROM ' || t_name;
LOOP
FETCH my_cur INTO case_insensitive;
EXIT WHEN my_cur%NOTFOUND;
... logic for match ....
END LOOP;
CLOSE my_cur;
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
Create or replace function get_empsalary_by dept(emp_dept varchar(20))
return number is
Total_salary number(10,2);
emp_dept varchar(20);
emp_salary number(10,2);
begin
select empdept, sum(empsalary) into emp_dept,emp_salary from employe where empdept=emp_dept;
total_salary :=sum(empsalary);
return total_salary;
end;
/
And the error is:
-----------------------------------------------------------------
PLS-00103: Encountered the symbol "DEPT" when expecting one of
the following:
( return compress compiled wrapped
Create or replace function get_empsalary_by_dept(p_emp_dept in employe.empdept%type)
return number
is
Total_salary number(10,2);
emp_dept varchar(20);
emp_salary number(10,2);
begin
select empdept, sum(empsalary)
into emp_dept,emp_salary
from employe
where empdept=p_emp_dept
group by empdept;
-- total_salary := emp_salary;
return emp_salary;
end;
/
Maybe that this can help you any further.
The name of your function cannot contain any spaces. The input parameter (emp_dept) can not be the same as a variable. The variable emp_dept you do not need in this case. The return value can be emp_salary.
Your function is called "get_empsalary_by dept" (there is a space between "by" and "dept") and so the parser complains because it expects after function a valid (i.e. without spaces) function name: instead it finds two strings.