简体   繁体   English

如何使用公用表表达式将SQL查询转换为一个(对于SQL Server 2000)

[英]How to Convert a SQL Query using Common Table Expressions to One Without (for SQL Server 2000)

I just found an adequate solution to How to Find Rows which are Duplicates by a Key but Not Duplicates in All Columns? 我刚刚找到了一个适当的解决方案, 如何找到所有列中按键重复但不重复的行? , coded the stored procedure, then learned that the database is stuck at SQL Server 2000. 编码存储过程,然后了解到数据库卡在SQL Server 2000上。

My solution, of course, relies heavily on Common Table Expressions. 当然,我的解决方案很大程度上依赖于公用表表达式。

Can anyone provide me a set of ru les for converting back to the SQL Server 2000 dialect? 任何人都可以为我提供一套转换回SQL Server 2000方言的文件吗?

Note that I have things like thisL: 请注意,我有像这样的东西:

;
WITH CTE1 AS ( ... ),
CTE2 AS (SELECT ... FROM CTE1 ... ),
CTE3 AS (SELECT ... FROM CTE1 INNER JOIN CTE2 ...)
SELECT * FROM CTE3
WHERE criteria
ORDER BY sequence

This would appear to make things more interesting... 这似乎会让事情变得更有趣......


Update: None of the CTEs are recursive. 更新:没有CTE是递归的。

Two options (granted, neither are pretty -- that's why we like CTE's) 两个选项(授予,两者都不漂亮 - 这就是我们喜欢CTE的原因)

OPTION 1 选项1

Create a temp table ( # , or if small enough @ ) and refer to it as you would the CTE. 创建临时表( # ,或者足够小@ )并像CTE一样引用它。 Drop when you are done. 完成后放弃。

OPTION 2 Put the entire CTE SELECT as a table in the FROM portion of the query. 选项2将整个CTE SELECT作为表放在查询的FROM部分中。

SELECT *
FROM  (SELECT *
       FROM table1) oldCTE

I don't think it is possible to come up with rules that would easily convert any cte into a non-cte statement. 我不认为有可能提出容易将任何 cte转换为非cte语句的规则。 as the possibilities are too open-ended (particularly if you're working with recursive CTEs). 因为可能性太开放(特别是如果你正在使用递归CTE)。 The closest I can think of would be to take each CTE in order, break it into it's own query, and use it to populate a temporary table that's used by the following queries. 我能想到的最接近的是将每个CTE按顺序排除,将其分解为自己的查询,并使用它来填充以下查询使用的临时表。 Hardly efficient, and not guarnateed to work in all possible situations. 几乎没有效率,也没有在所有可能的情况下工作。

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

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