2015-12-21 146 views
0

我試圖通過將查詢結果傳遞給我的參數來獲取存儲過程的結果。將查詢結果傳遞給存儲過程中的參數

ALTER PROCEDURE [DWH].[spAMBSiteAssetCountReport] 
    @Areaname, @SiteType, @EquipmentClass, @AssetStatus 
    @MaintenanceLocation varchar(Max), 
    @FLClassDescription varchar(max), 
    @EquipmentClass varchar(max), 
    @AssetStatus varchar(max) 
As 
Begin 
    SET NOCOUNT ON; 

    SET ANSI_WARNINGS OFF -- to suppress warning "string or binary data would be truncated" 

    SELECT 
     EC.ClassDescription, FL.Site, 
     SUBSTRING(FL.Site, CHARINDEX('-',FL.Site)+1,LEN(FL.Site)) SiteNo, 
     FL.SiteDesc, FL.FunctionalLocation, FL.Parish, 
     ST.SiteTypeName FLClassDescription, FL.MaintenanceLocationDesc, 
     Count(1) AS AssetCount 
    FROM  
     DWH.DimFunctionalLocation AS FL 
    INNER JOIN 
     DWH.DimEquipment AS EQ ON EQ.FunctionalLocationKey = FL.DW_FunctionalLocation_Key 
    INNER JOIN 
     DWH.DimEquipmentDetail AS EQD ON EQ.DW_Equipment_Key = EQD.EquipmentKey 
    INNER JOIN 
     DWH.DimEquipmentClass AS EC ON EC.DW_EquipmentClass_Key = EQD.DW_EquipmentDetail_Key 
    INNER JOIN 
     DWH.DimSiteType ST ON FL.SiteTypeKey = ST.DW_SiteType_Key 
    INNER JOIN 
     (select Item 
     from DWH.fnSplit(@FLClassDescription,',')) AS DNO ON (FL.SiteTypeKey = DNO.Item OR @FLClassDescription ='-1') 
    INNER JOIN 
     (select Item from DWH.fnSplit(@MaintenanceLocation,',')) AS ML ON (FL.MaintenanceLocationKey = ML.Item OR @MaintenanceLocation ='-1') 
    INNER JOIN 
     (select Item from DWH.fnSplit(@EquipmentClass,',')) AS FLC ON (EC.DW_EquipmentClass_Key = FLC.Item OR @EquipmentClass ='-1') 
    INNER JOIN 
     (select Item from DWH.fnSplit(@AssetStatus,',')) AS EQC ON ((EQD.CharacteristicName = EQC.Item AND ISNULL(LTRIM(RTRIM(EQD.CHARCharacteristicValue)),'') <> '') OR @AssetStatus ='All') 
    WHERE 
     FL.FLClassDescription IS NOT NULL 
    GROUP BY 
     EC.ClassDescription, FL.Site, FL.SiteDesc, 
     FL.FunctionalLocation, FL.Parish, ST.SiteTypeName, 
     FL.MaintenanceLocationDesc 
END 

當我運行查詢時得到結果,但我需要將結果傳遞給我的參數,以便僅爲設備類獲取不同的值。執行語句如下:

Exec DWH.spAMBSiteAssetCountReport @MaintenanceLocation=N'366,367,332,362,3,360,331,365,361,364,357,396,2,406,371,4,368,369,370,333,394,358,359,395,355,353,354,335,363,356,397,352,349,348,351,350,347,372,373,374,377,375,376,382,386,383,387,384,389,381,391,378,385,379,380,388,390',@FLClassDescription=N'3,4,5,2,1',@EquipmentClass = AssetCount,@AssetStatus=N'All' 

當試圖使用資產計數作爲有價值的參數執行存儲過程時出現語法錯誤。任何幫助,將不勝感激。

謝謝。

+2

你的參數只是varchars,所以你的過程調用中的「AssetCount」是什麼? –

回答

0

首先,您聲明兩次@AssetStatus參數。

其次,您將count列別名爲'AssetCount',但不要在其他任何地方使用它,所以我不確定爲什麼您認爲這會導致語法錯誤。

最後,你問:

「我試圖通過將查詢的結果我的參數來獲取存儲過程的結果。」

這是什麼意思?你是否試圖從本身遞歸調用存儲過程?或者你是否試圖將另一個查詢的結果傳遞給此存儲過程?更多細節將有所幫助!如果您對評論做出迴應,我會盡力爲您提供幫助 - 無論您的問題是什麼,我都不認爲這會很難解決。

+0

@PhilipHerman沒問題 - 我看到你沒有爲前四個參數提供數據類型,並且你在第四個參數後錯過了一個逗號。這肯定會引發語法錯誤。 –

相關問題