在MSSQL中使用IF作为外部应用程序

时间:2017-03-31 08:10:34

标签: sql-server sql-server-2008 tsql

我想在外部申请中写下 IF 条件。我需要返回多个列,所以我不想写子查询

SELECT name FROM v_users 
OUTER APPLY
(
    IF CAST(reg_date AS DATE)<CAST('03/25/2017' AS DATE)
   SELECT gender,email,age,class FROM tb_register_old WHERE id=v_users.user_id;
ELSE 
   SELECT gender,email,age,class FROM tb_register_new WHERE id=v_users.user_id;
)s

1 个答案:

答案 0 :(得分:4)

也许这个?

SELECT name FROM v_users 
OUTER APPLY
(
 SELECT gender,email,age,class 
 FROM tb_register_old 
 WHERE id=v_users.user_id and CAST(reg_date AS DATE)<CAST('03/25/2017' AS DATE)

 UNION ALL  

 SELECT gender,email,age,class 
 FROM tb_register_new 
 WHERE id=v_users.user_id and CAST(reg_date AS DATE) >= CAST('03/25/2017' AS DATE)
)s