2017-03-02 69 views
0

我是SQL新手,嘗試使用Power Query將Great Plains數據直接使用UDF向Excel中發送,我發現here可以傳遞參數從工作簿到查詢。這裏是UDF:Microsoft SQL:無法找到列「dbo」或用戶定義的函數或聚合

(ParameterName as text) => 
let 
ParamSource = Excel.CurrentWorkbook(){[Name="Parameters"]}[Content], 
ParamRow = Table.SelectRows(ParamSource, each ([Parameter] = ParameterName)), 
Value= 
if Table.IsEmpty(ParamRow)=true 
then null 
else Record.Field(ParamRow{0},"Value") 
in 
Value 

,這裏是SQL查詢:

Declare @accrualDate Date 
set @accrualDate = dbo.fnGetParameter("Accrual Date") 
Declare @lookback Date 
set @lookback = dbo.fnGetParameter("Lookback for Automated Accruals") 
Declare @lookbackBOP Date 
set @lookbackBOP = dbo.fnGetParameter("Lookback for Manual Accruals - BOP") 
Declare @lookbackEOP Date 
set @lookbackEOP = dbo.fnGetParameter("Lookback for Manual Accruals - EOP") 

SELECT 
[Open Year], 
[History Year], 
[Period ID], 
[TRX Date], 
[Account Number], 
[Account Description], 
[Journal Entry], 
[description], 
[reference], 
[Originating Posted Date], 
Sum([Debit Amount]) AS [Debit Amount], 
Sum([Credit Amount]) AS [Credit Amount] , 

Segment1, 
Segment2, 
Segment3, 
Segment4, 
Segment5, 
Segment6, 
Segment7 

FROM ['dbName'].[dbo].[AccountTransactions] 
WHERE Segment2 BETWEEN 6000 AND 6999 
and [description] = 'Purchases' 
and [TRX Date] BETWEEN @lookback AND @accrualDate 
GROUP BY [Open Year], [History Year], [Period ID], [TRX Date], [Account Number], [Account Description], [Journal Entry],[description],[reference], [Originating Posted Date], Segment1, Segment2, Segment3, Segment4, Segment5, Segment6, Segment7 

我已經通過自身運行UDF,並且已經證實,它從我的「參數」表返回預期值。但是,運行查詢時出現以下錯誤:

DataSource.Error: Microsoft SQL: Cannot find either column "dbo" or the user-defined function or aggregate "dbo.fnGetParameter", or the name is ambiguous. Details: DataSourceKind=SQL DataSourcePath=dcb-gp15-sql.us.medsolutions.com;MED01 Message=Cannot find either column "dbo" or the user-defined function or aggregate "dbo.fnGetParameter", or the name is ambiguous. Number=4121 Class=16

任何幫助將不勝感激。

+0

什麼是'dbo.fnGetParameter'?如果它是[表值函數](https://technet.microsoft.com/en-us/library/ms191165(v = sql.105).aspx),那麼你不能將它用作標量函數 –

+0

是' fnGetParameter是一個Excel函數?目前尚不清楚。 – DavidG

+0

fnGetParameter是我使用Power Query> From Other Sources>空白查詢>高級編輯器創建的UDF的名稱。對不起,我應該在問題中更清楚一點。 – battery514

回答

1

自定義「M」功能fnGetParameter僅在Power Query中運行混搭時定義,並且未發送到服務器。

如果您從Power Query中指定了原生SQL查詢,那就是我們發送給服務器的字面意思。 Power Query不會爲您提供在服務器上定義自定義UDF的方法。


相反,你可以導入AccountTransactions表到電源的查詢,並使用「M」的轉換來轉換表。我建議使用用戶界面來生成行過濾器和求和聚合,然後自定義「M」以使用您的客戶過濾器。

例如你的步驟之一會是這樣的:

FilteredRows3 = Table.SelectRows(
    FilteredRows2, 
    each 
     fnGetParameter("Lookback for Automated Accruals") < [TRX Date] 
     and 
     [TRX Date] < fnGetParameter("Accrual Date") 
) 
相關問題