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;
/
Related
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 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;
/
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 does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 1 year ago.
Improve this question
Explanation with example of syscursor.
When to use syscursor in plsql procedure? I have go through websites but didn't get how and when to use this.
SYS_REFCURSOR are used by PL/SQL procedures to return recordsets.
Example - lets get all table details for a given schema and return through sys_refcursor
CREATE OR REPLACE PROCEDURE GET_TABLE_DETAILS(schemaName IN VARCHAR2,
table_details OUT SYS_REFCURSOR) IS
BEGIN
OPEN table_details FOR
select table_name, column_name, data_type from ALL_TAB_COLUMNS where OWNER = schemaName;
END GET_TABLE_DETAILS;
Here table_details out parameter will contain the result data of the select query, it can be retrieved as below.
DECLARE
table_details_cursor SYS_REFCURSOR;
tab_name ALL_TAB_COLUMNS.table_name%TYPE;
col_name ALL_TAB_COLUMNS.column_name%TYPE;
data_type ALL_TAB_COLUMNS.data_type%TYPE;
BEGIN
GET_TABLE_DETAILS (schemaName => 'DUMMY',
table_details => table_details_cursor);
LOOP
FETCH table_details_cursor
INTO tab_name, col_name, data_type;
EXIT WHEN table_details_cursor%NOTFOUND;
DBMS_OUTPUT.PUT_LINE(tab_name || ' | ' || col_name || ' | ' || data_type);
END LOOP;
CLOSE table_details_cursor;
END;
However, you need to get through the Oracle documentation for detailed explanation - Oracle - Cursors Documenatation
As far as I know there is nothing like "syscursor" in Oracle. May be you are referring to SYS_REFCURSOR
The following url should help you in understanding how and when to use it.
https://oracle-base.com/articles/misc/using-ref-cursors-to-return-recordsets
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
the following trigger code applies a discount to the bill after a certain amount of visits but i'm getting the following error PLS-00103: Encountered the symbol "=" when expecting one of the following: := . ( # % ; indicator
CREATE OR REPLACE TRIGGER CHECK_DISCOUNT
BEFORE INSERT OR UPDATE OF C_NO,BILL ON APPOINTMENT
FOR EACH ROW
DECLARE
CURSOR C_APPTMNT
IS
SELECT C_NO,COUNT(C_NO)
FROM APPOINTMENT GROUP BY C_NO;
V_C_NO APPOINTMENT.C_NO%TYPE;
VISIT NUMBER(2);
V_TEN NUMBER(3):=0.9;
BEGIN
OPEN C_APPTMNT;
FETCH C_APPTMNT INTO V_C_NO,VISIT;
IF VISITS = 3 AND :NEW.C_NO = V_C_NO THEN
:NEW.BILL := :NEW.BILL * V_TEN
END IF;
END;
/
Getting a new error
PLS-00103: Encountered the symbol "END" when expecting one of the
following:
. ( * # % & = - + ; < / > at in is mod not rem
<> or != or ~= >= <= <> and or like
between ||
The symbol ";" was substituted for "END" to continue.
Assignment needs a :
:NEW.BILL := :NEW.BILL * V_TEN;