I have a number of triggers which do various things, but at the top of all of those which have unique integer primary keys, I need them to auto increment. The code currently looks as follows:
IF :NEW.student_id IS NULL THEN
SELECT seq_student_id.nextval
INTO :NEW.student_id
FROM sys.dual;
END IF;
I thought it may be useful to make a small function for this, as I would if this were a conventional programming language. However, I'm unsure how to pass the arguments to where they need to go to allow the function to run correctly. Ideally, I'd just like to be able to call AUTO_INCREMENT(student_id) at the top, and have something like the following run (does not currently work):
create or replace function "AUTO_INCREMENT"
(field in VARCHAR2)
return BOOLEAN
is
begin
IF ':NEW'.field IS NULL THEN
SELECT 'seq_'||field.nextval
INTO :NEW.field
FROM sys.dual;
END IF;
return true;
end;
Any help into getting this functionality working the way I would expect would be much appreciated. I'm new to PL/SQL and I think this would serve as a good introduction to functions (if this is where I should be using them). Thanks.