use if on sql server

4

I have 3 tables in my database, CUSTOMERS, DISTRIBUTORS and EXPORTS and I have a production order table where I get the type of customer (Distributor, customer or export) and the ID of each one. the doubt is: how can I put a if in select , so I can join the 3 tables with the order of production? type like this:

if (tipocliente == CLIENTE)
    select CamposDaTabela from OrdemDeProduçao inner join Cliente
if (tipocliente == Distribuidor)
    select select CamposDaTabela from OrdemDeProduçao inner join Distribuidor
if (tipocliente == exportaçao)
    select CamposDaTabela from OrdemDeProduçao inner join Exportaçao

Would you like to do so within the same sql?

    
asked by anonymous 10.06.2016 / 19:21

5 answers

3

I do not know if I understood the question very well, but come on.

You could make 3 SELECTS and use UNION to return everything at once, like this:

SELECT CamposDaTabela, CampoCliente 
  FROM OrdemDeProduçao 
 INNER JOIN Cliente ON ...
 WHERE tipocliente = cliente

 UNION 

SELECT CamposDaTabela, CampoDistribuidor 
  FROM OrdemDeProduçao 
 INNER JOIN Distribuidor ON ...
 WHERE tipocliente = distribuidor

UNION 

SELECT CamposDaTabela, CampoExportacao 
  FROM OrdemDeProduçao  
 INNER JOIN Exportaçao ON ...
 WHERE tipocliente = exportacao

UNION

SELECT CamposDaTabela, NULL AS CampoNulo // ou substitua por 0, ou string vazia, dependendo do tipo de dados nas outras selects...
  FROM OrdemDeProduçao  
 WHERE tipocliente = NULL
    
10.06.2016 / 19:29
3

In a summarized way :

You can use the CASE function:

SELECT CAST(
             CASE 
                  WHEN Obsolete = 'N' or InStock = 'Y' 
                     THEN 1 
                  ELSE 0 
             END AS bit) as Saleable, * 
FROM Product

Here has several examples and explanations.

    
10.06.2016 / 19:27
3

ELSE (IF ... ELSE) (Transact-SQL)

Imposes conditions when executing a Transact-SQL statement. The Transact-SQL (sql_statement) statement following Boolean_expression will be executed if Boolean_expression is evaluated as TRUE . The optional keyword ELSE is an alternate Transact-SQL statement that is executed when Boolean_expression evaluates to FALSE or NULL .

Arguments

  

Boolean_expression

Is an expression that returns TRUE or FALSE. If the Boolean_expression contains a SELECT statement, it should be enclosed in parentheses.

  

{sql_statement | statement_block}

Is any valid Transact-SQL statement or statement grouping, as defined with an instruction block. To define an instruction block (batch), use the BEGIN and END keywords of the flow control language. Although all Transact-SQL statements are valid in a BEGIN ... END block, certain Transact-SQL statements should not be grouped in the same batch (statement block).

SET @Number = 50;
IF @Number > 100
   PRINT 'The number is large.';
ELSE 
   BEGIN
      IF @Number < 10
      PRINT 'The number is small.';
   ELSE
      PRINT 'The number is medium.';
   END ;
    
10.06.2016 / 19:42
1

You can do the way you're doing, the only difference is that in sql you only use a = to compare.

declare @tipocliente int = 3, @CLIENTE int = 1, @Distribuidor int = 2, @exportaçao int = 3

if (@tipocliente = @CLIENTE)
print 'select CamposDaTabela from OrdemDeProduçao inner join Cliente'
if (@tipocliente = @Distribuidor)
print ' select select CamposDaTabela from OrdemDeProduçao inner join Distribuidor'
if (@tipocliente = @exportaçao)
print 'select CamposDaTabela from OrdemDeProduçao inner join Exportaçao'
    
10.06.2016 / 19:41
0

So folks ...

from the 2012 of SQL Server, it became possible to use the" in line if "

IIF (expression, true_value, value_if_false)

Obviously the solutions already presented would give better support / warranty to the programmer because they would work even on older versions of SQL Server.

    
25.04.2018 / 04:17