Running errors in PostgreSQL

CREATE OR REPLACE FUNCTION msgfailerror() RETURNS trigger AS 
' BEGIN
IF NEW.noces< new.first_column THEN
RAISE EXCEPTION ' cannot have a negative salary';
END IF;
return new;
END' LANGUAGE plpgsql

trigger

create trigger msgfail before insert on first for each row 
execute procedure msgfailerror()

Give an error:

syntax error at or near “cannot” LINE 5: RAISE
EXCEPTION’cannot have a negative …

For each row field, I almost have a verification. I want the trigger to complete the insertion Check all verifications at the same time, and then raise the error log all at once. Should I use the raise exception in the raise notice?

For example:

Insert into first (first_column, noces,dob) values ​​('4545','75','545')

I am checking if noces is less than first_column, for the same row I want to check if dob> 80 and first_column is an integer and it raises an error for all validations. Thanks in advance

The quote is wrong. It’s easier to use the U.S. dollar quote:

CREATE OR REPLACE FUNCTION msgfailerror () 
RETURNS trigger AS
$$
BEGIN
IF NEW.noces< new.first_column THEN
RAISE EXCEPTION'cannot have a negative salary';
END IF;
return new;
END;
$$
LANGUAGE plpgsql;

But on the other hand, what’s wrong with check constraints?

CREATE OR REPLACE FUNCTION msgfailerror() RETURNS trigger AS 
' BEGIN
IF NEW.noces< new.first_column THEN
RAISE EXCEPTION'cannot have a negative salary';
END IF;
return new;
END' LANGUAGE plpgsql

trigger

create trigger msgfail before insert on first for each row 
execute procedure msgfailerror()

Give an error:

< p>syntax error at or near “cannot” LINE 5: RAISE
EXCEPTION’cannot have a negative …

For each line field, I almost have a validation. I want to trigger The server checks all verifications when the insertion is complete, and then raises the error log all at once. Should I use the raise exception in the raise notice?

For example:

Insert into first (first_column, noces,dob) values ​​('4545','75','545')

I’m checking if noces is less than first_column, for the same row I want to check if dob>80 and first_column is an integer and it raises an error for all validations. Thanks in advance

The quotation is wrong. It is easier to use the U.S. dollar quote $$:

CREATE OR REPLACE FUNCTION msgfailerror() 
RETURNS trigger AS
$$
BEGIN
IF NEW.noces< new.first_column THEN
RAISE EXCEPTION'cannot have a negative salary';
END IF;
return new; < br />END;
$$
LANGUAGE plpgsql;

But on the other hand, what’s wrong with check constraints?

Leave a Comment

Your email address will not be published.