Join two IFs or more

1

I need to join some if 's and I'm having trouble,

I have already tried using union , but to no avail.

The query I'm running is:

IF (select count(Estado)

  FROM tbl_1
    where Estado != 'WIP' AND Estado !='on hold') > 0

    BEGIN
    SELECT 'Estado NOK: ', Nome, (Estado), SLA from tbl_1 
    where (Estado != 'WIP' AND Estado !='on hold')

    END

ELSE
    BEGIN
    SELECT 'Estado OK'

    END 


----------


//e o outro if seria:


----------


IF (select count(SLA) 

    FROM tbl_1
        where SLA != '24x7' AND SLA != '10x8') >0

    BEGIN
    SELECT 'Dados NOK: ', Nome, Estado, (SLA) from tbl_1 
    where (SLA != '24x7' AND SLA != '10x8')

    END

ELSE
    BEGIN
    SELECT 'Dados OK' 

    END 

What I need is for these two queries to be united into one.

    
asked by anonymous 20.03.2018 / 16:40

1 answer

0

You can do this using a dynamic sql:

DECLARE @SQL VARCHAR(MAX)
SELECT @SQL = ''

IF (/*CONDICAO 1*/)
BEGIN
    SELECT @SQL = @SQL + 'PESQUISA TRUE '
END
ELSE
    SELECT @SQL = @SQL + 'PESQUISA FALSE '

SELECT @SQL = @SQL + 'UNION '

IF (/*CONDICAO 2*/)
BEGIN
    SELECT @SQL = @SQL + 'PESQUISA TRUE '
END
ELSE
    SELECT @SQL = @SQL + 'PESQUISA FALSE '

EXEC(@SQL)

But to use union the two searches, when true or false, should return the same number of columns.

    
20.03.2018 / 20:42