简体   繁体   English

Select 从 CTE 使用 AS? SQL 服务器 2008

[英]Select From CTE using AS? SQL Server 2008

I'm trying to convert a PostgreSQL into SQL Server.我正在尝试将 PostgreSQL 转换为 SQL 服务器。 But this query doesn't work.但是这个查询不起作用。

What am I doing wrong?我究竟做错了什么? I tried to add a semicolon before WITH but no luck.我试图在 WITH 之前添加一个分号,但没有运气。

   SELECT 
      member_a AS you, member_b AS mightknow, shared_connection,
      CASE
         WHEN (n1.member_job_country = n2.member_job_country AND n1.member_job_country = n3.member_job_country) THEN 'country in common'
         WHEN (n1.member_unvan_id = n2.member_unvan_id AND n1.member_unvan_id = n3.member_unvan_id) THEN 'unvan in common'
         ELSE 'nothing in common'
      END AS reason
   FROM (
      WITH transitive_closure(member_a, member_b, distance, path_string, direct_connection) AS
        (SELECT 
             member_a, member_b, 1 AS distance,
             CAST(member_a as varchar(MAX)) + '.' + CAST(member_b as varchar(MAX)) + '.' AS path_string,
             member_b AS direct_connection
         FROM Member_Contact_Edges
         WHERE member_a = 45046 -- set the starting node

         UNION ALL

         SELECT 
             tc.member_a, e.member_b, tc.distance + 1,
             CAST(tc.path_string as varchar(MAX)) + CAST(e.member_b as varchar(MAX)) + '.' AS path_string,
             tc.direct_connection
         FROM Member_Contact_Edges AS e
         JOIN transitive_closure AS tc ON e.member_a = tc.member_b
         WHERE tc.path_string NOT LIKE '%' + CAST(e.member_b as varchar(MAX)) + '.%'
         AND tc.distance < 2
        )

   SELECT
       member_a, member_b,direct_connection AS shared_connection
   FROM transitive_closure
   WHERE distance = 2
  ) AS youmightknow
  LEFT JOIN Members AS n1 ON youmightknow.member_a = n1.memberID
  LEFT JOIN Members AS n2 ON youmightknow.member_b = n2.memberID
  LEFT JOIN Members AS n3 ON youmightknow.shared_connection = n3.memberID
  WHERE (n1.member_job_country = n2.member_job_country 
         AND n1.member_job_country = n3.member_job_country)
        OR (n1.member_unvan_id = n2.member_unvan_id 
            AND n1.member_unvan_id = n3.member_unvan_id);

Error I get:我得到的错误:

Msg 156, Level 15, State 1, Line 11消息 156,第 15 级,State 1,第 11 行
Incorrect syntax near the keyword 'WITH'.关键字“WITH”附近的语法不正确。
Msg 319, Level 15, State 1, Line 11消息 319,第 15 级,State 1,第 11 行
Incorrect syntax near the keyword 'with'.关键字“with”附近的语法不正确。 If this statement is a common table expression, an xmlnamespaces clause or a change tracking context clause, the previous statement must be terminated with a semicolon.如果此语句是公用表表达式、xmlnamespaces 子句或更改跟踪上下文子句,则前面的语句必须以分号结束。
Msg 102, Level 15, State 1, Line 34消息 102,第 15 级,State 1,第 34 行
Incorrect syntax near ')'. ')' 附近的语法不正确。

Here is the reference;这是参考; Graphs in the Database - SQL Meets Social Networks - Look at the facebook suggestion part at the bottom of the article. 数据库中的图表 - SQL 符合社交网络- 查看文章底部的 facebook 建议部分。

Thanks in advance提前致谢

The CTE declaration needs to go at the top. CTE 声明需要 go 在顶部。 You can also have multiple CTEs declared and joined by commas rather than mixing CTEs and derived tables.您还可以使用逗号声明和连接多个 CTE,而不是混合 CTE 和派生表。

Try尝试

;WITH 
/*First CTE declaration*/
transitive_closure(member_a, member_b, distance, path_string, direct_connection) 
AS
(   
     ...
),
/*Second CTE declaration*/
youmightknow AS
(
SELECT member_a, member_b,direct_connection AS shared_connection
FROM transitive_closure
WHERE distance = 2
)        
SELECT member_a AS you,
...
FROM youmightknow

Move your CTE definition to the beginning of your SQL statement.将 CTE 定义移至 SQL 语句的开头。 The keyword WITH appears once at the beginning of your statement to introduce one or more CTEs, which may then be referred to in the following SQL.关键字WITH出现在语句的开头一次,用于引入一个或多个 CTE,然后可以在以下 SQL 中引用。 Take a look at this CTE example , it will clear it up for you.看看这个CTE 示例,它会为您清除它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM