Ir para conteúdo

POWERED BY:

Arquivado

Este tópico foi arquivado e está fechado para novas respostas.

Fabiano Fernandes

Case na Function/Procedure

Recommended Posts

Dae galera,

 

 

Estou precisando usar uma estrutura de escolhas dentro da function, a lógica seria a seguinte:

 

case @tipo

when 1 then

delete from caixas where cd_conta=10;

when 2 then

delete from bancos where cd_banco=10;

when 3 then

delete from clientes where cd_cliente=10;

end

 

 

 

Mas no sql server pelo que andei pesquisando a sintaxe do case é da seguinte forma:

 

set @resultado = case @tipo

when 1 then

10

when 2 then

20

when 3 then

30

end;

 

 

Estou precisando de uma estrutura mais direta, percebi tambem que no sql server o elseif não existe, com isso tenho que fazer um monstro de código desse tamanho:

 

if @tipo = 1

delete from caixas _txt where i_empresa=@empresa and semestre=@semestre;

else

begin

if @tipo = 2

delete from professores_txt where i_empresa=@empresa and semestre=@semestre;

else

begin

if @tipo = 3

delete from disciplinas_txt where i_empresa=@empresa and semestre=@semestre;

else

begin

if @tipo = 4

delete from alunos_txt where i_empresa=@empresa and semestre=@semestre;

else

begin

if @tipo = 5

delete from classes_txt where i_empresa=@empresa and semestre=@semestre;

end

end

end

end

 

 

 

Precisava de alguma coisa assim:

 

case @tipo

when 1 then

delete from cursos_txt where i_empresa=@empresa and semestre=@semestre;

when 2 then

delete from professores_txt where i_empresa=@empresa and semestre=@semestre;

when 3 then

delete from disciplinas_txt where i_empresa=@empresa and semestre=@semestre;

when 4 then

delete from alunos_txt where i_empresa=@empresa and semestre=@semestre;

else

delete from classes_txt where i_empresa=@empresa and semestre=@semestre;

end;

 

Ou pelo menos próximo disso, o código ficará mais enxuto.

 

 

Obrigado pelo atenção.

Compartilhar este post


Link para o post
Compartilhar em outros sites

If Condicao
   Begin

   End
Else If Condicao
   Begin 
   
   End
Else If
   Begin
   
   End
Else
   Begin

   End

Existe Sim!

Compartilhar este post


Link para o post
Compartilhar em outros sites

If Condicao
   Begin

   End
Else If Condicao
   Begin 
   
   End
Else If
   Begin
   
   End
Else
   Begin

   End

Existe Sim!

 

 

Obrigado Dee

 

Essa sintaxe irá diminuir um pouco o código, mas ainda continuo sem uma opção descente de uso para um CASE / SWITCH.

 

flw

Compartilhar este post


Link para o post
Compartilhar em outros sites

×

Informação importante

Ao usar o fórum, você concorda com nossos Termos e condições.