是否可以在SELECT语句中包含WITH子句?

时间:2015-03-13 14:03:32

标签: sql-server-2008

我的工作中有一个基于云的GUI,我可以从不同的表中选择一些字段,以便之后可以打印出值。我不可能在SELECT语句之前编写WITH子句,因为程序会先自动写SELECT。所以我想知道如果可能的话。

2 个答案:

答案 0 :(得分:0)

是的,根据MSDN文档(https://msdn.microsoft.com/en-us/library/ms189499.aspx):

<SELECT statement> ::=  
    [WITH <common_table_expression> [,...n]]
    <query_expression> 
    [ ORDER BY { order_by_expression | column_position [ ASC | DESC ] } 
  [ ,...n ] ] 
    [ <FOR Clause>] 
    [ OPTION ( <query_hint> [ ,...n ] ) ] 
<query_expression> ::= 
    { <query_specification> | ( <query_expression> ) } 
    [  { UNION [ ALL ] | EXCEPT | INTERSECT }
        <query_specification> | ( <query_expression> ) [...n ] ] 
<query_specification> ::= 
SELECT [ ALL | DISTINCT ] 
    [TOP ( expression ) [PERCENT] [ WITH TIES ] ] 
    < select_list > 
    [ INTO new_table ] 
    [ FROM { <table_source> } [ ,...n ] ] 
    [ WHERE <search_condition> ] 
    [ <GROUP BY> ] 
    [ HAVING < search_condition > ] 

答案 1 :(得分:0)

如果你找不到任何其他方法,可以说服这个GUI接受

SELECT * FROM (
  -- write your entire select statement here
);