此名称在功能中不是有效的标识符

时间:2016-04-19 20:04:05

标签: sql sql-server sql-function dynamic-queries

我在函数中有一个带动态查询的变量,我需要确定返回另一个变量

EXEC @resultIf = @strSQLCount
                    IF @resultIf <> 0 BEGIN
                        SET @last_qtdChaves = @qtdChaves
                        SET @comissao_temp = @resultIf
                    END

然而是出现此错误:此名称不是函数中的有效标识符

我已经尝试了一切,我无法解决 记住这段代码片段在函数

- UPDATE -

ALTER FUNCTION [dbo].[FN_percentualComissao] 
(
  -- Add the parameters for the function here
  @cod_representante int,
  @cod_cliente int,
  @cod_produto varchar(max)
)
RETURNS float
AS
BEGIN
-- Declare the return variable here
DECLARE @temComissionamento bit,
        @comissao float,
        @strSQL varchar(max),
        @strSQLCount varchar(max)

set @temComissionamento = 1

IF (select ISNULL(comissao_rep,0) as comissao from representantes where cod_representante = @cod_representante) <= 0
    BEGIN
        set @temComissionamento = 0
    END

IF @temComissionamento = 1
    BEGIN
        DECLARE @micro_regiao varchar(max), 
                @cod_canal int, 
                @cod_pagamento int, 
                @cod_grupo_cliente int

        SELECT 
            @micro_regiao = micro_regiao,
            @cod_canal = cod_canal,
            @cod_pagamento = cod_pagamento,
            @cod_grupo_cliente = cod_grupo_cliente
        FROM
            clientes
        WHERE
            cod_cliente = @cod_cliente


    DECLARE @col1 bit,
            @col2 bit,
            @col3 bit,
            @col4 bit,
            @col5 bit,
            @col6 bit,
            @col7 bit,
            @qtdChaves int,
            @last_qtdChaves int,
            @comissao_temp float,
            @resultIf float

    DECLARE c CURSOR FOR
        SELECT col1,
               col2,
               col3,
               col4,
               col5,
               col6,
               col7
        FROM comissoes_repres_tbverdade 
        ORDER BY col1 DESC, col2 DESC, col3 DESC, col4 DESC, col5 DESC, col6 DESC, col7 DESC

    OPEN c
    FETCH NEXT FROM c INTO @col1, @col2, @col3, @col4, @col5, @col6, @col7

    SET @last_qtdChaves = 0
    While (@@FETCH_STATUS=0) BEGIN

        SET @strSQL = 'SELECT ISNULL(perc_comissao,0) as comissao FROM comissoes_repres WHERE 1 = 1 '
        SET @qtdChaves = 0

        IF @col1 = 1 
            BEGIN
                set @strSQL += ' AND (regiao = '''+@micro_regiao+''') '
                set @qtdChaves = @qtdChaves + 1
            END
        ELSE
            BEGIN
                set @strSQL += ' AND (regiao = ''?'') '
            END

        IF @col2 = 1 
            BEGIN
                set @strSQL += ' AND (repres = '''+CAST(@cod_representante as varchar)+''') '
                set @qtdChaves = @qtdChaves + 1
            END
        ELSE
            BEGIN
                set @strSQL += ' AND (repres = ''?'') '
            END

        IF @col3 = 1 
            BEGIN
                set @strSQL += ' AND (condicao_pag = '''+CAST(@cod_pagamento as varchar)+''') '
                set @qtdChaves = @qtdChaves + 1
            END
        ELSE
            BEGIN
                set @strSQL += ' AND (condicao_pag = ''?'') '
            END

        IF @col4 = 1 
            BEGIN
                set @strSQL += ' AND (canal_vendas = '''+CAST(@cod_canal AS varchar)+''') '
                set @qtdChaves = @qtdChaves + 1
            END
        ELSE
            BEGIN
                set @strSQL += ' AND (canal_vendas = ''?'') '
            END

        IF @col5 = 1 
            BEGIN
                set @strSQL += ' AND (grupo_cliente = '''+CAST(@cod_grupo_cliente AS varchar)+''') '
                set @qtdChaves = @qtdChaves + 1
            END
        ELSE
            BEGIN
                set @strSQL += ' AND (grupo_cliente = ''?'') '
            END

        IF @col6 = 1 
            BEGIN
                set @strSQL += ' AND (familia_comercial = '''+SUBSTRING(@cod_produto,3,2)+''') '
                set @qtdChaves = @qtdChaves + 1
            END
        ELSE
            BEGIN
                set @strSQL += ' AND (familia_comercial = ''?'') '
            END

        IF @col7 = 1 
            BEGIN
                set @strSQL += ' AND (item = '''+CAST(@cod_produto AS VARCHAR)+''') '
                set @qtdChaves = @qtdChaves + 1
            END
        ELSE
            BEGIN
                set @strSQL += ' AND (item = ''?'') '
            END
        SET @comissao_temp = 0
        SET @strSQLCount = N'SELECT COUNT(*) FROM ('+@strSQL+') as t'
        IF @last_qtdChaves <= @qtdChaves
            BEGIN
                EXEC @resultIf = @strSQLCount
                IF @resultIf <> 0 BEGIN
                    SET @last_qtdChaves = @qtdChaves
                    SET @comissao_temp = @resultIf
                END
            END
        IF @comissao_temp = 0 BEGIN
            SElECT @comissao_temp = ISNULL(comissao_rep,0)
            FROM representantes
            WHERE cod_representante = @cod_representante

            SET @comissao = 0

            IF @comissao_temp <> 0 BEGIN
                SET @comissao = @comissao_temp
            END
        END

        FETCH NEXT FROM c INTO @col1, @col2, @col3, @col4, @col5, @col6, @col7
    END
    CLOSE c
    DEALLOCATE c
END

return @comissao

END

0 个答案:

没有答案