简体   繁体   English

带R的SQl Server 2016

[英]SQl Server 2016 with R

I have the below Query 我有以下查询

DECLARE @speedmodel varbinary(max) = (SELECT [model] FROM [dbo].[stopping_distance_models] WHERE model_name = 'latest model');
EXEC sp_execute_external_script
    @language = N'R'
    , @script = N'
            current_model <- unserialize(as.raw(speedmodel));
            new <- data.frame(NewCarData);
            predicted.distance <- rxPredict(current_model, new);
            str(predicted.distance);
            OutputDataSet <- cbind(new, ceiling(predicted.distance));
            '
    , @input_data_1 = N' SELECT speed FROM [dbo].[NewCarSpeed] '
    , @input_data_1_name = N'NewCarData'
    , @params = N'@speedmodel varbinary(max)'
    , @speedmodel = @speedmodel
WITH RESULT SETS (([new_speed] INT, [predicted_distance] INT))

while running this code I got an error message 在运行此代码时,我收到一条错误消息

Msg 39004, Level 16, State 20, Line 1 A 'R' script error occurred during execution of 'sp_execute_external_script' with HRESULT 0x80004004. 消息39004,级别16,状态20,第1行1在执行带有HRESULT 0x80004004的“ sp_execute_external_script”时执行“ R”脚本错误。 Msg 39019, Level 16, State 1, Line 1 An external script error occurred: Error in unserialize(as.raw(speedmodel)) : read error Calls: source -> withVisible -> eval -> eval -> unserialize 消息39019,级别16,状态1,第1行发生外部脚本错误:反序列化错误(as.raw(speedmodel)):读取错误调用:源-> withVisible-> eval-> eval->反序列化

Error in ScaleR. ScaleR中的错误。 Check the output for more information. 检查输出以获取更多信息。 Error in eval(expr, envir, enclos) : Error in ScaleR. eval(expr,envir,enclos)中的错误:ScaleR中的错误。 Check the output for more information. 检查输出以获取更多信息。 Calls: source -> withVisible -> eval -> eval -> .Call Execution halted Msg 11536, Level 16, State 1, Line 2 EXECUTE statement failed because its WITH RESULT SETS clause specified 1 result set(s), but the statement only sent 0 result set(s) at run time. 调用:source-> withVisible-> eval-> eval-> .Call Execution暂停Msg 11536,级别16,状态1,第2行EXECUTE语句失败,因为其WITH RESULT SETS子句指定了1个结果集,但该语句仅在运行时发送了0个结果集。

Why I'm getting this error? 为什么我会收到此错误?

Did you even connect ro your SQL Server DB? 您甚至没有连接您的SQL Server数据库?

library("RODBC")
#library("XLConnect")

dbhandle <- odbcDriverConnect('driver={SQL Server};server=Name_Of_Server;database=Name_Of_DB;trusted_connection=true')
currTableSQL<-paste("SELECT * From Your_Table",sep="")
currTableDF<-sqlQuery(dbhandle,currTableSQL)

Uncomment the XLConnect if you want to utilize that library. 如果要使用该库,请取消注释XLConnect。 I feel like a lot of times if you are using SQL Server, you are using Excel as well. 我感觉很多时候,如果您使用的是SQL Server,也同时使用Excel。

Or . 要么 。 . .

library(RODBC)
dbconnection <- odbcDriverConnect("Driver=ODBC Driver 11 for SQL Server;Server=YourServerName; Database=YourDBName;Uid=; Pwd=; trusted_connection=yes")
initdata <- sqlQuery(dbconnection,paste("select * from MyTable;"))
odbcClose(channel)

Check out these links: 查看以下链接:

https://andersspur.wordpress.com/2013/11/26/connect-r-to-sql-server-2012-and-14/ https://andersspur.wordpress.com/2013/11/26/connect-r-to-sql-server-2012-and-14/

Finally, make sure SQL Server has all proper permissions applied. 最后,确保SQL Server拥有所有适当的权限。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM