如何在SQL视图中使用IF-ELSE类似条件?

时间:2016-01-18 05:17:57

标签: sql sql-server-2008-r2 sql-view

我想在IF-ELSE视图中使用SQL类似条件,因为我们知道我们无法使用实际的IF-ELSE。 以下是我想如何使用它的示例:

IF EXISTS ( SELECT    1
          FROM   SomeTable )
SELECT  *
FROM    TableA
ELSE
SELECT  *
FROM    TableB

另外,我不想使用UNION All。实现它的最佳逻辑是什么?

3 个答案:

答案 0 :(得分:0)

希望它可以帮助您按照以下代码处理您的问题

Simple CASE expression: 
CASE input_expression 
     WHEN when_expression THEN result_expression [ ...n ] 
     [ ELSE else_result_expression ] 
END 

参数

input_expression
Is the expression evaluated when the simple CASE format is used. input_expression is any valid expression.
WHEN when_expression
Is a simple expression to which input_expression is compared when the simple CASE format is used. when_expression is any valid expression. The data types of input_expression and each when_expression must be the same or must be an implicit conversion.
THEN result_expression
Is the expression returned when input_expression equals when_expression evaluates to TRUE, or Boolean_expression evaluates to TRUE. result expression is any valid expression.
ELSE else_result_expression
Is the expression returned if no comparison operation evaluates to TRUE. If this argument is omitted and no comparison operation evaluates to TRUE, CASE returns NULL. else_result_expression is any valid expression. The data types of else_result_expression and any result_expression must be the same or must be an implicit conversion.
WHEN Boolean_expression
Is the Boolean expression evaluated when using the searched CASE format. Boolean_expression is any valid Boolean expression.

示例

USE AdventureWorks2012;
GO
SELECT   ProductNumber, Category =
      CASE ProductLine
         WHEN 'R' THEN 'Road'
         WHEN 'M' THEN 'Mountain'
         WHEN 'T' THEN 'Touring'
         WHEN 'S' THEN 'Other sale items'
         ELSE 'Not for sale'
      END,
   Name
FROM Production.Product
ORDER BY ProductNumber;
GO

if-else with 2 tables

declare  @a nvarchar(50)
if 1=1 
set @a='select * from table1'
else
set @a='select * from table2'
begin 
 execute sp_executesql   @a
end 

答案 1 :(得分:0)

对于选择查询,UNION ALL可能是我们对您的需求知之甚少的有效解决方案。 e.g。

document.addEventListener("keydown", onKeyDown, false);
function onKeyDown(e) {
    e = e || window.event;
    var keyValue=0;
    var set = new Set([65,80,81,82,85,45,46]);
    var cont=false;
    //var isIE11 = navigator.userAgent.test(/Trident.*rv[ :]*11\./);
    if(isIE11){
        alert("ie11");
    }else{
        alert("not ie");    
    }
    if(e.ctrlKey){
        keyValue=17;
        if(set.has(e.keyCode)){
            cont=true;
        }
    }else if(e.shiftKey){
        keyValue=16;
        if((e.keyCode>=113 && e.keyCode<=118)&& e.keyCode!=115){
            cont=true;
        }
    }else if(e.altKey){
        keyValue=18;
        if(e.keyCode==76){
            cont=true;
        }
    }else if(e.keyCode>112&& e.keyCode<=123){
        cont=true;
    }
    if(cont){
        e.preventDefault();
        e.stopPropagation();
        e.returnValue = false;
        //tempAlert("close",5000);
        zAu.send(new zk.Event(zk.Widget.$('#content'), 'onCtrlKey', {key:e.keyCode,ctrlKey:keyValue},{toServer:true}));
    }
    return false;
}

对于VIEW,如果子查询很复杂,则可以使用CTE。

DECLARE @judge AS int

SET @judge = (SELECT 1 FROM SomeTable)

SELECT  col1, col2, col3 ...
FROM    TableA
WHERE @judge = 1

UNION ALL
SELECT  col1, col2, col3 ...
FROM    TableB
WHERE @judge <> 1

答案 2 :(得分:0)

你说你不想使用CREATE TABLE ta(id INT IDENTITY(1,1) NOT NULL,name NVARCHAR(256)); CREATE TABLE tb(id INT IDENTITY(1,1) NOT NULL,name NVARCHAR(256)); CREATE TABLE tprobe(id INT NULL); GO INSERT INTO ta(name)VALUES(N'TT'),(N'Zara'),(N'Peter'); INSERT INTO tb(name)VALUES(N'LL'),(N'Mandy'),(N'Peter'); GO CREATE VIEW list_names AS SELECT*FROM ta WHERE EXISTS(SELECT 1 FROM tprobe) UNION ALL SELECT*FROM tb WHERE NOT EXISTS(SELECT 1 FROM tprobe); GO SELECT * FROM list_names; INSERT INTO tprobe(id)VALUES(NULL); SELECT * FROM list_names; DROP VIEW list_names; DROP TABLE tprobe; DROP TABLE tb; DROP TABLE ta; GO ,但这是你从视图中完成这项任务的唯一方法。

这是我要做的示例脚本:

 this.addTripForUser = function(uid, tripObj) {
    var trips = getTripsArray(uid); // get trips for uid
 // NOTE THE NEW RETURN ON THE LINE BELOW   
 return trips.$loaded() // when trips array is loaded
    .then(
      function(response) {
        return trips.$add(tripObj); // return promise of adding trip
      }
    );
  };