2014-03-27 22 views
0

我與SQL Server 2012的如何執行正在返回表的用戶定義函數sql?

我已經創造了SQL函數像這樣的工作:

CREATE FUNCTION [dbo].[Fn_IsCompanyExistInUserLocation](@CompanyId int) 

RETURNS @Result table 
(
UserId int null 
) 
AS 
BEGIN 
    insert into @Result(UserId) select UL.UserId FROM [RRT_Reporting].[dbo].[UserLocation] as UL where [email protected] and UL.LocationType='COMPANY' 


    RETURN 
END 

我不知道它的正確的方式與否。

現在我試圖執行這樣的功能:

DECLARE @ret as table (UserId int null) 

EXEC @ret = [dbo].[Fn_IsCompanyExistInUserLocation] @CompanyId= 10; 

select UserId from @ret 

和我收到的錯誤:

Msg 137, Level 16, State 1, Line 6 
Must declare the scalar variable "@ret". 
+2

你不需要告訴我們,例如你會標記出接受的答案。這就是SO *如何工作*。 –

+0

ooohhh really;) –

回答

2

試試這個:

DECLARE @ret table (UserId int null) 
insert into @ret select * from [dbo].[Fn_IsCompanyExistInUserLocation](10) 
select UserID from @ret 
+0

它給出了一個錯誤:Msg 156,Level 15,State 1,Line 2 關鍵字'into'附近的語法不正確。 –

+0

掛上。我會稍微編輯一下我的答案。 –

+0

確定已更新的答案。 –

0

我懂了:

SELECT * 
FROM [dbo].[Fn_IsCompanyExistInUserLocation](10) 
+0

那麼你現在是否還在使用'@ ret'變量? –

+0

老兄我試着測試Fn_IsCompanyExistInUserLocation函數。 –

+1

http://technet.microsoft.com/en-US/enus/library/aa214485(v=sql.80).aspx確實有幫助 –

相關問題