Please enable JavaScript to view this site.

ESL Documentation

Navigation: ESL Documentation > ESL Database Services > Code Samples

Static SQL using High Level Commands

Scroll Prev Top Next More

This example uses Core commands and EDS High Level commands. First, it uses the High Level command EDSInit to establish a connection to the data source. Next, the application uses Core commands to create a table, prepare an INSERT statement, set the parameters for the insertion, and execute the statement, and then directly execute a SELECT statement. Then the application uses the High Level command EDSStaticBind to bind the first two columns of data to an ESL structure variable.  After using Core commands in a loop to fetch the data and print it to an error log file, the application disconnects from the data source by using the High Level function EDSDeInitEnv to end the connection and free the statement, connection, and environment handles.

Note: Instead of using DeInitEnv, you could perform the equivalent action by using the High Level command EDSDeInitConnect followed by the Core command EDSFreeEnv.

 

include eds.inc

 

##

## Declare stimulus for EDS.DLL.

##

stimulus EDSStimulus "eds"

##

## Declare variables.

##

string Dsn_SV is "default"

string Uid_SV is " YourUserID"

string Pwd_SV is " YourPassword"

string UseDsn_SV is "USE TESTDB"

string SelectString_SV is "SELECT ID, NAME FROM EMP"

string SQLState_SV

string ErrorText_SV

integer Henv_IV 

integer Hdbc_IV 

integer Hstmt_IV 

integer Error_IV 

integer NativeError_IV 

integer SType_IV 

boolean Forever_BV is true

##

## Declare structures.

##

structure EDSCOLDEF_STRUCT Colinfo_Struct

 

structure EMP is

   integer Id_IV

   string Name_SV 

end structure

 

structure EMP Employee_STRUCT

 

action CheckForError is

##

## Call EDSQueryInternalError to process

## internal EDS error.

##

   if (Error_IV < EDS_INTERNAL_ERR_START) then

      call EDSQueryInternalError(Error_IV, 

                                 ErrorText_SV)

      send ErrorText_SV "\n" to errorlog

      exit

   end if

##

## Call EDSError to process ODBC error.

##

   if ((Error_IV > EDS_INTERNAL_ERR_START) and

      (Error_IV < EDS_SUCCESS)) then

      call EDSError(Henv_IV,

                    Hdbc_IV,

                    Hstmt_IV, 

                    SQLState_SV,

                    NativeError_IV, 

                    ErrorText_SV) 

      send "SQL State : " SQLState_SV "\nError Text: " ErrorText_SV "\n" to errorlog

      exit

   end if 

   

response to start 

##

## EDS High level alternative to initialize

## environment, connection and statement handles

## as well as connect to the data source.

##

   call EDSInit(Henv_IV,

                Hdbc_IV,

                Hstmt_IV,

                Dsn_SV,

                Uid_SV,

                Pwd_SV)

   copy errorlevel to Error_IV

   action CheckForError

##

## Execute SQL: Use TestDB

##

   call EDSExecDirect(Hstmt_IV,

                      UseDsn_SV)

   copy errorlevel to Error_IV

   action CheckForError

##

## Execute the SELECT statment using the same

## Hstmt.

##

   call EDSExecDirect(Hstmt_IV,

                      SelectString_SV)

   copy errorlevel to Error_IV 

   action CheckForError

##

## EDS High Level alternative to bind a

## structure to a result set.

##

   copy (handle of EMP) to SType_IV 

   copy EDSStaticBind(Hstmt_IV,

                      SType_IV,

                      Employee_STRUCT) to Error_IV

   action CheckForError 

##

## Print a header to the errorlog first.

##

   send "ID NAME\n____________\n" to errorlog

##

## Fetch results; write them to errorlog.

##

   while (Forever_BV) loop

      call EDSFetch(Hstmt_IV)

      copy errorlevel to Error_IV 

      action CheckForError 

      if ((Error_IV = EDS_NO_DATA_FOUND) or

         (Error_IV = EDS_ERROR)) then

         leave loop

      else

         send Employee_STRUCT.Id_IV " " Employee_STRUCT.Name_SV "\n" to errorlog

      end if

   end loop

##

## EDS High Level alternative to disconnect and

## free all connections and their associated

## statements with Henv.

##

   call EDSDeInitEnv(Henv_IV)

   copy errorlevel to Error_IV

   action CheckForError

##

## Exit the application

##

   exit