【问题标题】:Follow the MS tutorial, but see the error [Microsoft][ODBC Driver Manager] Connection not open ODBC Error in SQLDisconnect按照 MS 教程,但看到错误 [Microsoft][ODBC Driver Manager] Connection not open ODBC Error in SQLDisconnect
【发布时间】:2018-06-14 21:37:55
【问题描述】:

使用以下 Microsoft SQL Server R 机器学习链接,我可以执行到第 2 步,一切正常:

https://microsoft.github.io/sql-ml-tutorials/R/customerclustering/step/3.html

但是当我尝试在 SQL SERVER 2017 中部署 ML 模型时,我总是会看到以下问题:

消息 39004,级别 16,状态 20,行 122 发生“R”脚本错误 在使用 HRESULT 执行“sp_execute_external_script”期间 0x80004004。消息 39019,级别 16,状态 2,第 122 行外部脚本 发生错误:

[Microsoft][ODBC SQL Server Driver][SQL Server]用户登录失败 '华硕\MSSQLSERVER01'。

无法打开数据源。 doTryCatch 中的错误(返回(表达式),名称, parentenv,处理程序):无法打开数据源。来电:来源... tryCatch -> tryCatchList -> tryCatchOne -> doTryCatch -> .Call

执行错误。检查输出以获取更多信息。错误 eval(expr, envir, enclos) : 执行错误。检查输出 了解更多信息。调用:source -> withVisible -> eval -> eval -> .调用执行停止

来自外部脚本的 STDOUT 消息:

[Microsoft][ODBC Driver Manager] 连接未打开

SQLDisconnect 中的 ODBC 错误

我的代码如下所示:

USE [tpcxbb_1gb]
DROP PROC IF EXISTS generate_customer_return_clusters;
GO
CREATE procedure [dbo].[generate_customer_return_clusters]
AS
/*
  This procedure uses R to classify customers into different groups based on their
  purchase & return history.
*/

BEGIN
    DECLARE @duration FLOAT
    , @instance_name NVARCHAR(100) = @@SERVERNAME
    , @database_name NVARCHAR(128) = db_name()
-- Input query to generate the purchase history & return metrics
    , @input_query NVARCHAR(MAX) = N'
SELECT
  ss_customer_sk AS customer,
  round(CASE WHEN ((orders_count = 0) OR (returns_count IS NULL) OR (orders_count IS NULL) OR ((returns_count / orders_count) IS NULL) ) THEN 0.0 ELSE (cast(returns_count as nchar(10)) / orders_count) END, 7) AS orderRatio,
  round(CASE WHEN ((orders_items = 0) OR(returns_items IS NULL) OR (orders_items IS NULL) OR ((returns_items / orders_items) IS NULL) ) THEN 0.0 ELSE (cast(returns_items as nchar(10)) / orders_items) END, 7) AS itemsRatio,
  round(CASE WHEN ((orders_money = 0) OR (returns_money IS NULL) OR (orders_money IS NULL) OR ((returns_money / orders_money) IS NULL) ) THEN 0.0 ELSE (cast(returns_money as nchar(10)) / orders_money) END, 7) AS monetaryRatio,
  round(CASE WHEN ( returns_count IS NULL                                                                        ) THEN 0.0 ELSE  returns_count                 END, 0) AS frequency

FROM
  (
    SELECT
      ss_customer_sk,
      -- return order ratio
      COUNT(distinct(ss_ticket_number)) AS orders_count,
      -- return ss_item_sk ratio
      COUNT(ss_item_sk) AS orders_items,
      -- return monetary amount ratio
      SUM( ss_net_paid ) AS orders_money
    FROM store_sales s
    GROUP BY ss_customer_sk
  ) orders
  LEFT OUTER JOIN
  (
    SELECT
      sr_customer_sk,
      -- return order ratio
      count(distinct(sr_ticket_number)) as returns_count,
      -- return ss_item_sk ratio
      COUNT(sr_item_sk) as returns_items,
      -- return monetary amount ratio
      SUM( sr_return_amt ) AS returns_money
    FROM store_returns
    GROUP BY sr_customer_sk
  ) returned ON ss_customer_sk=sr_customer_sk
 '
EXEC sp_execute_external_script
      @language = N'R'
    , @script = N'
# Define the connection string
connStr <- paste("Driver=SQL Server;Server=", instance_name, ";Database=", database_name, ";Trusted_Connection=true;" , sep="" );


# Input customer data that needs to be classified. This is the result we get from our query
customer_returns <- RxSqlServerData(sqlQuery = input_query,
                                    colClasses = c(customer = "numeric", orderRatio = "numeric", itemsRatio = "numeric", monetaryRatio = "numeric", frequency = "numeric"),
                                    connectionString = connStr);

# Output table to hold the customer cluster mappings
return_cluster = RxSqlServerData(table = "customer_return_clusters", connectionString = connStr);

# set.seed for random number generator for predicatability
set.seed(10);

# generate clusters using rxKmeans and output clusters to a table called "customer_return_clusters".
clust <- rxKmeans( ~ orderRatio + itemsRatio + monetaryRatio + frequency, customer_returns, numClusters = 4
                    , outFile = return_cluster, outColName = "cluster", writeModelVars = TRUE , extraVarsToWrite = c("customer"), overwrite = TRUE);
'
    , @input_data_1 = N''
    , @params = N'@instance_name nvarchar(100), @database_name nvarchar(128), @input_query nvarchar(max), @duration float OUTPUT'
    , @instance_name = @instance_name
    , @database_name = @database_name
    , @input_query = @input_query
    , @duration = @duration OUTPUT;
END;

GO

-- Perform clustering in SQL Server
-- We are now going to execute the stored procedure in SQL Server to perform the clustering
-- Empty table of the results before running the stored procedure
TRUNCATE TABLE customer_return_clusters;
--Execute the clustering. This will load the table customer_return_clusters with cluster mappings
EXEC [dbo].[generate_customer_return_clusters];

错误会出现在最后一行。

我怀疑问题出在连接 constr 行上:

connStr <- paste("Driver=SQL Server;Server=", instance_name, ";Database=", database_name, ";Trusted_Connection=true;" , sep="" );

我检查并删除了不必要的空间,但它不起作用。

谁能帮我解决这个问题?

【问题讨论】:

    标签: sql-server r tsql


    【解决方案1】:

    sp_execute_external_script 在工作人员帐户下运行 R 脚本并让 Windows 身份验证在这种情况下工作,您需要 Enable implied authentication for the Launchpad account group

    【讨论】:

      【解决方案2】:

      尝试使用Driver={ODBC Driver 13 for SQL Server}。

      对我来说它抛出了这个错误:

      Driver={SQL Server} 的错误

      【讨论】:

        【解决方案3】:

        对我来说,解决方案是将 SQLRUserGroup 添加为相关数据库的 db_owner,您可以按照以下步骤操作: http://www.kodyaz.com/t-sql/r-library-rodbc-script-to-connect-sql-server-database.aspx 转到最后一节

        希望对你有帮助,我正要放弃这最后一步

        最好的

        【讨论】:

          猜你喜欢
          • 2016-04-19
          • 2014-09-15
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-06-28
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多