2012-05-13 31 views
1

我在VIEWSELECT子句中尋找調用REMOTE FUNCTION的方法。 基本上我'嘗試做財產以後這樣的:從視圖調用遠程函數 - SQL Server

CREATE VIEW [dbo].[View_Year_Forcast] AS 
     (SELECT BshForecast.ProjectId, 
        BshForecast.JobTypeId, 
        Job_Type.NAME,   
        ProjectName , 
        ProjectManagerID,      
        ProjectManagerName , 


***EXEC @START_DATE =[192.168.0.10].[LudanProjectManager].[dbo].[func_Project_Get_Start_Date] @PROJECT_ID as [Start_Date],*** 

        StartBudget , 
        AdditionalBudget , 
        TotalBudget , 
        UsedBudget , 
     FROM  BshForecast INNER JOIN Job_Type ON BshForecast.JobTypeId = ID     
     WHERE (dbo.BshForecast.Year = DATEPART(YYYY,GETDATE())) 
       AND (dbo.BshForecast.IsDeleted = 0) 
       AND (dbo.BshForecast.BranchId = 200) 
       AND (dbo.BshForecast.Approved = 1)); 

什麼我'試圖得到的是一種觀點認爲,seven'th列將保存每個項目的開始日期,這將是從評估在遠程服務器中運行。

回答

2

我知道調用遠程函數的唯一方法是openquery。但openquery只需要字符串文字,因此您必須將電話打包到exec中的openquery

下面是一個創建函數並通過名爲「localhost」的鏈接服務器調用它的示例。

use TestDatabase 
if exists (select * from sys.objects where name = 'fn_twice') 
    drop function fn_twice 
go 
create function dbo.fn_twice(@i int) returns int as begin return 2*@i end 
go 
declare @i int 
set @i = 21 

declare @func_sql nvarchar(max) 
set @func_sql = 'select @result = a.result from openquery(localhost, ' + 
     '''select TestDatabase.dbo.fn_twice(' + 
     cast(@i as varchar(12)) + ') as result'') a' 

declare @result int 
exec sp_executesql @func_sql, N'@result int output', @result output 

-- The result of the function call is now available in @result, and 
-- you can use it in a query. 
+1

如果我錯了,請糾正我,但它不必是'OPENQUERY'。你可以像這樣調用函數:'EXEC remoteserver.database..sp_executesql N'SELECT @result = dbo.FunctionName(arguments)',N'@int int output',@myresult output'。不過,也許我錯過了一些東西。 –

+0

@AndriyM:如果允許RPC和'sp_executesql'的執行權限,那麼你的解決方案更簡潔 – Andomar

+0

但是你的觀點非常好,特別是關於RPC的觀點。而且我忘記了我實際上打算爲您的建議投票,因爲我記得曾經不得不使用'OPENQUERY'就像那樣,當我需要調用遠程函數時。 –