Show TOC

SET Statement [ESQL]Locate this document in the navigation structure

Assigns a value to a SQL variable.

Syntax
SET <identifier> = <expression>
Examples

(back to top)

  • Example 1 this code fragment inserts a large text value into the database:
    EXEC SQL BEGIN DECLARE SECTION;
    char buffer[5001];
    EXEC SQL END DECLARE SECTION;
    
    
    EXEC SQL CREATE VARIABLE hold_text VARCHAR;
    EXEC SQL SET hold_text = '';
    for(;;) {
    	/* read some data into buffer ... */
    	size = fread( buffer, 1, 5000, fp );
    	if( size <= 0 ) break;
    	
    	/* buffer must be null-terminated */
    	buffer[size] = '\0';
    	/* add data to blob using concatenation */
    	EXEC SQL SET hold_text = hold_text || :buffer;
    }
    EXEC SQL INSERT INTO some_table VALUES ( 1, hold_text );
    EXEC SQL DROP VARIABLE hold_text;
  • Example 2 this code fragment inserts a large binary value into the database:
    EXEC SQL BEGIN DECLARE SECTION;
    DECL_BINARY( 5000 ) buffer;
    EXEC SQL END DECLARE SECTION;
    EXEC SQL CREATE VARIABLE hold_blob LONG BINARY;
    EXEC SQL SET hold_blob = '';
    for(;;) {
    	/* read some data into buffer ... */
    	size = fread( &(buffer.array), 1, 5000, fp );
    	if( size <= 0 ) break;
    	buffer.len = size;
    
    
    	/* add data to blob using concatenation
    		Note that concatenation works for 
    		binary data too! */
    	EXEC SQL SET hold_blob = hold_blob || :buffer;
    }
    EXEC SQL INSERT INTO some_table VALUES ( 1, hold_blob );
    EXEC SQL DROP VARIABLE hold_blob;
Usage

(back to top)

The SET statement assigns a new value to a variable that was previously created using the CREATE VARIABLE statement.

You can use a variable in a SQL statement anywhere a column name is allowed. If there is no column name that matches the identifier, the database server checks to see if there is a variable that matches, and uses its value.

Variables are local to the current connection, and disappear when you disconnect from the database or when you use DROP VARIABLE. They are not affected by COMMIT or ROLLBACK statements.

Variables are necessary for creating large text or binary objects for INSERT or UPDATE statements from Embedded SQL programs because Embedded SQL host variables are limited to 32,767 bytes.

Standards

(back to top)

  • SQL—ISO/ANSI SQL compliant.
  • SAP Database products—Not supported. In SAP ASE, variables are assigned using the SELECT statement with no table, a Transact-SQL syntax that is also supported by SAP IQ. The SET statement is used to set database options in SAP ASE.
Permissions