title: Postgresql RAISE EXCEPTION when operation get 0 or less in Update
body: <p>I have three tables:</p>
  
  <pre><code>    CREATE TABLE public.art_movimientos
      (
        cmovimiento bigint NOT NULL DEFAULT nextval('art_movimientos_cmovimiento_seq'::regclass),
        tipo character varying(3) NOT NULL, -- Tipos de Valores:...
        fecha_mov timestamp without time zone NOT NULL,
        documento integer NOT NULL,
        control integer,
        fecha_doc timestamp without time zone NOT NULL,
        corden integer NOT NULL DEFAULT 0,
        calmacen integer NOT NULL,
        calmacen2 integer,
        status character varying(13) NOT NULL DEFAULT 'PENDIENTE'::bpchar, -- PENDIENTE...
        donado integer NOT NULL DEFAULT 0,
        monto_mov numeric(11,2) NOT NULL DEFAULT 0.00,
        monto_desc numeric(11,2) NOT NULL DEFAULT 0.00,
        monto_total numeric(11,2) NOT NULL DEFAULT 0.00,
        observacion text,
        casiento integer,
        crea_user character varying(25),
        crea_date timestamp without time zone,
        mod_user character varying(25),
        mod_date timestamp without time zone,
        cproveedor integer NOT NULL DEFAULT 0
      )
  CREATE TABLE public.art_movimientos_det
  (
    cmovimiento_det bigint NOT NULL DEFAULT nextval('art_movimientos_det_cmovimiento_det_seq'::regclass),
    cmovimiento integer NOT NULL,
    cart_generico integer NOT NULL,
    cunidad integer NOT NULL DEFAULT 1,
    cant numeric(11,2) NOT NULL DEFAULT 0.00,
    iva numeric(11,2) NOT NULL DEFAULT 0.00,
    costou numeric(11,2) NOT NULL DEFAULT 0.00,
    crea_user character varying(25),
    crea_date timestamp without time zone,
    mod_user character varying(25),
    mod_date timestamp without time zone,
    cart_comercial integer NOT NULL,
    costot numeric(11,2) NOT NULL DEFAULT 0.00
  )
  CREATE TABLE public.ordencompra_det
  (
    corden_det bigint NOT NULL DEFAULT nextval('ordencompra_det_corden_det_seq'::regclass),
    corden integer NOT NULL,
    cart_comercial integer NOT NULL,
    cunidad integer NOT NULL DEFAULT 1,
    cant numeric(11,2) NOT NULL DEFAULT 0.00,
    costou numeric(11,2) NOT NULL DEFAULT 0.00,
    iva numeric(11,0) NOT NULL DEFAULT 0.00,
    costot numeric(11,2) NOT NULL DEFAULT 0.00,
    crea_user character varying(25),
    crea_date timestamp without time zone,
    mod_user character varying(25),
    mod_date timestamp without time zone,
    cant_restante numeric(11,2) NOT NULL DEFAULT 0
  )
  </code></pre>
  
  <p>I have a procedure that reduce the <code>cant_restante</code> in <code>ordencompra_det</code>:</p>
  
  <pre><code>UPDATE ordencompra_det AS od
          SET cant_restante = cant_restante - s.cant_real
          FROM (SELECT am.corden, md.cart_comercial,(md.cant*u.multiplicador)cant_real FROM art_movimientos am INNER JOIN art_movimientos_det md ON am.cmovimiento=md.cmovimiento INNER JOIN art_und u ON md.cunidad=u.cunidad WHERE md.cmovimiento=cmov) AS s
          WHERE od.corden=s.corden and od.cart_comercial=s.cart_comercial
  </code></pre>
  
  <p>But sometimes i get 0 or less in <code>cant_restante</code>, how i can do a check, if the update result below 0 ? i don't wanna have negative values "/, if i get negatives values, rollback the Update and raise a exception?</p>
  
  <p>I'm using postgresql function (procedure) for it (because I'm doing a lot stuffs in the DB)</p>