sql - This name is not a valid identifier in function -


i have variable dynamic query within function , need assing return variable

exec @resultif = @strsqlcount                     if @resultif <> 0 begin                         set @last_qtdchaves = @qtdchaves                         set @comissao_temp = @resultif                     end 

however giving error: this name not valid identifier in function

i've tried , can not solve remembering code snippet inside function

--update--

alter function [dbo].[fn_percentualcomissao]  (   -- add parameters function here   @cod_representante int,   @cod_cliente int,   @cod_produto varchar(max) ) returns float begin -- declare return variable here declare @temcomissionamento bit,         @comissao float,         @strsql varchar(max),         @strsqlcount varchar(max)  set @temcomissionamento = 1  if (select isnull(comissao_rep,0) comissao representantes 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                     clientes                     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         select col1,                col2,                col3,                col4,                col5,                col6,                col7         comissoes_repres_tbverdade          order col1 desc, col2 desc, col3 desc, col4 desc, col5 desc, col6 desc, col7 desc      open c     fetch next c @col1, @col2, @col3, @col4, @col5, @col6, @col7      set @last_qtdchaves = 0     while (@@fetch_status=0) begin          set @strsql = 'select isnull(perc_comissao,0) comissao comissoes_repres 1 = 1 '         set @qtdchaves = 0          if @col1 = 1              begin                 set @strsql += ' , (regiao = '''+@micro_regiao+''') '                 set @qtdchaves = @qtdchaves + 1             end         else             begin                 set @strsql += ' , (regiao = ''?'') '             end          if @col2 = 1              begin                 set @strsql += ' , (repres = '''+cast(@cod_representante varchar)+''') '                 set @qtdchaves = @qtdchaves + 1             end         else             begin                 set @strsql += ' , (repres = ''?'') '             end          if @col3 = 1              begin                 set @strsql += ' , (condicao_pag = '''+cast(@cod_pagamento varchar)+''') '                 set @qtdchaves = @qtdchaves + 1             end         else             begin                 set @strsql += ' , (condicao_pag = ''?'') '             end          if @col4 = 1              begin                 set @strsql += ' , (canal_vendas = '''+cast(@cod_canal varchar)+''') '                 set @qtdchaves = @qtdchaves + 1             end         else             begin                 set @strsql += ' , (canal_vendas = ''?'') '             end          if @col5 = 1              begin                 set @strsql += ' , (grupo_cliente = '''+cast(@cod_grupo_cliente varchar)+''') '                 set @qtdchaves = @qtdchaves + 1             end         else             begin                 set @strsql += ' , (grupo_cliente = ''?'') '             end          if @col6 = 1              begin                 set @strsql += ' , (familia_comercial = '''+substring(@cod_produto,3,2)+''') '                 set @qtdchaves = @qtdchaves + 1             end         else             begin                 set @strsql += ' , (familia_comercial = ''?'') '             end          if @col7 = 1              begin                 set @strsql += ' , (item = '''+cast(@cod_produto varchar)+''') '                 set @qtdchaves = @qtdchaves + 1             end         else             begin                 set @strsql += ' , (item = ''?'') '             end         set @comissao_temp = 0         set @strsqlcount = n'select count(*) ('+@strsql+') 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)             representantes             cod_representante = @cod_representante              set @comissao = 0              if @comissao_temp <> 0 begin                 set @comissao = @comissao_temp             end         end          fetch next c @col1, @col2, @col3, @col4, @col5, @col6, @col7     end     close c     deallocate c end  return @comissao 

end


Comments

Popular posts from this blog

java - nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet Hibernate+SpringMVC -

sql - Postgresql tables exists, but getting "relation does not exist" when querying -

asp.net mvc - breakpoint on javascript in CSHTML? -