Mike Pérez
Mike Pérez

Reputation: 201

Exception not running PostgreSQL

I got the code:

DECLARE

BEGIN

    INSERT INTO articulo VALUES (33, 'Mesa 33', 1000.45463, 50.2345, 200.23459, 20, 'Conjunto');
    INSERT INTO articulo VALUES (34, 'Mesa 34', 300.4500, 15.2379 , 1.2379 , 5, 'kid 4');

    SELECT * FROM articulo;

    INSERT INTO cliente (id_clie, nom_clie, rfc_clie, tel_clie, dir_clie, suspendido) VALUES (44,'Rosa Almaran', 'R7XA-910101', '5544466677', 'sur 24', 0);

EXCEPTION
WHEN NO_DATA_FOUND THEN
  RAISE EXCEPTION ''All fields are required'';  
WHEN OTHERS THEN
RAISE EXCEPTION ''This is an exception'';   
END; 

Which I read from here. When I try to run the code I get:

ERROR: syntax error at or near "INSERT"
LINE 5: INSERT INTO Product VALUES (33, 'Mesa 33', 1000.45463, 50 ....
        ^

I want PostgreSQL to show an exception depending of the type of exception it encounters.

Any help would be thanked!

Upvotes: 1

Views: 791

Answers (1)

Wolph
Wolph

Reputation: 80061

Try wrapping your code in a DO statement:

DO LANGUAGE plpgsql $$
DECLARE

BEGIN

    INSERT INTO articulo VALUES (33, 'Mesa 33', 1000.45463, 50.2345, 200.23459, 20, 'Conjunto');
    INSERT INTO articulo VALUES (34, 'Mesa 34', 300.4500, 15.2379 , 1.2379 , 5, 'kid 4');

    SELECT * FROM articulo;

    INSERT INTO cliente (id_clie, nom_clie, rfc_clie, tel_clie, dir_clie, suspendido) VALUES (44,'Rosa Almaran', 'R7XA-910101', '5544466677', 'sur 24', 0);

EXCEPTION
WHEN NO_DATA_FOUND THEN
  RAISE EXCEPTION 'All fields are required';  
WHEN OTHERS THEN
RAISE EXCEPTION 'This is an exception';   
END;
$$;

Upvotes: 1

Related Questions