From bd7c95f0c1a38becffceb3ea7234d57167f6d4bf Mon Sep 17 00:00:00 2001 From: Michael Meskes Date: Sat, 16 Feb 2019 10:55:17 +0100 Subject: Add DECLARE STATEMENT support to ECPG. DECLARE STATEMENT is a statement that lets users declare an identifier pointing at a connection. This identifier will be used in other embedded dynamic SQL statement such as PREPARE, EXECUTE, DECLARE CURSOR and so on. When connecting to a non-default connection, the AT clause can be used in a DECLARE STATEMENT once and is no longer needed in every dynamic SQL statement. This makes ECPG applications easier and more efficient. Moreover, writing code without designating connection explicitly improves portability. Authors: Ideriha-san ("Ideriha, Takeshi" ) Kuroda-san ("Kuroda, Hayato" ) Discussion: https://postgr.es/m4E72940DA2BF16479384A86D54D0988A565669DF@G01JPEXMBKW04 --- doc/src/sgml/ecpg.sgml | 192 ++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 191 insertions(+), 1 deletion(-) (limited to 'doc/src') diff --git a/doc/src/sgml/ecpg.sgml b/doc/src/sgml/ecpg.sgml index fac45400b03..d2319c8b323 100644 --- a/doc/src/sgml/ecpg.sgml +++ b/doc/src/sgml/ecpg.sgml @@ -236,7 +236,7 @@ EXEC SQL CONNECT TO :target USER :user USING :passwd; SQL statements in embedded SQL programs are by default executed on the current connection, that is, the most recently opened one. If an application needs to manage multiple connections, then there are - two ways to handle this. + three ways to handle this. @@ -310,6 +310,17 @@ current=testdb2 (should be testdb2) current=testdb1 (should be testdb1) + + + The third option is to declare a sql identifier linked to + the connection, for example: + +EXEC SQL AT connection-name DECLARE statement-name STATEMENT; +EXEC SQL PREPARE statement-name FROM :dyn-string; + + Once you link a sql identifier to a connection, you execute a dynamic SQL + without AT clause. + @@ -6766,6 +6777,185 @@ EXEC SQL DECLARE cur1 CURSOR FOR stmt1; + + + DECLARE STATEMENT + declares SQL statement identifier associated with connection + + + + +EXEC SQL [ AT connection_name ] DECLARE statement_name STATEMENT + + + + + Description + + + DECLARE STATEMENT declares SQL statement identifier. + SQL statement identifier is associated with connection. + + + + DELARE CURSOR with a SQL statement identifier can be written before PREPARE. + + + + + Parameters + + + + connection_name + + + A database connection name established by the CONNECT command. + + + If AT clause is omitted, an SQL statement identifier is associated with the DEFAULT connection. + + + + + + + + statement_name + + + The name of a SQL statement identifier, either as an SQL identifier or a host variable. + + + + + + + + Notes + + AT clause can be used at other dynamic SQL statements. The following table + gives the connected database when AT clause is used at DECLARE STATEMENT + and other dynamic statements. + + + Scenario + + + + + Using Scenario + + + Declare Statement + + + Other Dynamic Statements + + + Executed Database + + + + + + + 1 + + + Without AT clause + + + Without AT clause + + + Default connection + + + + + 2 + + + Using AT clause connecting at con1 + + + Without AT clause + + + con1 + + + + + 3 + + + Using AT clause connecting at con1 + + + Using AT clause connecting at con2 + + + con1 + + + + + 4 + + + Without AT clause + + + Using AT clause connecting at con2 + + + con2 + + + + +
+ + In scenario 4, DECLARE STATEMENT will be ignored. + +
+ + + Examples + + +EXEC SQL CONNECT TO postgres AS con1; +EXEC SQL AT con1 DECLARE sql_stmt STATEMENT; +EXEC SQL DECLARE cursor_name CURSOR FOR sql_stmt; +EXEC SQL PREPARE sql_stmt FROM :dyn_string; +EXEC SQL OPEN cursor_name; +EXEC SQL FETCH cursor_name INTO :column1; +EXEC SQL CLOSE cursor_name; + + + + + Compatibility + + + DECLARE STATEMENT is a PostgreSQL extension of the SQL standard, + but can be used in Oracle and DB2. + + + + + See Also + + + + + + + +
+ DESCRIBE -- cgit v1.2.3