PREPARE

|         PREPARE          |                            |                            |                  |                                     |
|:------------------------:|----------------------------|----------------------------|------------------|-------------------------------------|
|[Prev](ecpg-sql-open.html)|[Up](ecpg-sql-commands.html)|36.14. Embedded SQL Commands|[Home](index.html)| [Next](ecpg-sql-set-autocommit.html)|

---

## PREPARE

PREPARE — prepare a statement for execution

## Synopsis

```
PREPARE prepared_name FROM string

```

## Description

`PREPARE` prepares a statement dynamically specified as a string for execution. This is different from the direct SQL statement [PREPARE](sql-prepare.html), which can also be used in embedded programs. The [EXECUTE](sql-execute.html) command is used to execute either kind of prepared statement.

## Parameters

*`prepared_name`*

 An identifier for the prepared query.

*`string`*

 A literal string or a host variable containing a preparable SQL statement, one of SELECT, INSERT, UPDATE, or DELETE. Use question marks (`?`) for parameter values to be supplied at execution.

## Notes

 In typical usage, the *`string`* is a host variable reference to a string containing a dynamically-constructed SQL statement. The case of a literal string is not very useful; you might as well just write a direct SQL `PREPARE` statement.

 If you do use a literal string, keep in mind that any double quotes you might wish to include in the SQL statement must be written as octal escapes (`\042`) not the usual C idiom `\"`. This is because the string is inside an `EXEC SQL` section, so the ECPG lexer parses it according to SQL rules not C rules. Any embedded backslashes will later be handled according to C rules; but `\"` causes an immediate syntax error because it is seen as ending the literal.

## Examples

```
char *stmt = "SELECT * FROM test1 WHERE a = ? AND b = ?";

EXEC SQL ALLOCATE DESCRIPTOR outdesc;
EXEC SQL PREPARE foo FROM :stmt;

EXEC SQL EXECUTE foo USING SQL DESCRIPTOR indesc INTO SQL DESCRIPTOR outdesc;

```

## Compatibility

`PREPARE` is specified in the SQL standard.

## See Also

[EXECUTE](sql-execute.html)

---

|[Prev](ecpg-sql-open.html)|[Up](ecpg-sql-commands.html)| [Next](ecpg-sql-set-autocommit.html)|
|:-------------------------|:--------------------------:|------------------------------------:|
|           OPEN           |     [Home](index.html)     |            SET AUTOCOMMIT           |

 // \<![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.'); })(); } // ]]\>