本文介绍了将PLSQL Cursor FOR循环语法转换为TSQL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些通过一些逻辑循环的PLSQL代码:

I have some PLSQL code which loops through some logic:

 FOR I in cur1
 LOOP

    SELECT value1, value2
    FROM db1..table1 t1

 END LOOP;

有人可以向我解释在TSQL中执行此操作的语法吗?

Can anyone explain to me the syntax for doing this in TSQL?

推荐答案

这是标准TSQL游标中的通用循环.但是,请尽可能避免使用游标.他们的表现非常糟糕.

This is a generic loop in a standar TSQL Cursor. But try to avoid Cursors when possible. They Have very bad performance.

DECLARE @somevariable VARIABLE_TYPE_HERE
DECLARE @sampleCursor CURSOR

SET @sampleCursor = CURSOR FOR
SELECT somefield... from bla bla bla...

OPEN @sampleCursor
FETCH NEXT
FROM @sampleCursor INTO @somevariable
WHILE @@FETCH_STATUS = 0
BEGIN

PRINT @somevariable

FETCH NEXT
FROM @sampleCursor INTO @somevariable

END
CLOSE @sampleCursor
DEALLOCATE @sampleCursor

这篇关于将PLSQL Cursor FOR循环语法转换为TSQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-08 21:14