SQL 中的无限循环?
有没有办法在SQL中实现无限循环?我正在思考一些就像在另一个中进行选择一样,递归地......(也许我在说愚蠢的事情)
There is some way to implement a infinite loop in SQL?? I was thinking on some like a select inside another one, recursively... (Maybe im talking foolishness)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您可以使用 CTE 执行递归无限循环:
默认情况下,SQL Server 将在 100 处停止;你可以让它永远循环:
You can do a recursive infinite loop with a CTE:
By default, SQL Server would stop at 100; you can make it loop forever with:
这应该在 oracle 中工作,
需要外部选择来避免客户端和数据库的某些组合发生的一些优化。
不完全确定
level >= 0
部分是否按预期工作,因为通常您只会将其与level <= 50
之类的内容一起使用来获得固定数量的行。This should work in oracle
the outer select is needed to avoid some optimizations which happen with some combinations of client and database.
Not completely sure if the
level >= 0
part works as intended, since normally you would use this only with something likelevel <= 50
to get a fixed number of rows.我无法想象您为什么要这样做,但这将取决于您的实现(SQL Server、Oracle、MySQL...)支持的循环结构。
例如,在 SQL Server 中,您可以像使用任何命令式语言一样编写无限循环。大致:
I can't imagine why you would want to do this, but it's going to depend on your implementation's (SQL Server, Oracle, MySQL...) supported loop constructs.
For example, in SQL Server, you would write an infinite loop the way you would in any imperative language. Roughly:
您无法使用 SQL 等声明性语言创建无限循环,因为编译器不允许您这样做。您将收到错误消息和/或它无法运行。
您需要像 JNK 建议的那样,使用 SQL 编写顺序的内容。
You can't create an infinite loop with a declaritive language like SQL because the compiler's won't let you. You will get error messages and/or it just won't run.
You need to do something like what JNK suggested, using SQL to write something sequential.