unable to write mysql datatable into csv file using R Script

0

Here i am using the following R external script to write sql database tableas csv file.

I know how to export the data using export and import wizard.

but i have to export data using scripts.

        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;

note:

  1. fcst_model is database table
  2. it has two columns

while executing the script i got the following error.

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

can anyone help me to solve this issue.

Thanks in advance.

mysql
r
export-to-csv
asked on Stack Overflow Feb 4, 2019 by neeraja

1 Answer

0
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"
answered on Stack Overflow Feb 11, 2019 by neeraja

User contributions licensed under CC BY-SA 3.0