将 PLSQL 游标 FOR 循环语法转换为 TSQL

发布于 2024-09-15 14:58:57 字数 178 浏览 4 评论 0原文

我有一些 PLSQL 代码,它循环执行一些逻辑:

 FOR I in cur1
 LOOP

    SELECT value1, value2
    FROM db1..table1 t1

 END LOOP;

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

I have some PLSQL code which loops through some logic:

 FOR I in cur1
 LOOP

    SELECT value1, value2
    FROM db1..table1 t1

 END LOOP;

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

森林很绿却致人迷途 2024-09-22 14:58:57

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

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 

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 
魄砕の薆 2024-09-22 14:58:57

T-SQL 中没有 FOR。 WHILE 示例:

DECLARE Employee_Cursor CURSOR FOR
SELECT EmployeeID, Title 
FROM AdventureWorks2008R2.HumanResources.Employee

OPEN Employee_Cursor;

FETCH NEXT FROM Employee_Cursor;

WHILE @@FETCH_STATUS = 0
BEGIN
   FETCH NEXT FROM Employee_Cursor;
END;

CLOSE Employee_Cursor;

DEALLOCATE Employee_Cursor;

有关详细信息:http://msdn.microsoft.com /en-us/library/ms178642.aspx

There is no FOR in T-SQL. An example with WHILE:

DECLARE Employee_Cursor CURSOR FOR
SELECT EmployeeID, Title 
FROM AdventureWorks2008R2.HumanResources.Employee

OPEN Employee_Cursor;

FETCH NEXT FROM Employee_Cursor;

WHILE @@FETCH_STATUS = 0
BEGIN
   FETCH NEXT FROM Employee_Cursor;
END;

CLOSE Employee_Cursor;

DEALLOCATE Employee_Cursor;

For more information: http://msdn.microsoft.com/en-us/library/ms178642.aspx

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文