SVSQL
Database management using SQL commands.
The SVSQL instruction allows accessing an ODBC source using SQL commands from within SCADA Basic scripts. The ODBC source must first be configured using the host operating system. How to do this is explained in the book Using ODBC.
WebVue support - Yes.
The use of pre-configured Sql connections along with the instructions SQL_CONNECTION, SQL_COMMAND and SQL_QUERY is preferred to using the verb SVSQL (based on ODBC).
Pre-configured Sql connections are configured in the Application Explorer. See the Data Connections book for information.
'Database' is used here in the general sense, and bears no relation to the SV Configuration Variables Tree (previously known as the configuration 'database').
Mode |
Mnemonic |
Syntax |
1 | CONNECT | 1 |
2 | DISCONNECT | 2 |
3 | BEGINTRANS | 2 |
4 | COMMIT | 2 |
5 | ROLLBACK | 2 |
6 | EXECUTE | 3, 7 |
7 | FETCH | 2 |
8 | GETCOL | 4 |
9 | GETROW | 5 |
10 | NUMCOL | 2 |
11 | ROWCOUNT | 2 |
12 | GETCOLNAME | 4 |
13 | GETCOLSIZE | 6 |
14 | FREE | 2 |
15 | ERROR | 7 |
Syntax 1
LongVal = SVSQL(Mode, Source[, UserID] [, Password] [, Timeout]);
Return type: LONG.
Argument |
Meaning |
Source |
The name of the ODBC source. Type STR. |
UserID |
The user name to access the ODBC source. Type STR. |
Password |
The user password to access the ODBC source. Type STR. |
Timeout |
The connection time-out in seconds. Type LONG. |
Execution
Mode |
Mnemonic |
Action |
1 |
CONNECT |
An attempt is made to connect to the specified ODBC source. Return: >0 The return is the handle for the connection
which is then used in subsequent transactions. |
Syntax 2
LongVal = SVSQL(Mode, CHandle);
Return type: LONG.
Argument |
Meaning |
CHandle |
The handle returned when connecting to an ODBC source. Type LONG. |
Execution
Mode |
Mnemonic |
Action |
2 |
DISCONNECT |
An attempt is made to disconnect from the ODBC source
using the given handle. Return: 0 if disconnection successful. |
3 |
BEGINTRANS |
Start a transaction. Return: 0 if transaction successful. |
|
|
Not all ODBC drivers support transactions and those that do treat them in a different ways. This instruction only operates under the following conditions: The driver supports transactions. |
4 |
COMMIT |
Execute a COMMIT. Return: 0 if successful. |
|
|
Before you can execute a COMMIT, you must first have started a transaction with BEGINTRANS instruction. The operation of COMMIT depends on the ODBC driver used. |
5 |
ROLLBACK |
Execute a ROLLBACK. Return: if
successful. |
|
|
Before you can execute a ROLLBACK, you must first have started a transaction with BEGINTRANS instruction. The operation of ROLLBACK depends on the ODBC driver used. |
7 |
FETCH |
Fetches a single record from the result of an EXECUTE
instruction. Return: 0 if FETCH is successful and a record was
returned. |
|
|
FETCH places the next record in an internal buffer but does not supply the values to the SCADA Basic script. To retrieve individual fields it must be followed with a GETCOL or GETROW instruction. |
10 |
NUMCOL |
Return the number of columns resulting from executing a command. Return: >=0 the instruction was executed successfully.
The return is the number of columns. |
|
|
The instruction must be preceded by an EXECUTE. |
11 |
ROWCOUNT |
Return the number of records affected by an Insert, Update or Delete query. Return: >=0 the instruction was executed successfully.
The return is the number of records. |
|
|
The instruction must be preceded by an EXECUTE. |
14 |
FREE |
Frees resources used by an SQL request. Return: 0 if successful. |
|
|
This instruction allows the recovery of resources that may still be allocated due to a failed request. A FREE action is also executed automatically when the instruction DISCONNECT is used. The FREE instruction must also be used prior to starting a transaction. |
Syntax 3
LongVal = SVSQL(Mode, CHandle, MsgString);
Return type: LONG.
Argument |
Meaning |
CHandle |
The handle returned when connecting to an ODBC source. Type LONG. |
MsgString |
The SQL command to be executed. Type STR. |
Execution
Mode |
Mnemonic |
Action |
6 |
EXECUTE |
The SQL command supplied is executed. Return: 0 if command is executed successfully. |
|
|
Only a single SQL command may be active per connection. If you execute another command before the previous one has completed, the previous command will be terminated and the result lost. |
Syntax 4
LongVal = SVSQL(Mode, Shandle, ColNum, BHandle);
Return type: LONG.
Argument |
Meaning |
SHandle |
The handle returned when connecting to an ODBC source. Type LONG. |
ColNum |
The column number of the field to retrieve. Starts at 1 and increments from left to right. Type INTEGER. |
BHandle |
The handle of a memory buffer into which the retrieved field is placed. Type LONG. |
Execution
Mode |
Mnemonic |
Action |
8 |
GETCOL |
Retrieves a single field of data and places it in the
specified memory buffer. The field is always retrieved as a character string regardless of its format within the Variables Tree. The maximum length field that can be retrieved is 255 characters. |
12 |
GETCOLNAME |
Retrieves the name of a column and places it in a memory buffer. This instruction must be preceded by an EXECUTE and a FETCH. |
|
|
Return: 0 < 2,047 the instruction was executed successfully. The return is the number of characters placed in the buffer. |
Syntax 5
LongVal = SVSQL(Mode, Shandle, Bhandle, Delim);
Return type: LONG.
Argument |
Meaning |
CHandle |
The handle returned when connecting to an ODBC source. Type LONG. |
BHandle |
The handle of a memory buffer into which the retrieved field is placed. Type LONG. |
Delim |
A chain of characters to be used as a field delimiter. Type STR. |
Execution
Mode |
Mnemonic |
Action |
9 |
GETROW |
Retrieves an entire row of data and places it in the specified memory buffer. Return: >= 0 the instruction was executed successfully. The return is the number of characters placed in the buffer. |
|
|
The data is always retrieved as a character string regardless of its format within the Variables Tree. |
Syntax 6
LongVal = SVSQL(Mode, CHandle, ColNum);
Return type: LONG.
Argument |
Meaning |
CHandle |
The handle returned when connecting to an ODBC source. Type LONG. |
ColNum |
A column number. Starts at 1 and increments from left to right. Type INTEGER. |
Execution
Mode |
Mnemonic |
Action |
13 |
GETCOLSIZE |
Retrieves the maximum size of a column from a preceding EXECUTE instruction. |
|
|
Return: >= 0 the instruction was executed successfully.
The return is the size of the column. |
|
|
The size returned corresponds to the number of characters necessary to store the column as text, rather than the number of bytes that the column occupies. |
Syntax 7
LongVal = SVSQL(Mode, CHandle, BHandle);
Return type: LONG.
Argument |
Meaning |
CHandle |
The handle returned when connecting to an ODBC source. Type LONG. |
BHandle |
The handle of a memory buffer. Type LONG. |
Execution
Mode |
Mnemonic |
Action |
6 |
EXECUTE |
The SQL command contained in the memory buffer is executed. Return: 0 if command is executed successfully. |
|
|
Only a single SQL command may be active per connection. If you execute another command before the previous one has completed, the previous command will be terminated and the result lost. |
15 |
ERROR |
Returns the last error message from the ODBC driver and places in the memory buffer specified by the handle. Return: >=0 if command is executed successfully. The return corresponds to the number of bytes in the message. |
Example
For an example, select the Example link above.