将 R 数据帧插入 SQL (RODBC) - 找不到错误 table
Insert R dataframe into SQL (RODBC) - error table not found
我想从 R 中删除我的整个数据帧,最好使用带有 sqlSave
语句(而不是 sqlQuery
)的 RODBC。这是我的示例代码。
library(RODBC)
myconn <- odbcDriverConnect("some connection string")
mydf <- data.frame(col_1 = c(1,2,3), col_2 = c(2,3,4))
sqlSave(myconn, mydf, tablename = '[some_db].[some_schema].[my_table]', append = F, rownames = F, verbose=TRUE)
odbcClose(myconn)
执行后,返回错误信息:
Error in sqlColumns(channel, tablename) :
‘my_table’: table not found on channel
当我签入 SQL 服务器时,出现一个空的 table。
如果我再次 运行 相同的代码,我会收到错误消息:
Error in sqlSave(myconn, mydf, tablename = "[some_db].[some_schema].[my_table]", :
42S01 2714 [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]There is already an object named 'my_table' in the database.
[RODBC] ERROR: Could not SQLExecDirect 'CREATE TABLE [some_db].[some_schema].[my_table] ("col_1" float, "col_2" float)'
关于如何解决问题有什么建议吗?
更新
在 SSMS 中,我可以 运行 成功执行以下命令:
CREATE TABLE [some_db].[some_schema].[my_table] (
test int
);
drop table [some_db].[some_schema].[my_table]
以下是连接字符串的详细信息:
Driver=ODBC Driver 17 for SQL Server; Server=someserveraddress; Uid=user_login; Pwd=some_password
为避免错误,您可以 specify the database in the connection string:
Driver=ODBC Driver 17 for SQL Server; Server = someserveraddress; database = some_db; Uid = user_login; Pwd = some_password
和avoid using brackets:
sqlSave(myconn, mydf, tablename = 'some_schema.my_table', append = F, rownames = F, verbose=TRUE)
我想从 R 中删除我的整个数据帧,最好使用带有 sqlSave
语句(而不是 sqlQuery
)的 RODBC。这是我的示例代码。
library(RODBC)
myconn <- odbcDriverConnect("some connection string")
mydf <- data.frame(col_1 = c(1,2,3), col_2 = c(2,3,4))
sqlSave(myconn, mydf, tablename = '[some_db].[some_schema].[my_table]', append = F, rownames = F, verbose=TRUE)
odbcClose(myconn)
执行后,返回错误信息:
Error in sqlColumns(channel, tablename) : ‘my_table’: table not found on channel
当我签入 SQL 服务器时,出现一个空的 table。
如果我再次 运行 相同的代码,我会收到错误消息:
Error in sqlSave(myconn, mydf, tablename = "[some_db].[some_schema].[my_table]", : 42S01 2714 [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]There is already an object named 'my_table' in the database. [RODBC] ERROR: Could not SQLExecDirect 'CREATE TABLE [some_db].[some_schema].[my_table] ("col_1" float, "col_2" float)'
关于如何解决问题有什么建议吗?
更新
在 SSMS 中,我可以 运行 成功执行以下命令:
CREATE TABLE [some_db].[some_schema].[my_table] (
test int
);
drop table [some_db].[some_schema].[my_table]
以下是连接字符串的详细信息:
Driver=ODBC Driver 17 for SQL Server; Server=someserveraddress; Uid=user_login; Pwd=some_password
为避免错误,您可以 specify the database in the connection string:
Driver=ODBC Driver 17 for SQL Server; Server = someserveraddress; database = some_db; Uid = user_login; Pwd = some_password
和avoid using brackets:
sqlSave(myconn, mydf, tablename = 'some_schema.my_table', append = F, rownames = F, verbose=TRUE)