How to do an IF/ELSE SQL check

Asked

Viewed 65 times

0

If anyone can help me, I really appreciate... I am beginner and I am not able to develop the following check:

  • If substring (tasks, 9, 1) = S, it should delete only in the Startup tables

  • If substring (tasks, 9, 1) = D, it should delete only in Development tables

        create table teste (
        projeto varchar(255),
        tarefas varchar (255),
        inicio varchar(255),
        nome varchar(255),
        horas nvarchar(255),
        atividade varchar(255)
        );

       insert into teste values ('Pxxxx - ','CC210 - D - ','02/03/2020 08:00',  'nome','9','atividade')
       insert into teste values ('Pxxxx - ','CC210 - D - ','03/03/2020 07:00',  'nome','5','atividade')
       insert into teste values ('Pxxxx - ','CC210 - D - ','03/03/2020 13:00','nome','4','atividade')
       insert into teste values ('Pxxxx - ','CC210 - D - ','04/03/2020 09:54','nome',   '2','atividade')
       insert into teste values ('Pxxxx - ','CC210 - D - ','04/03/2020 10:34','nome','2',NULL)


       Go

       create procedure sp_SA_INTEGRG7TRELLO

       AS
       Begin

       declare @projeto varchar(50),
         @nome varchar(50),
         @inicio varchar(50)


       declare cur_teste cursor
       for select substring (projeto, 1, 7), nome, inicio from teste;

       open cur_teste;

       fetch next from cur_teste
       into @projeto, @nome, @inicio

       while @@FETCH_STATUS = 0
       Begin

    Begin

        delete TB_ITEMSTARTUP from TB_ITEMSTARTUP as I
        inner join TB_VENDEDOR as V on V.ID_VEND = I.ID_VEND
        inner join TB_HORASTARTUP as H on H.ID_HS = I.ID_HS
        inner join TB_PEDIDO as P on P.ID_PED = H.ID_PED
        inner join TB_FUNCSTARTUP as F on F.ID_VEND = I.ID_VEND
        where P.NPROJ_PED = @projeto and V.NOM_VEND = @nome and F.DTINIC_FS = @inicio

        delete TB_HORASTARTUP from TB_HORASTARTUP as H
        inner join TB_VENDEDOR as V on V.ID_VEND = H.ID_VEND
        inner join TB_ITEMSTARTUP as I on I.ID_HS = H.ID_HS
        inner join TB_PEDIDO as P on P.ID_PED = H.ID_PED
        inner join TB_FUNCSTARTUP as F on F.ID_VEND = H.ID_VEND
        where P.NPROJ_PED = @projeto and V.NOM_VEND = @nome and F.DTINIC_FS = @inicio 


        delete TB_FUNCSTARTUP from TB_FUNCSTARTUP as F
        inner join TB_VENDEDOR as V on V.ID_VEND = F.ID_VEND
        inner join TB_PEDIDO as P on P.ID_PED = F.ID_PED 
        where P.NPROJ_PED = @projeto and V.NOM_VEND = @nome and F.DTINIC_FS = @inicio 

    End

    Begin

        delete TB_HORADESENV from TB_HORADESENV as H
        inner join TB_VENDEDOR as V on V.ID_VEND = H.ID_VEND
        inner join TB_PEDIDO as P on P.ID_PED = H.ID_PED 
        inner join TB_CENTROCUSTO as C on C.ID_USUGRAV = H.ID_USUGRAV
        inner join TB_FUNCSTARTUP as F on F.ID_USUGRAV = H.ID_USUGRAV
        where P.NPROJ_PED = @projeto and V.NOM_VEND = @nome and F.DTINIC_FS = @inicio

        delete TB_ATIVIDADE from TB_ATIVIDADE as A
        inner join TB_VENDEDOR as V on V.ID_VEND = ID_VEND
        inner join TB_PEDIDO as P on P.ID_PED = ID_PED 
        inner join TB_CENTROCUSTO as C on C.ID_USUGRAV = A.ID_USUGRAV
        inner join TB_FUNCSTARTUP as F on F.ID_USUGRAV = A.ID_USUGRAV
        where P.NPROJ_PED = @projeto and V.NOM_VEND = @nome and F.DTINIC_FS = @inicio

    End

       fetch next from cur_teste
       into @projeto, @nome, @inicio;
    End

       close cur_teste;
      deallocate cur_teste;

     End
  • You just need to put the IF conditional before the Begin of each step, and the Else before the other step, is already up to the Begin and End created

  • Yes, but I doubt how to build the syntax after writing IF because the condition is a substring (substring (tasks, 9, 1)) and none of the ways I tried is working...

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.