2012-04-24 68 views
3

我正在創建一個存儲過程,它獲取一些參數,然後這些參數被髮送到另一個存儲過程,我從openrowset調用,但我得到一些語法錯誤。如何使用openrowset執行存儲過程的參數

CREATE PROCEDURE UpdatePrevFYConfigData 
    -- Add the parameters for the stored procedure here 

     @startDate datetime, 
     @endDate datetime, 
     @productGroup varchar(8000) = 'All', 
     @projectType varchar(500) = 'All', 
     @businessUnit nvarchar(50) = 'All', 
     @developmentLocation nvarchar(100) = 'All' 
AS 
BEGIN 
    -- SET NOCOUNT ON added to prevent extra result sets from 
    -- interfering with SELECT statements. 
    SET NOCOUNT ON; 

declare @start varchar(50) 
declare @end varchar(50) 

set @start = cast(@startDate as varchar(40)) 
set @end = cast(@endDate as varchar(40)) 

    -- Insert statements for procedure here 
select round(avg(a.DeviationDeadline),2) as DeviationDeadline, 
     round(avg(a.DeviationDefinition),2) as DeviationDefinition, 
     round(avg(a.DeviationRDCosts),2) as DeviationRDCosts, 
     round(avg(a.FunctionsAdded) + avg(a.FunctionsDeleted),2) as NotRealizedFuncs, 
     round(avg(a.DeviationPM2000Aufwand),2) as DeviationPM200Aufwand, 
     round(avg(b.Defect),2) as Defect 
     into #tempTable 
from openrowset('SQLNCLI', 
       'Server=.\sqlexpress;Trusted_Connection=yes;', 
       'SET NOCOUNT ON;SET FMTONLY OFF;EXEC [BSC_DB].dbo.SelectScorecardGraphData 
        '''[email protected]+''', 
        '''[email protected]+''', 
        '''[email protected]+''', 
        '''[email protected]+''', 
        ''1'', 
        ''0'', 
        ''All'', 
        ''Current'', 
        '''[email protected]+''', 
        '''[email protected]+''' 
       ') as a, 

    openrowset('SQLNCLI', 'Server=.\sqlexpress;Trusted_Connection=yes;', 'SET NOCOUNT ON;SET FMTONLY OFF;EXEC [BSC_DB].dbo.GetSPCDefectDistributionData 
'''+cast(@startDate as varchar(40))+''', 
'''+cast(@endDate as varchar(40))+''', 
''Defect'', 
'''[email protected]+''', 
'''[email protected]+''', 
'''[email protected]+''', 
'''[email protected]+'''') as b 


update dbo.EA_ProcessScorecard_Config_Tbl 
set EPC_Deviation = case EPC_Metric 
    when 'PM200' then (select DeviationDefinition from #tempTable) 
    when 'PM300' then (select DeviationDeadline from #tempTable) 
    when 'Cost' then (select DeviationRDCosts from #tempTable) 
    when 'PM150' then (select DeviationPM200Aufwand from #tempTable) 
    when 'Defect' then (select Defect from #tempTable) 
    when 'Funcs' then (select NotRealizedFuncs from #tempTable) 
END 
where EPC_Description = 'PrevFY' and EPC_FYYear = '0' 

drop table #tempTable 

END 

GO 

我不能來創建它,我得到的錯誤信息:

Msg 102, Level 15, State 1, Procedure UpdatePrevFYConfigData, 
Line 38 Incorrect syntax near '+'. 

...但如果我使用硬編碼值,它的工作參數!

請幫忙!

回答

7

OPENROWSET和OPENDATASOURCE都只能用於訪問外部數據,比如快速和骯髒的解決方案,或者不能配置永久鏈接服務器。這些功能不提供鏈接服務器提供的所有功能。 OPENROWSET和OPENDATASOURCE的參數不支持變量。他們必須被指定爲字符串文字。如果需要將變量作爲參數傳遞給這些函數,則可以動態構造包含這些變量的查詢字符串,並使用EXEC語句執行。 到(不語法檢查)類似

DECLARE @sqlCommand varchar(1000) 
SET @sqlCommand = 'SELECT * 
FROM OPENROWSET(''SQLNCLI'',''server=.\sqlexpress;Trusted_Connection=yes'',''SET NOCOUNT ON;SET FMTONLY OFF;EXEC [BSC_DB].dbo.SelectScorecardGraphData ''''' + cast(@param1 as varchar(10)) + ''''',''' + cast(@param2 as varchar(n)) ''')' 
EXEC @sqlCommand 

等等...... 希望有所幫助。親切的問候, 斯特凡

+0

沒有用的,如果使用OPENROWSET調用存儲過程的要點是把結果放到一個SELECT語句。我想這簡直是無法完成的,而被調用的存儲過程需要更改爲一個函數。這是我最終解決的解決方案。 – Neo 2013-07-11 16:30:57

1
-- FOR USING OPENROWSETS 
    EXEC sp_configure 'Ad Hoc Distributed Queries' 
     ,1 

    RECONFIGURE 

    DECLARE @SQL NVARCHAR(MAX) 

    SET @SQL = 'INSERT INTO #TABLESIZESYEAR SELECT NULL AS [TABLE NAME], * FROM OPENROWSET 
        (''SQLOLEDB'',''Server=(local);TRUSTED_CONNECTION=YES;'',''set fmtonly off EXEC one.[dbo].[InvestigateDataGrowthByYearAndClient] @pDATECOLUMN =' + @YEARCOLUMN + ' , 
         @pTABLENAME = ' + @TABLENAME + ' WITH RESULT SETS(
         ([YEAR NAME] NVARCHAR(5) NULL 
           , [NUMBER OF ROWS] CHAR(11)  
           , [RESERVED SPACE] VARCHAR(18) 
           , [DATA SPACE] VARCHAR(18)  
           , [INDEX SIZE] VARCHAR(18)  
           , [UNUSED SPACE] VARCHAR(18)) 
        ) 
         ;'') ' 

    DECLARE @ParmDefinition NVARCHAR(500) = '@pDATECOLUMN NVARCHAR(20) 
          ,@YEARCOLUMN NVARCHAR(20) 
          ,@pTABLENAME NVARCHAR(60)'; 

    EXECUTE sp_executesql @sql 
     ,@ParmDefinition 
     ,@YEARCOLUMN = @YEARCOLUMN 
     ,@pDATECOLUMN = @YEARCOLUMN 
     ,@pTABLENAME = @TABLENAME 
相關問題