Solved

currval() when doing insert/select

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

Get Database Help Now w/ Support & Database Audit

Keeping your database environment tuned, optimized and high-performance is key to achieving business goals. If your database goes down, so does your business. Percona experts have a long history of helping enterprises ensure their databases are running smoothly.

Question has a verified solution.

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

Suggested Solutions

Many companies are looking to get out of the datacenter business and to services like Microsoft Azure to provide Infrastructure as a Service (IaaS) solutions for legacy client server workloads, rather than continuing to make capital investments in h…
As technology users and professionals, we’re always learning. Our universal interest in advancing our knowledge of the trade is unmatched by most industries. It’s a curiosity that makes sense, given the climate of change. Within that, there lies a…
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…

751 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