我有一些PLSQL代码循环一些逻辑:
FOR I in cur1
LOOP
SELECT value1, value2
FROM db1..table1 t1
END LOOP;
任何人都可以向我解释在TSQL中执行此操作的语法吗?
答案 0 :(得分:2)
这是标准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
答案 1 :(得分:0)
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