sql >> Database teknologi >  >> RDS >> PostgreSQL

RETURERER data fra opdaterbar visning ikke virker?

Du var så tæt på;) du behøver kun at opdatere NEW.id i din trigger.

CREATE FUNCTION t0ii_person_details() RETURNS trigger AS $$
DECLARE
  addr  integer;
BEGIN
  -- Want at least a town to insert anything into table address.
  IF (NEW.town IS NOT NULL) THEN
    INSERT INTO address(street, town)
      VALUES (NEW.street, NEW.town)
      RETURNING id INTO addr;
  ELSE
    addr := NULL;
  END IF;

  INSERT INTO person(first_name, family_name, address)
    VALUES (NEW.first_name, NEW.family_name, addr) 
    -- here is the only change I made
    RETURNING id INTO NEW.id;
  RETURN NEW;
END; $$ LANGUAGE plpgsql SECURITY DEFINER;



  1. Anvend `trim()` og `regexp_replace()` på tekstarray

  2. Hvordan skal ip2long konverterede IP'er gemmes i MySQL?

  3. mysql join ON og AND for at laravel veltalende

  4. Brug af Oracle-database til standard ASP.NET MVC-applikationen