Skip to contents

sqlAppendTable() generates a single SQL string that inserts a data frame into an existing table. sqlAppendTableTemplate() generates a template suitable for use with dbBind(). The default methods are ANSI SQL 99 compliant. These methods are mostly useful for backend implementers.

Methods in other packages

This documentation page describes the generics. Refer to the documentation pages linked below for the documentation for the methods that are implemented in various backend packages.

Usage

sqlAppendTable(con, table, values, row.names = NA, ...)

sqlAppendTableTemplate(
  con,
  table,
  values,
  row.names = NA,
  prefix = "?",
  ...,
  pattern = ""
)

Arguments

con

A database connection.

table

The table name, passed on to dbQuoteIdentifier(). Options are:

  • a character string with the unquoted DBMS table name, e.g. "table_name",

  • a call to Id() with components to the fully qualified table name, e.g. Id(schema = "my_schema", table = "table_name")

  • a call to SQL() with the quoted and fully qualified table name given verbatim, e.g. SQL('"my_schema"."table_name"')

values

A data frame. Factors will be converted to character vectors. Character vectors will be escaped with dbQuoteString().

row.names

Either TRUE, FALSE, NA or a string.

If TRUE, always translate row names to a column called "row_names". If FALSE, never translate row names. If NA, translate rownames only if they're a character vector.

A string is equivalent to TRUE, but allows you to override the default name.

For backward compatibility, NULL is equivalent to FALSE.

...

Other arguments used by individual methods.

prefix

Parameter prefix to use for placeholders.

pattern

Parameter pattern to use for placeholders:

  • "": no pattern

  • "1": position

  • anything else: field name

Details

The row.names argument must be passed explicitly in order to avoid a compatibility warning. The default will be changed in a later release.

Examples

sqlAppendTable(ANSI(), "iris", head(iris))
#> Warning: Do not rely on the default value of the row.names argument for sqlAppendTable(), it will change in the future.
#> <SQL> INSERT INTO "iris"
#>   ("Sepal.Length", "Sepal.Width", "Petal.Length", "Petal.Width", "Species")
#> VALUES
#>   (5.09999999999999964e+00, 3.50000000000000000e+00, 1.39999999999999991e+00, 2.00000000000000011e-01, 'setosa'),
#>   (4.90000000000000036e+00, 3.00000000000000000e+00, 1.39999999999999991e+00, 2.00000000000000011e-01, 'setosa'),
#>   (4.70000000000000018e+00, 3.20000000000000018e+00, 1.30000000000000004e+00, 2.00000000000000011e-01, 'setosa'),
#>   (4.59999999999999964e+00, 3.10000000000000009e+00, 1.50000000000000000e+00, 2.00000000000000011e-01, 'setosa'),
#>   (5.00000000000000000e+00, 3.60000000000000009e+00, 1.39999999999999991e+00, 2.00000000000000011e-01, 'setosa'),
#>   (5.40000000000000036e+00, 3.89999999999999991e+00, 1.69999999999999996e+00, 4.00000000000000022e-01, 'setosa')

sqlAppendTable(ANSI(), "mtcars", head(mtcars))
#> Warning: Do not rely on the default value of the row.names argument for sqlAppendTable(), it will change in the future.
#> <SQL> INSERT INTO "mtcars"
#>   ("row_names", "mpg", "cyl", "disp", "hp", "drat", "wt", "qsec", "vs", "am", "gear", "carb")
#> VALUES
#>   ('Mazda RX4', 2.10000000000000000e+01, 6.00000000000000000e+00, 1.60000000000000000e+02, 1.10000000000000000e+02, 3.89999999999999991e+00, 2.62000000000000011e+00, 1.64600000000000009e+01, 0.00000000000000000e+00, 1.00000000000000000e+00, 4.00000000000000000e+00, 4.00000000000000000e+00),
#>   ('Mazda RX4 Wag', 2.10000000000000000e+01, 6.00000000000000000e+00, 1.60000000000000000e+02, 1.10000000000000000e+02, 3.89999999999999991e+00, 2.87500000000000000e+00, 1.70199999999999996e+01, 0.00000000000000000e+00, 1.00000000000000000e+00, 4.00000000000000000e+00, 4.00000000000000000e+00),
#>   ('Datsun 710', 2.28000000000000007e+01, 4.00000000000000000e+00, 1.08000000000000000e+02, 9.30000000000000000e+01, 3.85000000000000009e+00, 2.31999999999999984e+00, 1.86099999999999994e+01, 1.00000000000000000e+00, 1.00000000000000000e+00, 4.00000000000000000e+00, 1.00000000000000000e+00),
#>   ('Hornet 4 Drive', 2.13999999999999986e+01, 6.00000000000000000e+00, 2.58000000000000000e+02, 1.10000000000000000e+02, 3.08000000000000007e+00, 3.21499999999999986e+00, 1.94400000000000013e+01, 1.00000000000000000e+00, 0.00000000000000000e+00, 3.00000000000000000e+00, 1.00000000000000000e+00),
#>   ('Hornet Sportabout', 1.86999999999999993e+01, 8.00000000000000000e+00, 3.60000000000000000e+02, 1.75000000000000000e+02, 3.14999999999999991e+00, 3.43999999999999995e+00, 1.70199999999999996e+01, 0.00000000000000000e+00, 0.00000000000000000e+00, 3.00000000000000000e+00, 2.00000000000000000e+00),
#>   ('Valiant', 1.81000000000000014e+01, 6.00000000000000000e+00, 2.25000000000000000e+02, 1.05000000000000000e+02, 2.75999999999999979e+00, 3.45999999999999996e+00, 2.02199999999999989e+01, 1.00000000000000000e+00, 0.00000000000000000e+00, 3.00000000000000000e+00, 1.00000000000000000e+00)
sqlAppendTable(ANSI(), "mtcars", head(mtcars), row.names = FALSE)
#> <SQL> INSERT INTO "mtcars"
#>   ("mpg", "cyl", "disp", "hp", "drat", "wt", "qsec", "vs", "am", "gear", "carb")
#> VALUES
#>   (2.10000000000000000e+01, 6.00000000000000000e+00, 1.60000000000000000e+02, 1.10000000000000000e+02, 3.89999999999999991e+00, 2.62000000000000011e+00, 1.64600000000000009e+01, 0.00000000000000000e+00, 1.00000000000000000e+00, 4.00000000000000000e+00, 4.00000000000000000e+00),
#>   (2.10000000000000000e+01, 6.00000000000000000e+00, 1.60000000000000000e+02, 1.10000000000000000e+02, 3.89999999999999991e+00, 2.87500000000000000e+00, 1.70199999999999996e+01, 0.00000000000000000e+00, 1.00000000000000000e+00, 4.00000000000000000e+00, 4.00000000000000000e+00),
#>   (2.28000000000000007e+01, 4.00000000000000000e+00, 1.08000000000000000e+02, 9.30000000000000000e+01, 3.85000000000000009e+00, 2.31999999999999984e+00, 1.86099999999999994e+01, 1.00000000000000000e+00, 1.00000000000000000e+00, 4.00000000000000000e+00, 1.00000000000000000e+00),
#>   (2.13999999999999986e+01, 6.00000000000000000e+00, 2.58000000000000000e+02, 1.10000000000000000e+02, 3.08000000000000007e+00, 3.21499999999999986e+00, 1.94400000000000013e+01, 1.00000000000000000e+00, 0.00000000000000000e+00, 3.00000000000000000e+00, 1.00000000000000000e+00),
#>   (1.86999999999999993e+01, 8.00000000000000000e+00, 3.60000000000000000e+02, 1.75000000000000000e+02, 3.14999999999999991e+00, 3.43999999999999995e+00, 1.70199999999999996e+01, 0.00000000000000000e+00, 0.00000000000000000e+00, 3.00000000000000000e+00, 2.00000000000000000e+00),
#>   (1.81000000000000014e+01, 6.00000000000000000e+00, 2.25000000000000000e+02, 1.05000000000000000e+02, 2.75999999999999979e+00, 3.45999999999999996e+00, 2.02199999999999989e+01, 1.00000000000000000e+00, 0.00000000000000000e+00, 3.00000000000000000e+00, 1.00000000000000000e+00)
sqlAppendTableTemplate(ANSI(), "iris", iris)
#> Warning: Do not rely on the default value of the `row.names` argument to `sqlAppendTableTemplate()`, it will change in the future.
#> <SQL> INSERT INTO "iris"
#>   ("Sepal.Length", "Sepal.Width", "Petal.Length", "Petal.Width", "Species")
#> VALUES
#>   (?, ?, ?, ?, ?)

sqlAppendTableTemplate(ANSI(), "mtcars", mtcars)
#> Warning: Do not rely on the default value of the `row.names` argument to `sqlAppendTableTemplate()`, it will change in the future.
#> <SQL> INSERT INTO "mtcars"
#>   ("row_names", "mpg", "cyl", "disp", "hp", "drat", "wt", "qsec", "vs", "am", "gear", "carb")
#> VALUES
#>   (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
sqlAppendTableTemplate(ANSI(), "mtcars", mtcars, row.names = FALSE)
#> <SQL> INSERT INTO "mtcars"
#>   ("mpg", "cyl", "disp", "hp", "drat", "wt", "qsec", "vs", "am", "gear", "carb")
#> VALUES
#>   (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)