如果sql server中的语句如何从子查询中获取值

时间:2014-06-20 11:55:23

标签: sql sql-server database

我尝试从不同的表中获取结果。 如果name是1111则SELECT * FROM TABLE 1 如果name是2222则SELECT * FROm TABLE 2

SELECT CASE 

WHEN type='111111'

THEN (SELECT * FROM db1)
ELSE (SELECT * FROM db2)
FROM db_name

问题在哪里?

2 个答案:

答案 0 :(得分:1)

尝试如下:

SELECT 
CASE 
  WHEN COLUMN1='XYZ' THEN (SELECT SINGLE_COL FROM DB1)
  ELSE (SELECT SINGLE_COL FROM DB1) 
END 
FROM TABLE_NAME

内部查询必须返回单个值。

答案 1 :(得分:0)

这将给你答案

Select db1.* from db1 join db_name on 1=1 where db_name = '1111111'
union all
Select db2.* from db2 join db_name on 1=1 where db_name <> '1111111'