Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1ikXHC-0005Kt-7B for pgsql-general@arkaria.postgresql.org; Thu, 26 Dec 2019 17:50:22 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1ikXHA-00037S-Vu for pgsql-general@arkaria.postgresql.org; Thu, 26 Dec 2019 17:50:20 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1ikXHA-00037L-MJ for pgsql-general@lists.postgresql.org; Thu, 26 Dec 2019 17:50:20 +0000 Received: from mailbackend.panix.com ([166.84.1.89]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1ikXH7-0007bM-Nz for pgsql-general@lists.postgresql.org; Thu, 26 Dec 2019 17:50:20 +0000 Received: from panix5.panix.com (panix5.panix.com [166.84.1.5]) by mailbackend.panix.com (Postfix) with ESMTP id 47kHY356Y4z16Cx; Thu, 26 Dec 2019 12:50:15 -0500 (EST) Received: by panix5.panix.com (Postfix, from userid 17622) id 47kHY33fhYzfYc; Thu, 26 Dec 2019 12:50:15 -0500 (EST) Date: Thu, 26 Dec 2019 12:50:15 -0500 From: stan To: "David G. Johnston" Cc: pgsql-general@lists.postgresql.org Subject: Re: Not my day :-( Another syntax error Message-ID: <20191226175015.GA17780@panix.com> References: <20191226163320.GA16208@panix.com> <20191226173810.GB8161@panix.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-Editor: gVim User-Agent: Mutt/1.12.1 (2019-06-15) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On Thu, Dec 26, 2019 at 10:39:54AM -0700, David G. Johnston wrote: > You should probably send that reply again using reply-to-all. > > Dave > > > On Thu, Dec 26, 2019 at 10:38 AM stan wrote: > > > On Thu, Dec 26, 2019 at 10:26:49AM -0700, David G. Johnston wrote: > > > On Thu, Dec 26, 2019 at 9:33 AM stan wrote: > > > > > > > > > > > WITH inserted AS ( > > > > INSERT into project_cost_category > > > > (category) > > > > VALUES > > > > ('MISC') > > > > RETURNING > > > > * > > > > ) > > > > SELECT project_cost_category_key > > > > INTO NEW.project_cost_category_key = > > > > ( SELECT > > > > project_cost_category_key > > > > FROM > > > > inserted ) > > > > > > > > > > > You have two SELECTs. The "inner" one has a FROM clause attached to it > > > providing columns from the "inserted" CTE. The "outer" one doesn't have > > a > > > FROM clause and so doesn't have access to columns. The "outer" SELECT > > > project_cost_category_key is thus invalid. > > > > > > > INSERT into project_bom > > (project_key, bom_name) > > VALUES > > (NEW.project_key , 'Main') > > RETURNING > > project_bom_key > > ) > > SELECT project_bom_key INTO NEW.project_bom_key > > = ( SELECT > > project_bom_key > > FROM inserted ) > > ; > > > > Which is working, to the best of my knowledge. BTW the oen I am having > > trouble with originaly had: > > > > RETURBING project_cost_category_key > > > > Bit I changed that to * during my debuging efforts. > > > > Please tell me if I am looking at this worng. > > > > And thatnls for looking through my really long post Turns out, you were correct, changed it to: DROP FUNCTION default_cost_category() CASCADE; CREATE FUNCTION default_cost_category() RETURNS trigger AS $$ DECLARE _cost_category_key numeric; BEGIN /* ZZZZZ */ if NEW.project_cost_category_key IS NULL THEN /* DEBUG RAISE NOTICE 'Called default_cost_category() and NEW.project_cost_category_key is NULL' ; */ _cost_category_key = ( SELECT project_cost_category_key FROM project_cost_category WHERE category = 'MISC' ) ; /* DEBUG RAISE NOTICE '_cost_category_key = %', _cost_category_key ; */ IF _cost_category_key is NULL THEN WITH inserted AS ( INSERT into project_cost_category (category) VALUES ('MISC') RETURNING * ) SELECT project_cost_category_key INTO NEW.project_cost_category_key FROM ( SELECT project_cost_category_key FROM inserted ) AS project_cost_category_key ; ELSE NEW.project_cost_category_key = _cost_category_key; END IF; END IF; return NEW; END; $$ LANGUAGE PLPGSQL SECURITY DEFINER -- Set a secure search_path: trusted schema(s), then 'pg_temp'. SET search_path = ica, "user" , public VOLATILE ; And all is well. Thank you! -- "They that would give up essential liberty for temporary safety deserve neither liberty nor safety." -- Benjamin Franklin