[英]Execute stored procedure in a view?
是否可以在視圖中執行存儲過程?
即
創建視圖[dbo]。[v_ReportInvoiceClientsThisMonth]作為EXEC [dbo]。[GetInvoiceClients] @startDate ='2009-03-01',@endDate ='2009-04-01'
(無效)
我需要這樣做的原因是,我需要一種在Excel中訪問SP的方法(以一種傻瓜安全的方式,即沒有VBA)。
您可以通過查看表值函數來執行此操作。 看起來像這樣:
-- === 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
有一些警告:
函數的功能有一些限制。 特別是在存儲過程代碼和函數代碼之間存在某種阻抗不匹配的情況,因此,您通常不能使用函數來包裝存儲過程來進行此類操作。
第一點意味着您可能必須將存儲過程重新轉換為表值函數。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.