为什么在 T-SQL 中做一个循环如此困难

2021-09-10 00:00:00 tsql sql-server

好的,我知道它可以做到,我经常这样做,但是为什么在 T-SQL 中做一个循环如此困难?我可以想到很多原因,我想通过查询结果集进行解析,并做一些没有循环就无法完成的事情,但设置和执行我的循环的代码超过 20 行.

OK, I know it can be done, I do it quite often, but why so difficult to do a loop in T-SQL? I can think of a ton of reasons I'd want to parse thru a query result set and do something that simply can't be done without a loop, yet the code to setup and execute my loop is > 20 lines.

我相信其他人也有类似的看法,那么为什么我们仍然没有一种简单的方法来执行循环?

I'm sure others have a similar opinions so why are we still without a simple way to perform a loop?

顺便说一句:我们终于在 SQL2008 中获得了 UPSERT(又名 MERGE),所以也许没有失去希望.

An aside: we finally got an UPSERT (aka MERGE) in SQL2008 so maybe all hope isn't lost.

推荐答案

SQL 是基于集合的,声明性语言;不是程序或命令式语言.T-SQL 试图跨越这两者,但它仍然建立在一个基本的基于集合的范式上.

SQL is a set-based, declarative language; not a procedural or imperative language. T-SQL tries to straddle the two, but it's still built on a fundamentally set-based paradigm.

相关文章