Solved

currval() when doing insert/select

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Database tuning – How to start and what to tune. This question is frequently asked by many people, both online and offline. There is no hard and fast rule-of-thumb for performance tuning, however, before beginning the tuning process one should a…
CCModeler offers a way to enter basic information like entities, attributes and relationships and export them as yEd or erviz diagram. It also can import existing Access or SQL Server tables with relationships.
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…

746 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now