Usuário com melhor resposta
Ajuda com Function

Pergunta
-
Galera, eu tenho uma function que me traz a porcentagem de uma certa consulta:
Create FUNCTION [dbo].[FNC_CALCULAR_PORCENTAGEM] (@tipo int, @Usuario varchar(20)) RETURNS VARCHAR(30) AS BEGIN declare @Result varchar(30) declare @n1 decimal declare @n2 decimal if @tipo=1 --% set @n1 = (select isnull(mes1,'0') valor from dados1 where tipo =4 and user=@Usuario) --impostos set @n2 = (select isnull(mes1,'0') valor from dados2 where tipo =3 and user=@Usuario) --receita total set @Result = (select round((@n1/ @n2 * 100),2) as PERCENTUAL) return @Result end GO
Funciona tranquilo.
Porém, tenho o seguinte o problema... o mes é separado por campo, ou seja, nesse caso mes1 é referente a Janeiro.
Se eu quisesse fevereiro, deveria ser mes2, março mes3... e assim em diante.Existe alguma forma de montar o campo de acordo com o parametro?
Exemplo:Create FUNCTION [dbo].[FNC_CALCULAR_PORCENTAGEM] (@tipo int, @Usuario varchar(20), @Mes varchar(2)) RETURNS VARCHAR(30) AS BEGIN declare @Result varchar(30) declare @n1 decimal declare @n2 decimal if @tipo=1 --% set @n1 = (select isnull(@mes,'0') valor from dados1 where tipo =4 and user=@Usuario) --impostos set @n2 = (select isnull(@mes,'0') valor from dados2 where tipo =3 and user=@Usuario) --receita total set @Result = (select round((@n1/ @n2 * 100),2) as PERCENTUAL) return @Result end GO
ou seja, o campo mes1, fosse trocado por mes2, mes3... de acordo com o parametro de entrada @mes, para trazer os valores corretos do mes (janeiro, fevereiro, março) no select?
- Editado Paulo.Sérgio quinta-feira, 4 de janeiro de 2018 15:05
Respostas
-
Existem duas formas de resolver este problema
1: usando o case
Create FUNCTION [dbo].[FNC_CALCULAR_PORCENTAGEM] (@tipo int, @Usuario varchar(20), @Mes varchar(2)) RETURNS VARCHAR(30) AS BEGIN declare @Result varchar(30) declare @n1 decimal declare @n2 decimal if @tipo=1 --% set @n1 = (select case @Mes when 'mes1' then isnull(mes1,'0') when 'mes2' then isnull(mes2,'0') when 'mes3' then isnull(mes3,'0') when 'mes4' then isnull(mes4,'0') when 'mes5' then isnull(mes5,'0') when 'mes6' then isnull(mes5,'0') when 'mes7' then isnull(mes6,'0') when 'mes8' then isnull(mes7,'0') when 'mes9' then isnull(mes8,'0') when 'mes10' then isnull(mes10,'0') when 'mes11' then isnull(mes11,'0') when 'mes12' then isnull(mes12,'0') end as valor from dados1 where tipo =4 and user=@Usuario) --impostos set @n2 = (select case @Mes when 'mes1' then isnull(mes1,'0') when 'mes2' then isnull(mes2,'0') when 'mes3' then isnull(mes3,'0') when 'mes4' then isnull(mes4,'0') when 'mes5' then isnull(mes5,'0') when 'mes6' then isnull(mes5,'0') when 'mes7' then isnull(mes6,'0') when 'mes8' then isnull(mes7,'0') when 'mes9' then isnull(mes8,'0') when 'mes10' then isnull(mes10,'0') when 'mes11' then isnull(mes11,'0') when 'mes12' then isnull(mes12,'0') end as valor from dados2 where tipo =3 and user=@Usuario) --receita total set @Result = (select round((@n1/ @n2 * 100),2) as PERCENTUAL) return @Result end
2: Usando o Execute para execuçao de querys dinamicas
Create FUNCTION [dbo].[FNC_CALCULAR_PORCENTAGEM] (@tipo int, @Usuario varchar(20), @Mes varchar(2)) RETURNS VARCHAR(30) AS BEGIN declare @SQLQuery varchar(4000) declare @Result varchar(30) declare @n1 decimal declare @n2 decimal if @tipo=1 --% set @SQLQuery ='(select isnull('+ @mes +',''0'') valor from dados1 where tipo =4 and user=' + @Usuario +')' set @n1 = Execute @SQLQuery --impostos set @SQLQuery ='(select isnull('+ @mes +',''0'') valor from dados2 where tipo =3 and user=' + @Usuario +')' set @n2 = Execute @SQLQuery --receita total set @Result = (select round((@n1/ @n2 * 100),2) as PERCENTUAL) return @Result end
Pessoalmente eu nao gosto de querys dinamicas pelo fato de serem dificeis de debugar e serem mais lentas. Mas tem casos que nao tem como escapar.
Att
William John Adam Trindade
Analyste-programmeur
Sogi Informatique ltée
If you found this post helpful, please "Vote as Helpful". If it actually answered your question, remember to "Mark as Answer". Se achou este post útil, por favor clique em "Votar como útil". Se por acaso respondeu sua dúvida, lembre de "Marcar como Resposta".- Marcado como Resposta Paulo.Sérgio quinta-feira, 4 de janeiro de 2018 18:48
Todas as Respostas
-
Existem duas formas de resolver este problema
1: usando o case
Create FUNCTION [dbo].[FNC_CALCULAR_PORCENTAGEM] (@tipo int, @Usuario varchar(20), @Mes varchar(2)) RETURNS VARCHAR(30) AS BEGIN declare @Result varchar(30) declare @n1 decimal declare @n2 decimal if @tipo=1 --% set @n1 = (select case @Mes when 'mes1' then isnull(mes1,'0') when 'mes2' then isnull(mes2,'0') when 'mes3' then isnull(mes3,'0') when 'mes4' then isnull(mes4,'0') when 'mes5' then isnull(mes5,'0') when 'mes6' then isnull(mes5,'0') when 'mes7' then isnull(mes6,'0') when 'mes8' then isnull(mes7,'0') when 'mes9' then isnull(mes8,'0') when 'mes10' then isnull(mes10,'0') when 'mes11' then isnull(mes11,'0') when 'mes12' then isnull(mes12,'0') end as valor from dados1 where tipo =4 and user=@Usuario) --impostos set @n2 = (select case @Mes when 'mes1' then isnull(mes1,'0') when 'mes2' then isnull(mes2,'0') when 'mes3' then isnull(mes3,'0') when 'mes4' then isnull(mes4,'0') when 'mes5' then isnull(mes5,'0') when 'mes6' then isnull(mes5,'0') when 'mes7' then isnull(mes6,'0') when 'mes8' then isnull(mes7,'0') when 'mes9' then isnull(mes8,'0') when 'mes10' then isnull(mes10,'0') when 'mes11' then isnull(mes11,'0') when 'mes12' then isnull(mes12,'0') end as valor from dados2 where tipo =3 and user=@Usuario) --receita total set @Result = (select round((@n1/ @n2 * 100),2) as PERCENTUAL) return @Result end
2: Usando o Execute para execuçao de querys dinamicas
Create FUNCTION [dbo].[FNC_CALCULAR_PORCENTAGEM] (@tipo int, @Usuario varchar(20), @Mes varchar(2)) RETURNS VARCHAR(30) AS BEGIN declare @SQLQuery varchar(4000) declare @Result varchar(30) declare @n1 decimal declare @n2 decimal if @tipo=1 --% set @SQLQuery ='(select isnull('+ @mes +',''0'') valor from dados1 where tipo =4 and user=' + @Usuario +')' set @n1 = Execute @SQLQuery --impostos set @SQLQuery ='(select isnull('+ @mes +',''0'') valor from dados2 where tipo =3 and user=' + @Usuario +')' set @n2 = Execute @SQLQuery --receita total set @Result = (select round((@n1/ @n2 * 100),2) as PERCENTUAL) return @Result end
Pessoalmente eu nao gosto de querys dinamicas pelo fato de serem dificeis de debugar e serem mais lentas. Mas tem casos que nao tem como escapar.
Att
William John Adam Trindade
Analyste-programmeur
Sogi Informatique ltée
If you found this post helpful, please "Vote as Helpful". If it actually answered your question, remember to "Mark as Answer". Se achou este post útil, por favor clique em "Votar como útil". Se por acaso respondeu sua dúvida, lembre de "Marcar como Resposta".- Marcado como Resposta Paulo.Sérgio quinta-feira, 4 de janeiro de 2018 18:48
-