Put your tips and tricks online - Share your knowledge! Login | Register
 
 
  Search     Advanced search
 

Home | Ask Question | Add tip | My tips | Recent tips & tricks | Suggest a category | FAQ | Forums

 
 
 
 Category : Home > Databases > PostgreSQL     

Select nextval from multiple sequences at the same time


Sometimes you may want to get nextval from several sequences at the same time, i.e. if fields
are not set to auto increment and you want to run transaction from php script, this will save
calls to pg_query and pg_fetch_array. Here's one of the ways to do it in PostgreSQL and PHP.

/* In Postgres database create table */
CREATE TABLE dual (
	dummy varchar(1)
);

INSERT INTO dual (dummy) VALUES ('X');

/* In PHP script use this code */
$seq_sql = "SELECT *
		FROM
		(SELECT nextval('seq1') AS id1 FROM dual) a,
		(SELECT nextval('seq2') AS id2 FROM dual) b,
		(SELECT nextval('seq3') AS id3 FROM dual) c";

$result = pg_query($conn, $sql);
$rows_returned = pg_NumRows($result);

if ($rows_returned > 0) {

	for ($i=0; $i < $rows_returned; $i++) {

		$ids = pg_fetch_array($result, $i);

	}

}


  Options
 
   del.icio.us  |  newsvine  |  digg  |  furl  |  google  |  yahoo  |  Ma.gnolia  |  vigillar  |  reddit  |  technorati  |  icerocket  |  pubsub

       Rate this tip:    

Start discussion or add comment to this tip

  Details
Tip reference : #114
views : 343
Added on : 08/09/06
Submited by : h8dk97
 
Send a message Send a message Printer friendly output Printer friendly output
Display this member's tips Display this member's tips (161)
 
 
<< Previous Next >>
 Most viewed tips 
  Databases > Oracle > Security : How to unlock Oracle user account  
  Operating Systems > Unix : How to kill Unix user session  
  Databases > Oracle > Performance Tuning : How to enable trace in Oracle  
  Databases > Oracle : Kill user session  
   
  All categories
Databases | Programming | Hardware | Operating Systems | Networking | Internet | ERP / CRM | Games & Multimedia | Graphics & Design | Miscellaneous | Office Software | TipLib FAQ
 
 

Home |  FAQ |  Terms of Use |  Privacy Policy

© 2005 tiplib.com