无效使用副作用运算符' EXECUTE STRING'在函数错误消息中

时间:2014-08-08 19:06:16

标签: sql-server function tsql

我在线搜索了此错误,并尝试使用动态SQL。我仍然得到'在一个函数中无效使用副作用操作符'EXECUTE STRING''如果有任何建议请告诉我。我感谢任何帮助。

  ALTER FUNCTION [dbo].[fn_get_type_ids]
    (
        @category_id  int = null, 
        @all_id          int = null,
        @category_id_op varchar(20) = null,
        @all_op  varchar(20) = null,
    )  
    RETURNS @TypeIDs table
    (
        type_id int
    )
    AS
    BEGIN


        IF (@category_id_op = 'Is')
        BEGIN
            SET @category1 = ('level_1_id = @category_id')
        END
        ELSE IF (@category_id_op = 'IS NOT')
        BEGIN
            SET @category1 = ('level_1_id <> @category_id OR level_1_id IS NULL') 
        END    

        IF (@all_op = 'Is')
        BEGIN
            SET @category2 = ('level_2_id = @all_id')
        END
        ELSE IF (@all_op = 'Is NOT')
        BEGIN
            SET @category2 = ('level_2_id <> @all_id OR level_1_id IS NULL') 
        END    


            DECLARE @sql nvarchar(4000)

           set @sql = 'insert into @TypeIDs 
                     SELECT atr.type_id
              FROM type_rel atr 
                      LEFT JOIN category_ref acr1 ON atr.level_1_id = acr1.category_id
                      WHERE (' + @category1 + ' AND ' +  @category2 + ')' 


    exec (@sql)

     RETURN 
    END

0 个答案:

没有答案