DECLARE

|                  DECLARE                  |                            |                            |                  |                                        |
|:-----------------------------------------:|----------------------------|----------------------------|------------------|----------------------------------------|
|[Prev](ecpg-sql-deallocate-descriptor.html)|[Up](ecpg-sql-commands.html)|36.14. Embedded SQL Commands|[Home](index.html)| [Next](ecpg-sql-declare-statement.html)|

---

## DECLARE

DECLARE — define a cursor

## Synopsis

```
DECLARE cursor_name [ BINARY ] [ ASENSITIVE | INSENSITIVE ] [ [ NO ] SCROLL ] CURSOR [ { WITH | WITHOUT } HOLD ] FOR prepared_name
DECLARE cursor_name [ BINARY ] [ ASENSITIVE | INSENSITIVE ] [ [ NO ] SCROLL ] CURSOR [ { WITH | WITHOUT } HOLD ] FOR query

```

## Description

`DECLARE` declares a cursor for iterating over the result set of a prepared statement. This command has slightly different semantics from the direct SQL command `DECLARE`: Whereas the latter executes a query and prepares the result set for retrieval, this embedded SQL command merely declares a name as a “loop variable” for iterating over the result set of a query; the actual execution happens when the cursor is opened with the `OPEN` command.

## Parameters

*`cursor_name`*

 A cursor name, case sensitive. This can be an SQL identifier or a host variable.

*`prepared_name`*

 The name of a prepared query, either as an SQL identifier or a host variable.

*`query`*

 A [SELECT](sql-select.html) or [VALUES](sql-values.html) command which will provide the rows to be returned by the cursor.

 For the meaning of the cursor options, see [DECLARE](sql-declare.html).

## Examples

 Examples declaring a cursor for a query:

```
EXEC SQL DECLARE C CURSOR FOR SELECT * FROM My_Table;
EXEC SQL DECLARE C CURSOR FOR SELECT Item1 FROM T;
EXEC SQL DECLARE cur1 CURSOR FOR SELECT version();

```

 An example declaring a cursor for a prepared statement:

```
EXEC SQL PREPARE stmt1 AS SELECT version();
EXEC SQL DECLARE cur1 CURSOR FOR stmt1;

```

## Compatibility

`DECLARE` is specified in the SQL standard.

## See Also

[OPEN](ecpg-sql-open.html), [CLOSE](sql-close.html), [DECLARE](sql-declare.html)

---

|[Prev](ecpg-sql-deallocate-descriptor.html)|[Up](ecpg-sql-commands.html)| [Next](ecpg-sql-declare-statement.html)|
|:------------------------------------------|:--------------------------:|---------------------------------------:|
|           DEALLOCATE DESCRIPTOR           |     [Home](index.html)     |            DECLARE STATEMENT           |

 // \<![CDATA[ \<-- For SVG support if ('WebSocket' in window) { (function() { function refreshCSS() { var sheets = [].slice.call(document.getElementsByTagName("link")); var head = document.getElementsByTagName("head")[0]; for (var i = 0; i \< sheets.length; ++i) { var elem = sheets[i]; head.removeChild(elem); var rel = elem.rel; if (elem.href && typeof rel != "string" || rel.length == 0 || rel.toLowerCase() == "stylesheet") { var url = elem.href.replace(/(&|\\?)\_cacheOverride=\\d+/, ''); elem.href = url + (url.indexOf('?') \>= 0 ? '&' : '?') + '\_cacheOverride=' + (new Date().valueOf()); } head.appendChild(elem); } } var protocol = window.location.protocol === 'http:' ? 'ws://' : 'wss://'; var address = protocol + window.location.host + window.location.pathname + '/ws'; var socket = new WebSocket(address); socket.onmessage = function(msg) { if (msg.data == 'reload') window.location.reload(); else if (msg.data == 'refreshcss') refreshCSS(); }; console.log('Live reload enabled.'); })(); } // ]]\>