2012-05-10 71 views
0

不能執行此功能,我得到的錯誤:canot執行此T-SQL函數

Must declare the table variable "@Handle".

函數定義:

CREATE FUNCTION [dbo].[FnGetHandle]() 
RETURNS INT 
AS 
    BEGIN 
     DECLARE @Handle AS INT; 
     DECLARE @strUser AS NCHAR(256); 

     SET @Handle = 0; 
     SET @strUser = CURRENT_USER; 

     INSERT INTO tbl_handle 
     Output  Handle 
     INTO @Handle (UserName) 
     VALUES  (@strUser); 

     RETURN @Handle 
    END 

表定義:

tbl_handle definition (Handle int identity, username nvarchar, created date)

回答

3

output命令輸出到表或表變量。您可以創建一個保存輸出值的表變量。

declare @T table(handle int) 

insert into ... 
select ... 
output inserted.Handle into @T 

select @Handle = handle 
from @T 

return @Handle