2009-04-28 160 views
1

是否有可能在一個視圖來執行存儲過程?在視圖中執行存儲過程?

I.e.

CREATE VIEW [DBO]。[v_ReportInvoiceClientsThisMonth] AS EXEC [DBO]。[GetInvoiceClients] @startDate = '2009-03-01',@EndDate = '2009-04-01'

(不工作)

我之所以需要這個是我需要一種方法來訪問Excel中的SP(在一個白癡安全的方式,即不VBA)。

+0

查看同類問題:http://stackoverflow.com/questions/728898/can-you-do-a-select-on-the-results-of-a-stored-procedure-in-t- SQL – 2009-04-28 08:11:07

+0

http://stackoverflow.com/questions/916784/how-to-call-stored-procedure-in-a-view – 2012-09-20 16:11:16

回答

3

您可以在表值函數的視圖做到這一點。這看起來是這樣的:

-- === Table-Valued function ==================================== 
-- 
create function fn_foo (
     @Date datetime 

) returns @ResultSet table (
     DateKey   datetime 
     ,DisplayDate  varchar (20) 
) as 
    insert @ResultSet (
      DateKey 
      ,DisplayDate 
    ) 
    select DateKey  -- Just pretend there's something to select 
      ,DisplayDate -- behind the scenes 
     from ods.Dates 
    where DateKey <= @Date 
    return 
go 


-- === View ============================================ 
-- 
create view vw_foo (
     DateKey 
     ,DisplayDate 
) as 
select DateKey 
     ,DisplayDate 
    from fn_foo ('2009-04-31') 
go 

有幾個注意事項:

  • 有一些限制,你可以用函數做什麼。尤其是,存在存儲過程的代碼和功能代碼,所以你通常不能使用的功能圍繞一個存儲過程包做這樣的事情的目的之間的阻抗失配的東西。

  • 第一點意味着你可能要重新投你的存儲過程作爲一個表值函數。