declare.7

来自「PostgreSQL 8.2中增加了很多企业用户所需要的功能和性能上的提高,其开」· 7 代码 · 共 192 行

7
192
字号
.\\" auto-generated by docbook2man-spec $Revision: 1.1.1.1 $.TH "DECLARE" "" "2008-01-03" "SQL - Language Statements" "SQL Commands".SH NAMEDECLARE \- define a cursor.SH SYNOPSIS.sp.nfDECLARE \fIname\fR [ BINARY ] [ INSENSITIVE ] [ [ NO ] SCROLL ]    CURSOR [ { WITH | WITHOUT } HOLD ] FOR \fIquery\fR    [ FOR { READ ONLY | UPDATE [ OF \fIcolumn\fR [, ...] ] } ].sp.fi.SH "DESCRIPTION".PP\fBDECLARE\fR allows a user to create cursors, whichcan be used to retrievea small number of rows at a time out of a larger query. Cursors canreturn data either in text or in binary format usingFETCH [\fBfetch\fR(7)]..PPNormal cursors return data in text format, the same as a\fBSELECT\fR would produce. Since data is stored natively inbinary format, the system must do a conversion to produce the textformat. Once the information comes back in text form, the clientapplication may need to convert it to a binary format to manipulateit. In addition, data in the text format is often larger in sizethan in the binary format. Binary cursors return the data in abinary representation that may be more easily manipulated.Nevertheless, if you intend to display the data as text anyway,retrieving it in text form willsave you some effort on the client side..PPAs an example, if a query returns a value of one from an integer column,you would get a string of 1 with a default cursorwhereas with a binary cursor you would geta 4-byte field containing the internal representation of the value(in big-endian byte order)..PPBinary cursors should be used carefully. Many applications,including \fBpsql\fR, are not prepared tohandle binary cursors and expect data to come back in the textformat..sp.RS.B "Note:"When the client application uses the ``extended query'' protocolto issue a \fBFETCH\fR command, the Bind protocol messagespecifies whether data is to be retrieved in text or binary format.This choice overrides the way that the cursor is defined. The conceptof a binary cursor as such is thus obsolete when using extended queryprotocol \(em any cursor can be treated as either text or binary..RE.sp.SH "PARAMETERS".TP\fB\fIname\fB\fRThe name of the cursor to be created..TP\fBBINARY\fRCauses the cursor to return data in binary rather than in text format..TP\fBINSENSITIVE\fRIndicates that data retrieved from the cursor should beunaffected by updates to the tables underlying the cursor whilethe cursor exists. In PostgreSQL,all cursors are insensitive; this key word currently has noeffect and is present for compatibility with the SQL standard..TP\fBSCROLL\fR.TP\fBNO SCROLL\fRSCROLL specifies that the cursor may be usedto retrieve rows in a nonsequential fashion (e.g.,backward). Depending upon the complexity of the query'sexecution plan, specifying SCROLL may imposea performance penalty on the query's execution time.NO SCROLL specifies that the cursor cannot beused to retrieve rows in a nonsequential fashion. The default is toallow scrolling in some cases; this is not the same as specifyingSCROLL. See Notes [\fBdeclare\fR(7)] for details..TP\fBWITH HOLD\fR.TP\fBWITHOUT HOLD\fRWITH HOLD specifies that the cursor maycontinue to be used after the transaction that created itsuccessfully commits. WITHOUT HOLD specifiesthat the cursor cannot be used outside of the transaction thatcreated it. If neither WITHOUT HOLD norWITH HOLD is specified, WITHOUTHOLD is the default..TP\fB\fIquery\fB\fRA SELECT [\fBselect\fR(7)] orVALUES [\fBvalues\fR(7)] commandwhich will provide the rows to be returned by the cursor..TP\fBFOR READ ONLY\fR.TP\fBFOR UPDATE\fRFOR READ ONLY indicates that the cursor willbe used in a read-only mode. FOR UPDATEindicates that the cursor will be used to update tables. Sincecursor updates are not currently supported inPostgreSQL, specifying FORUPDATE will cause an error message and specifyingFOR READ ONLY has no effect..TP\fB\fIcolumn\fB\fRColumn(s) to be updated by the cursor. Since cursor updates arenot currently supported inPostgreSQL, the FORUPDATE clause provokes an error message..PPThe key words BINARY,INSENSITIVE, and SCROLL mayappear in any order..PP.SH "NOTES".PPUnless WITH HOLD is specified, the cursorcreated by this command can only be used within the currenttransaction. Thus, \fBDECLARE\fR without WITHHOLD is useless outside a transaction block: the cursor wouldsurvive only to the completion of the statement. ThereforePostgreSQL reports an error if thiscommand is used outside a transaction block.UseBEGIN [\fBbegin\fR(7)],COMMIT [\fBcommit\fR(7)]andROLLBACK [\fBrollback\fR(7)]to define a transaction block..PPIf WITH HOLD is specified and the transactionthat created the cursor successfully commits, the cursor cancontinue to be accessed by subsequent transactions in the samesession. (But if the creating transaction is aborted, the cursoris removed.) A cursor created with WITH HOLDis closed when an explicit \fBCLOSE\fR command isissued on it, or the session ends. In the current implementation,the rows represented by a held cursor are copied into a temporaryfile or memory area so that they remain available for subsequenttransactions..PPThe SCROLL option should be specified when defining acursor that will be used to fetch backwards. This is required bythe SQL standard. However, for compatibility with earlierversions, PostgreSQL will allowbackward fetches without SCROLL, if the cursor's queryplan is simple enough that no extra overhead is needed to supportit. However, application developers are advised not to rely onusing backward fetches from a cursor that has not been createdwith SCROLL. If NO SCROLL isspecified, then backward fetches are disallowed in any case..PPThe SQL standard only makes provisions for cursors in embeddedSQL. The PostgreSQLserver does not implement an \fBOPEN\fR statement forcursors; a cursor is considered to be open when it is declared.However, \fBECPG\fR, the embedded SQLpreprocessor for PostgreSQL, supportsthe standard SQL cursor conventions, including those involving\fBDECLARE\fR and \fBOPEN\fR statements..PPYou can see all available cursors by querying the \fBpg_cursors\fRsystem view..SH "EXAMPLES".PPTo declare a cursor:.sp.nfDECLARE liahona CURSOR FOR SELECT * FROM films;.sp.fiSee FETCH [\fBfetch\fR(7)] for moreexamples of cursor usage..SH "COMPATIBILITY".PPThe SQL standard allows cursors only in embeddedSQL and in modules. PostgreSQLpermits cursors to be used interactively..PPThe SQL standard allows cursors to update table data. AllPostgreSQL cursors are read only..PPBinary cursors are a PostgreSQLextension..SH "SEE ALSO"CLOSE [\fBclose\fR(7)], FETCH [\fBfetch\fR(l)], MOVE [\fBmove\fR(l)]

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?