【问题标题】:unable to write mysql datatable into csv file using R Script无法使用 R 脚本将 mysql 数据表写入 csv 文件
【发布时间】:2019-02-04 17:47:23
【问题描述】:

这里我使用以下 R 外部脚本将 sql 数据库表写入 csv 文件。

我知道如何使用导出和导入向导导出数据。

但我必须使用脚本导出数据。

        declare @file_path varchar(300)
        select @file_path = 'C:/NB/DATA/DB/arima.csv' 
        EXEC sp_execute_external_script
            @language = N'R'
            ,@script = N'
             write.csv(data,file=file_path,row.names=FALSE);'
            ,@input_data_1_name = N'data'
            ,@input_data_1= N'select * from [dbo].[fcst_model]'
            ,@params = N'@file_path varchar(300)' 
            ,@file_path = @file_path;

注意:

  1. fcst_model 是数据库表
  2. 它有两列

执行脚本时出现以下错误。

Msg 39004, Level 16, State 20, Line 305
A 'R' script error occurred during execution of 'sp_execute_external_script' 
with HRESULT 0x80004004.
Msg 39019, Level 16, State 2, Line 305
An external script error occurred: 
Error in file(file, ifelse(append, "a", "w")) : 
cannot open the connection
Calls: source ... write.csv -> eval.parent -> eval -> eval -> write.table -> 
file
In addition: Warning message:
In file(file, ifelse(append, "a", "w")) :
cannot open file 'C:/NB/DATA/DB/arima.csv': Permission denied

谁能帮我解决这个问题。

提前致谢。

【问题讨论】:

  • 你能解决你的问题吗?
  • 是的,实际上在早些时候我没有权限在 C 驱动器中写入文件,现在我可以访问它们然后我通过将 file_path 作为参数传递给存储过程来运行代码。谢谢你的提问..
  • 我通过为目标文件夹分配所有应用程序包权限解决了我的问题。无论如何,很好,我们都解决了我们的问题:)

标签: mysql r export-to-csv


【解决方案1】:
drop proc if exists to_csv;
go
create proc to_csv(@param1 varchar(MAX))
as
begin
    exec sp_execute_external_script
    @language =  N'R'
    ,@script = N'           
                write.csv(df,output_path)'
    ,@input_data_1 = N'select * from fcst_data1'
    ,@input_data_1_name=N'df'
    ,@params = N'@output_path varchar(MAX) '
    ,@output_path = @param1;
end;
go

exec to_csv "C:\\NB\\DB\\SQL_R\\data\\data.csv"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-12-29
    • 2020-10-09
    • 1970-01-01
    • 1970-01-01
    • 2012-05-28
    • 1970-01-01
    • 2015-07-21
    • 1970-01-01
    相关资源
    最近更新 更多