Solved

currval() when doing insert/select

Posted on 2011-09-25
2
426 Views
Last Modified: 2012-05-12
I have created a view with a rule that allows me to insert into 2 tables that have a base/child 1:1 relationship.  The base table has a projtag_id field and the child table has a FK to this field.  I'm trying to use currval() to determine what the current projtag_id is after the row was inserted into the base table when using an INSERT...SELECT style query.

The problem is that it seems all records are inserted into the base table first.  Then the child table will get currval() only after the last id entered into the base table.  It does not do after every row insert.

For example, if my sequence starts at 100 and I insert 100 rows using INSERT...SELECT, then the child table will get 200 for the projtag_id for every row instead of 101,102,103,...

I've included my rule.  This is for Postgres.


 
CREATE OR REPLACE RULE rule_base_test_i AS
    ON INSERT TO base_test
    DO INSTEAD (
    INSERT INTO base_test_t
 (project_id,projtag_name) 
     VALUES
 (new.project_id,new.projtag_name);
 INSERT INTO child_test_t (projtag_id,projtag_type,required_values) 
  VALUES ((select currval('base_test_t_projtag_id_seq')),new.projtag_type,new.required_values);
);

Open in new window

0
Comment
Question by:mock5c
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 9

Expert Comment

by:fcontrepois
ID: 36597327
It's best to extract the value of your seq. in a variable then use it on both queries.

CREATE OR REPLACE RULE rule_base_test_i AS
    ON INSERT TO base_test
    DO INSTEAD (
    MYID = execute("SELECT curval('base_test_t_projtag_id_seq')"); 
    INSERT INTO base_test_t
 (projtag_id, project_id,projtag_name) 
     VALUES
 (MYID, new.project_id,new.projtag_name);
 INSERT INTO child_test_t (projtag_id,projtag_type,required_values) 
  VALUES (MYID),new.projtag_type,new.required_values);
);

Open in new window


Not tested
0
 
LVL 22

Accepted Solution

by:
earth man2 earned 500 total points
ID: 36713511
curval requires that a nextval has been executed before in that database connection.

Use RETURNING clause in INSERT statement instead.
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows the steps required to install WordPress on Azure. Web Apps, Mobile Apps, API Apps, or Functions, in Azure all these run in an App Service plan. WordPress is no exception and requires an App Service Plan and Database to install
Recently, Microsoft released a best-practice guide for securing Active Directory. It's a whopping 300+ pages long. Those of us tasked with securing our company’s databases and systems would, ideally, have time to devote to learning the ins and outs…
Steps to create a PostgreSQL RDS instance in the Amazon cloud. We will cover some of the default settings and show how to connect to the instance once it is up and running.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

632 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question