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;
Related
Hello fellow programmers and happy new year to you all!
I have few university tasks for winter break and one of them is to create trigger on table:
PERSON(ID, Name, Surname, Age);
Trigger is supposed to inform user when they have inserted row with invalid ID. Vadility criteria is that ID is 11 digits long.
I tried to write solution like this:
CREATE OR REPLACE TRIGGER person_id_trigg
AFTER INSERT
ON person
DECLARE
idNew VARCHAR(50);
lengthException EXCEPTION;
BEGIN
SELECT id INTO idNew FROM INSERTED;
IF LENGTH(idNew) <> 11 THEN
RAISE lengthException;
END IF;
EXCEPTION
WHEN lengthException THEN
dbms_output.put_line('ID for new person is INVALID. It must be 11 digits long!');
END;
Then I realized that INSERTED exists only in sqlserver and not in oracle.
What would you suggest I could do to fix that?
Thanks in advance!
Do you want to raise an exception (which would prevent the insert from succeeding)? Or do you want to allow the insert to succeed and write a string to the dbms_output buffer that may or may not exist and may or may not be shown to a human running the insert?
In either case, you'll want this to be a row-level trigger, not a statement-level trigger, so you'll need to add the for each row clause.
CREATE OR REPLACE TRIGGER person_id_trigg
AFTER INSERT
ON person
FOR EACH ROW
If you want to raise an exception
BEGIN
IF( length( :new.id ) <> 11 )
THEN
RAISE_APPLICATION_ERROR( -20001,
'The new ID value must have a length of 11' );
END IF;
END;
If you want to potentially print output but allow the insert to succeed
BEGIN
IF( length( :new.id ) <> 11 )
THEN
dbms_output.put_line( 'The new ID value must have a length of 11' );
END IF;
END;
Of course, in reality, you would never use a trigger for this sort of thing. In the real world, you would use a constraint.
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 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 does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 9 years ago.
Improve this question
I am working on some procedures to insert a new customer. This is my code:
CREATE OR REPLACE PROCEDURE ADD_CUST
(
P_CUST_ID CUST_INFO.CUST_ID%TYPE,
P_CUST_F_NAME CUST_INFO.CUST_F_NAME%TYPE,
P_CUST_L_NAME CUST_INFO.CUST_L_NAME%TYPE,
P_CUST_ADDRESS CUST_INFO.CUST_ADDRESS%TYPE,
P_CITY CUST_INFO.CITY%TYPE,
P_STATE CUST_INFO.STATE%TYPE,
P_ZIP CUST_INFO.ZIP%TYPE,
P_PHONE CUST_INFO.PHONE%TYPE
)
IS
BEGIN
INSERT INTO CUST_INFO (
CUST_ID, CUST_F_NAME, CUST_L_NAME, CUST_ADDRESS, CITY,STATE, ZIP, PHONE)
VALUES (
P_CUST_ID, P_CUST_F_NAME, P_CUST_L_NAME, P_CUST_ADDRESS, P_CITY, P_STATE, P_ZIP,P_PHONE
)
END ADD_CUST;
/
This is my cust_info table:
CREATE TABLE CUST_INFO
(
CUST_ID NUMBER(15),
CUST_F_NAME VARCHAR(20),
CUST_L_NAME VARCHAR(20),
CUST_ADDRESS VARCHAR(40),
CITY VARCHAR(30),
STATE VARCHAR(30),
ZIP NUMBER,
PHONE VARCHAR(12),
PRIMARY KEY (CUST_ID)
);
I am getting errors which are not very useful at least for me.
LINE/COL ERROR
15/1 PL/SQL: SQL Statement ignored
20/7 PL/SQL: ORA-00933: SQL command not properly ended
20/19 PLS-00103: Encountered the symbol "end-of-file" when expecting
one of the following:
( begin case declare end exception exit for goto if loop mod
null pragma raise return select update while with
<< continue close current delete fetch lock
insert open rollback savepoint set sql execute commit forall
merge pipe purge
Thank you.
CREATE OR REPLACE PROCEDURE ADD_CUST
(
P_CUST_ID CUST_INFO.CUST_ID%TYPE,
P_CUST_F_NAME CUST_INFO.CUST_F_NAME%TYPE,
P_CUST_L_NAME CUST_INFO.CUST_L_NAME%TYPE,
P_CUST_ADDRESS CUST_INFO.CUST_ADDRESS%TYPE,
P_CITY CUST_INFO.CITY%TYPE,
P_STATE CUST_INFO.STATE%TYPE,
P_ZIP CUST_INFO.ZIP%TYPE,
P_PHONE CUST_INFO.PHONE%TYPE
)
IS
BEGIN
INSERT INTO CUST_INFO (
CUST_ID, CUST_F_NAME, CUST_L_NAME, CUST_ADDRESS, CITY,STATE, ZIP, PHONE)
VALUES (
P_CUST_ID, P_CUST_F_NAME, P_CUST_L_NAME, P_CUST_ADDRESS, P_CITY, P_STATE, P_ZIP,P_PHONE
);
END ADD_CUST;
/
Just removed the brackets in between, possibly a copy paste issue; Time for a break I believe!
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Autoincrement in Oracle
I have a table in oracle and I want to make userid auto increment..
Once I make it auto increment by using sequence and try to insert into table using procedure where do i put my sample.seq_userid
How do i insert userid? do i have to declare it in my procedure?
PROCEDURE insertExample
(
name_in IN sample.name%TYPE,
age_in IN sample.age%TYPE
)
IS
BEGIN
INSERT INTO sample
(name, age)
VALUES
(name_in, age_in);
END insertExample;
here is my update one
PROCEDURE updateExample
(
userid_in IN sample.userid%TYPE,
name_in IN sample.name%TYPE,
age_in IN sample.age%TYPE
)
IS
BEGIN
UPDATE sample
SET name = name_in,
age = age_in
WHERE userid = userid_in;
END updateExample;
you need a sequence:
create sequence seq_user_id start with 1 increment by 1;
and a trigger on a table
CREATE TRIGGER user_id_trg
BEFORE insert
ON sample
FOR EACH ROW
BEGIN
SELECT seq_user_id.NEXTVAL INTO :new.user_id FROM dual;
END;
/