Wednesday 15 August 2012

PostgreSQL nextval and currval in same query -


I was hoping to call once and return values ​​were used many times. Although this does not work.

Sequence seq_name comes at 10 ( questions are executed freely, not in the listed order ):

  SELECT NEXTVAL ('seq_name'), NextVival ('seq_name');   

Above 11, 12 (I want 11 and 11 again).

  SELECT NEXTVAL ('seq_name'), curvall ('seq_name'); SELECT NEXTVAL ('seq_name'), LastWell ();   

The above given 11, 10 OR will give an error which NEXTVAL was never called in the current session [ I know why the error happens :) ]

Is there a way to use it NEXTVAL and CURRVAL again : Is the SQL statement to get the same pay scale value (11 in this case) for use? Or a simple solution to this problem?

A potential solution

  << Strong>  Use  INSERT ... value  to enter instead of  

Code> INSERT ... SELECT

  Col1, col2, col3, col4, ... in INSERT Table1, select Next, Next, 5, 'Text value',. From (Select Next ('seq_name') Next) q    

No comments:

Post a Comment