tomroh / bcputility

R package for fast bulk imports/exports from/to SQL Server with the bcp command line utility
https://bcputility.roh.engineering
Other
14 stars 2 forks source link

Document requirement for using DBI::SQL() when specifying schema of destination table #14

Closed mkoohafkan closed 2 years ago

mkoohafkan commented 2 years ago

This does not work:

bcpImport(..., table = "dbo.mytable")

But this does:

bcpImport(..., table = DBI::SQL("dbo.mytable"))

Easy fix, but the only way I figured it out was looking at the source code: https://github.com/tomroh/bcputility/blob/6fb9764e5ebb353c2f82a2c3bccff7d777764c62/R/bcp.R#L217-L220

This error actually occurs for both spatial and non-spatial tables, but it looks like the inherits(table, 'SQL')check only happens for spatial tables.

tomroh commented 2 years ago

I see your point that this is tricky for the end user. I'll add in the documentation a note for specifying schema. This issue is actually a DBI issue. bcp can handle the above and that is actually what is going on even when DBI::SQL is used. The check in the "spatial" part is to protect the user from an accidental SQL injection. I was assuming dbWriteTable and dbCreateTable create sql statements the same way but this is not the case so you are seeing an error because dbCreateTable writes "dbo.dbo.tablename" to the database. I should probably include DBI::Id as a method that will pass through too.

For your issue, this depends on the SQL Server config but usually "dbo" is the default schema and you do not need to specify it. bcpImport(..., table = "mytable") should write "dbo.mytable" to the db.

tomroh commented 2 years ago
tomroh commented 2 years ago

b12f8bb6e90b4c88ebe9e0494c0d5fefbc24ae5e

This will be in the next release.

devtools::install_github('tomroh/bcputility@devel')