Sending Transact-SQL Statements to SQL Server

The SqlCmd% and SqlExec% functions allow your application to execute Transact-SQL statements.

The SqlCmd% function fills the command buffer with Transact-SQL statements that you then send to SQL Server. Each succeeding call to SqlCmd% appends the supplied Transact-SQL text to the end of text already in the buffer. When you add text to text already in the buffer, be sure to supply necessary blanks between words, such as the blank space at the beginning of the WHERE clause in the program fragment that follows.

'Retrieve two columns from the "authors" table
'in the "pubs" database.

'Put the command into the command buffer.
Result% = SqlCmd%(Sqlconn%, "SELECT au_lname, city")
Result% = SqlCmd%(Sqlconn%, " FROM pubs..authors")
Result% = SqlCmd%(Sqlconn%, " WHERE state = 'CA'")

'Send the command to SQL Server and start execution.
Result% = SqlExec%(Sqlconn%)

SqlExec% sends the contents of the buffer to SQL Server, which parses and executes the specified Transact-SQL statements. You can also use SqlSend% and SqlOk% instead of SqlExec%.

For an example of how these functions are used, refer to the QUERY sample application. This sample application calls the routine ExecuteSqlCommand, which in turn calls the functions SqlCmd% and SqlExec%. ExecuteSqlCommand is defined separately in the VBSQLGEN.BAS common code module.