Skip site navigation (1) Skip section navigation (2)

Query Question

From: Henry Ramsey <henry(dot)ramsey(dot)pcpa(at)statefarm(dot)com>
To: "pgsql-novice(at)postgresql(dot)org" <pgsql-novice(at)postgresql(dot)org>
Subject: Query Question
Date: 2012-06-15 23:08:41
Message-ID: 11019B26C993DA4FAD1B53A8A6B7A762277F07@WPSDGQHR.OPR.STATEFARM.ORG (view raw, whole thread or download thread mbox)
Lists: pgsql-novice
I have created 3 tables, the parent table has a primary key(pmt_id) which is generated by a sequence object which is tied to this table.  The 2nd table paid_policy is a child to the first table and has a primary key of pmt_id, (which is the FK to the 1st table), plcy_num and rqst_id.  The third table which is a child to the 2nd table has a primary key of pmt_id, plcy_num, rqst_id, and pmt_tstmp.  When inserting a row into the parent the sequence number is generated and returned by the statement below; however, I have not been able to figure out a way to capture this id to insert it in the 3rd table since it is part of the key.  This application will have thousands if not hundreds of thousands of inserts daily, so I was concerned that current value could change.  I thought about trying to capture the rowid from the statement below but was wondering if there is a better way to accomplish this.  Any assistance would be greatly appreciated.  I just joined the mailing list within the last hour so I am very new at this.  Thanks in advance.

WITH testtbl AS (
INSERT INTO schema.pmt_wip
     (pmt_id, clnt_id, agt_assoc_id, pmt_stts_cd, refr_num, user_id, pmt_rcpt_tstmp)
     (nextval('schema.pmt_wip_seq'), 'Client ID 5', 'Assoc Id 6', 1, 'reference number', 'PCPA', Now())
)  INSERT INTO schema.paid_plcy
         (pmt_id, plcy_num, rqst_id, plcy_st_cd, co_code, addl_life_plcy_amt, prem_trf_amt)
         ((SELECT pmt_id FROM testtbl), 'policy #2', 'rqst id2', 25, 4, 0, 0);

Henry Ramsey Jr.  A
Database Administrator
State Farm Insurance Companies
(309) 287-1064


pgsql-novice by date

Next:From: Josh KupershmidtDate: 2012-06-15 23:18:52
Subject: Re: Table name as a variable and/or EXECUTE and NEW.*
Previous:From: Alessandro GagliardiDate: 2012-06-15 19:55:49
Subject: Re: Planning a Large RDBMS

Privacy Policy | About PostgreSQL
Copyright © 1996-2017 The PostgreSQL Global Development Group