编写三重查询时出现语法错误

时间:2014-03-27 16:13:07

标签: tsql sql-server-2008-r2

我正在编写以下查询:

select object_id from sys.columns  
where name in ('TCP_Port_Export','TCP_Port_Import')
order by 1 desc 

并将结果显示为:

的object_id

1322342229
1322342229个
1306136094个
1306136094个
284905178个
284905178

然后我将此查询混合为:

select name from sys.tables where object_id in (select object_id from sys.columns  
where name in ('TCP_Port_Export','TCP_Port_Import'))

我得到的结果如下: 名称

IB_Formats_BACKUP
IB_Formats
IB_Formats_Archive

但在我将此查询合并到表单后:

select * from (select name from sys.tables where object_id in (select object_id from sys.columns  
where name in ('TCP_Port_Export','TCP_Port_Import')))

我收到以下错误:

Msg 102,Level 15,State 1,Line 3 ')'附近的语法不正确。

有人可以帮忙吗?

1 个答案:

答案 0 :(得分:0)

您需要先对查询进行别名,然后才能从中SELECT进行操作。所以你需要把它改成:

select * from 
(select name from sys.tables 
 where object_id in 
 (select object_id from sys.columns  
  where name in ('TCP_Port_Export','TCP_Port_Import'))) as s

但是,这将返回与运行中间和最内层查询相同的结果,因此请说明您需要最高查询的原因。

相关问题