简体   繁体   English

Power Query将Excel中的字符串分配给SQL Server变量

[英]Power Query Assign a string from excel to sql server variable

I am using Power query & I would like to assign a comma delimited string from an excel cell to a sql server variable. 我正在使用Power查询,我想将一个逗号分隔的字符串从excel单元格分配给sql服务器变量。 The power query I have so far is below. 到目前为止,我的电源查询如下。 My Parameter is in a Excel "Table3" in "Column2" : 我的参数在“ Column2”中的Excel“ Table3”中:

let

    ProdParameterSource =Excel.CurrentWorkbook(){[Name="Table3"]}[Content]{0}[Column2],


    Source = Sql.Database("server", "database", [Query="Declare @Product varchar(max) = "&(ProdParameterSource)&"  select @Product"])
in
    Source

I see the below error : 我看到以下错误:

在此处输入图片说明

The parameters I am using as seen in the excel sheet are below : 我在excel工作表中看到的参数如下:

在此处输入图片说明

How can I fix this error and see my input parameters in the sql server variable ABC as '44,216' (with the inverted comma). 如何解决此错误,并在sql server变量ABC中看到我的输入参数为'44,216'(带反逗号)。

I do not do a lot of power query but I do work in Power BI Desktop version. 我并没有进行很多电源查询,但是我确实在Power BI Desktop版本中工作。

First, you need to look at the reference to the SQL.Database() on MSDN . 首先,您需要查看对MSDN上SQL.Database()的引用。

It is looking for a whole query, either a dynamic string you make up or a call to a stored procedure. 它正在寻找一个完整的查询,要么是您组成的动态字符串,要么是对存储过程的调用。

My simple example below, pulls data from the Adventure Works DW 2012 Customer table. 我下面的简单示例从Adventure Works DW 2012 Customer表中提取数据。

在此处输入图片说明

What you are missing is a TSQL statement or multiple TSQL statements. 您缺少的是一个TSQL语句或多个TSQL语句。 Use the semicolon to combine statements into one batch to be called. 使用分号将语句合并为一个要调用的批处理。

The example below create a variable @x as an integer and returns the value. 下面的示例将变量@x创建为整数并返回值。 This is almost like you example above. 这几乎就像您上面的示例。

在此处输入图片说明

You will be prompted for security (trusted credentials) and it will tell you that it is an unencrypted connection. 系统将提示您输入安全性(受信任的凭据),它会告诉您这是未加密的连接。

I did some looking around the NET. 我做了一些环顾网上的事情。 This article from Reeves Smith is like my last example but slanted towards power query and excel. 里夫斯·史密斯(Reeves Smith)的这篇文章就像我的最后一个例子一样,但偏向于功率查询和excel。 Just remember, both products use the same engine. 请记住,两种产品使用相同的引擎。

https://reevessmith.wordpress.com/2014/08/19/power-query-and-stored-procedures-with-parameters/ https://reevessmith.wordpress.com/2014/08/19/power-query-and-stored-procedures-with-parameters/

Again 再次

This will fix it: [Query="Declare @Product varchar(max) = '" & ProdParameterSource & "' select @Product"]) . 这将解决它: [Query="Declare @Product varchar(max) = '" & ProdParameterSource & "' select @Product"])

This will fix it in a safer way, since you will escape any extra single-quotes which could break out of the string value: [Query="Declare @Product varchar(max) = '" & Text.Replace(ProdParameterSource, "'", "''") &"' select @Product"]) . 这将以一种更安全的方式修复它,因为您将转义可能会破译字符串值的所有多余的单引号: [Query="Declare @Product varchar(max) = '" & Text.Replace(ProdParameterSource, "'", "''") &"' select @Product"])

What happened is that Power Query treats the text passed to Query as the entire script, and when you built the script you didn't put the value of ProdParameterSource in quotes, so the script appears to set a varchar value to 44,216 (without quotes). 发生的事情是Power Query将传递给Query的文本视为整个脚本,并且在构建脚本时,您并未将ProdParameterSource的值放在引号中,因此该脚本似乎将varchar值设置为44,216(不带引号) 。 This is an invalid statement. 这是无效的陈述。

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

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