Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can use <code>RETURNING</code> with multiple values:</p> <pre><code>psql=&gt; create table t (id serial not null, x varchar not null); psql=&gt; insert into t (x) values ('a'),('b'),('c') returning id; id ---- 1 2 3 (3 rows) </code></pre> <p>So you want something more like this:</p> <pre><code>INSERT INTO AutoKeyEntity (Name,Description,EntityKey) VALUES ('AutoKey 254e3c64-485e-42a4-b1cf-d2e1e629df6a','Testing 5/4/2011 8:59:43 AM',DEFAULT) returning EntityKey; INSERT INTO AutoKeyEntityListed (EntityKey,Listed,ItemIndex) VALUES (CURRVAL('autokeyentity_entityKey_seq'),'Test 1 AutoKey 254e3c64-485e-42a4-b1cf-d2e1e629df6a', 0), (CURRVAL('autokeyentity_entityKey_seq'),'Test 2 AutoKey 254e3c64-485e-42a4-b1cf-d2e1e629df6a', 1), (CURRVAL('autokeyentity_entityKey_seq'),'Test 3 AutoKey 254e3c64-485e-42a4-b1cf-d2e1e629df6a', 2) returning EntityKey; -- etc. </code></pre> <p>And then you'll have to gather the returned <code>EntityKey</code> values from each statement in your transaction.</p> <p>You could try to grab the sequence's current value at the beginning and end of the transaction and use those to figure out which sequence values were used but <a href="http://www.postgresql.org/docs/9.0/interactive/sql-createsequence.html#AEN61833">that is not reliable</a>:</p> <blockquote> <p>Furthermore, although multiple sessions are guaranteed to allocate distinct sequence values, the values might be generated out of sequence when all the sessions are considered. For example, with a <em>cache</em> setting of 10, session A might reserve values 1..10 and return <code>nextval=1</code>, then session B might reserve values 11..20 and return <code>nextval=11</code> before session A has generated nextval=2. Thus, with a <em>cache</em> setting of one it is safe to assume that <code>nextval</code> values are generated sequentially; with a <em>cache</em> setting greater than one you should only assume that the <code>nextval</code> values are all distinct, not that they are generated purely sequentially. Also, <code>last_value</code> will reflect the latest value reserved by any session, whether or not it has yet been returned by <code>nextval</code>.</p> </blockquote> <p>So, even if your sequences have <em>cache</em> values of one you can still have non-contiguous sequence values in your transaction. However, you might be safe if the sequence's <em>cache</em> value matches the number of INSERTs in your transaction but I'd guess that that's going to be too large to make sense.</p> <p><strong>UPDATE</strong>: I just noticed (thanks to the questioner's comments) that there are two tables involved, got a bit lost in the wall of text.</p> <p>In that case, you should be able to use the current INSERTS:</p> <pre><code>INSERT INTO AutoKeyEntity (Name,Description,EntityKey) VALUES ('AutoKey 254e3c64-485e-42a4-b1cf-d2e1e629df6a','Testing 5/4/2011 8:59:43 AM',DEFAULT) returning EntityKey; INSERT INTO AutoKeyEntityListed (EntityKey,Listed,ItemIndex) VALUES (CURRVAL('autokeyentity_entityKey_seq'),'Test 1 AutoKey 254e3c64-485e-42a4-b1cf-d2e1e629df6a', 0), (CURRVAL('autokeyentity_entityKey_seq'),'Test 2 AutoKey 254e3c64-485e-42a4-b1cf-d2e1e629df6a', 1), (CURRVAL('autokeyentity_entityKey_seq'),'Test 3 AutoKey 254e3c64-485e-42a4-b1cf-d2e1e629df6a', 2); -- etc. </code></pre> <p>And grab the <code>EntityKey</code> values one at a time from the INSERTs on <code>AutoEntityKey</code>. Some sort of script might be needed to handle the RETURNING values. You could also wrap the <code>AutoKeyEntity</code> and related <code>AutoKeyEntityListed</code> INSERTs in a function, then use <a href="http://www.postgresql.org/docs/current/static/plpgsql-statements.html#PLPGSQL-STATEMENTS-SQL-ONEROW"><code>INTO</code></a> to grab the <code>EntityKey</code> value and return it from the function:</p> <pre><code>INSERT INTO AutoKeyEntity /*...*/ RETURNING EntityKey INTO ek; /* AutoKeyEntityListed INSERTs ... */ RETURN ek; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload