Solved

currval() when doing insert/select

Posted on 2011-09-25
2
418 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
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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

I annotated my article on ransomware somewhat extensively, but I keep adding new references and wanted to put a link to the reference library.  Despite all the reference tools I have on hand, it was not easy to find a way to do this easily. I finall…
This article explains all about SQL Server Piecemeal Restore with examples in step by step manner.
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.
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…

829 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