postgresql nextval question on sequences

六眼飞鱼酱① 提交于 2019-12-24 08:11:45

问题


I am trying to work with postgresql 'nextval' in PHP. How can I fill in the parenthesis in the third line in order to replace TXN_ID with the value of nextval('schemadb.audit_txn_seq')?

$DB->query("SELECT nextval('schemadb.audit_txn_seq')");
$DB->query('SET CONSTRAINTS ALL DEFERRED');
$DB->query('SELECT schemadb.undo_transaction(TXN_ID)');

Thanks!


回答1:


You did not tell us what is $DB variable. I guess you are using PDO.

The question "Is there anyway to store the result of query into php variable" is kind of beginner question, and is easilly answered in the manual.

You must understand, that (from PHP's point of view) there is no difference between

SELECT nextval('schemadb.audit_txn_seq')

and

SELECT xcolumn FROM xtable LIMIT 1

If you want to fetch data values from ANY query, you do it always the same, standard way:

  1. query
  2. execute
  3. fetch

Hope that helps.




回答2:


Try:

$DB->query("SELECT nextval('schemadb.audit_txn_seq')");
$DB->query('SET CONSTRAINTS ALL DEFERRED');
$DB->query("SELECT schemadb.undo_transaction( currval('schemadb.audit_txn_seq') )");



回答3:


I'm not sure about the interaction with SET CONSTRAINTS ALL DEFERRED, but have you tried doing this:

SELECT schemadb.undo_transaction(nextval('schemadb.audit_txn_seq'))

The question is now what does "undo_transaction" return? If it returns the transaction ID upon successful completion, then you're good to go!



来源:https://stackoverflow.com/questions/2189114/postgresql-nextval-question-on-sequences

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!