Learn R Programming

RODBCext (version 0.2.1)

sqlExecute: Executes an already prepared query

Description

Executes a parameterized query.

Optionally (fetch=TRUE) fetches results using sqlGetResults.

Optionally (query=NULL) uses query already prepared by sqlPrepare.

Usage

sqlExecute(channel, query = NULL, data = NULL, fetch = FALSE,
  errors = TRUE, rows_at_time = attr(channel, "rows_at_time"), ...)

Arguments

channel
ODBC connection obtained by odbcConnect
query
a query string (NULL if query already prepared using sqlPrepare)
data
data to pass to sqlExecute (as data.frame)
fetch
whether to automatically fetch results (if data provided)
errors
whether to display errors
rows_at_time
number of rows to fetch at one time - see details of sqlQuery
...
parameters to pass to sqlGetResults (if fetch=TRUE)

Value

  • see datails

Details

Return value depends on the combination of parameters:
  • if there were errors during query preparation or execution or fetching results return value depends on errors parameter - if errors=TRUE error is thrown, otherwise -1 will be returned
  • if fetch=FALSE and there were no errors invisible(1) will be returned
  • if fetch=TRUE and there were no errors a data.frame with results will be returned

Examples

Run this code
conn = odbcConnect('MyDataSource')

  # prepare, execute and fetch results separatly
  sqlPrepare(conn, "SELECT * FROM myTable WHERE column = ?")
  sqlExecute(conn, NULL, 'myValue')
  sqlGetResults(conn)

  # prepare and execute at one time, fetch results separately
  sqlExecute(conn, "SELECT * FROM myTable WHERE column = ?", 'myValue')
  sqlGetResults(conn)

  # prepare, execute and fetch at one time
  sqlExecute(conn, "SELECT * FROM myTable WHERE column = ?", 'myValue', TRUE)

  # prepare, execute and fetch at one time, pass additional parameters to sqlFetch()
  sqlExecute(
    conn,
    "SELECT * FROM myTable WHERE column = ?",
    'myValue',
    TRUE,
    stringsAsFactors=FALSE
  )

Run the code above in your browser using DataLab