簡體   English   中英

將PostgreSQL PSQL SELECT轉換為變量

[英]PostgreSQL PSQL SELECT into a variable

在Postgres控制台(PSQL)上,我嘗試編寫一個簡單的“批處理”測試:

\set oid

\set username 'john'
\set emailadr 'john@mailbox.net'
\set password 'jjjj'
INSERT INTO :oid SELECT auc_user_insert(:'username', :'emailadr', :'password');
SELECT auc_user_select(:oid);
SELECT auc_user_delete(:oid);

auc_user_insertauc_user_selectauc_user_delete是特定auc_user_delete應用程序的Postgres函數,並且可以對每個self正常工作(在查詢工具中)。 insert函數將bigint作為唯一ID返回,我想將其分配給變量oid ,以在下一行中使用。

但是在PSQL中的INTO :oid語句中,我得到了錯誤

ERROR:  Relation 'bigint' doesn't exist

有什么問題,使用這種變量的正確方法是什么? 感謝您的提示!

假設INSERT INTO是要影響:oid變量,而不是將select的結果插入表中,那么您需要這樣做:

SELECT auc_user_insert(:'username', :'emailadr', :'password') AS oid;
\gset
SELECT auc_user_select(:oid);
SELECT auc_user_delete(:oid);

\\gset是自psql 9.3起的\\gset命令

如果您使用的是舊版本,則很難模仿它。

聯機幫助頁中的說明:

\\ gset [前綴]

將當前查詢輸入緩沖區發送到服務器,並將查詢的輸出存儲到psql變量中(請參見變量)。 要執行的查詢必須恰好返回一行。 該行的每一列都存儲在一個單獨的變量中,該變量與該列相同

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM