2

我的功能看起来是这样的:为什么我不能在T-SQL函数的表变量上运行INSERT EXEC?

CREATE FUNCTION fn_FileSys_DirExists(@dirName AS nvarchar(260)) 
RETURNS bit 
AS 
BEGIN 
    DECLARE @dirExists int 
    DECLARE @fileResults TABLE 
    (
     file_exists int, 
     file_is_a_directory int, 
     parent_directory_exists int 
    ) 

    INSERT @fileResults (file_exists, file_is_a_directory, parent_directory_exists) 
    EXEC master.dbo.xp_fileexist @dirName 

    SELECT @dirExists = file_is_a_directory FROM @fileResults 
    RETURN @dirExists 
END 

当我尝试并执行上面的SQL,我得到以下错误:

Invalid use of a side-effecting operator 'INSERT EXEC' within a function.

我想在一个表变量操作的功能不考虑副作用?

+2

我不确定你可以在函数内执行存储过程。如果我是正确的,那不是插入,它本身就是'exec'。 –

+0

@ZoharPeled是的,我怀疑你可能是对的。 – ProfK

+0

好吧,事实证明我可能是正确的,[因为这个SO帖子建议](http://stackoverflow.com/questions/6344880/execute-stored-procedure-from-a-function) –

回答

相关问题