小编bhe*_*zel的帖子

在没有光标的SQL Server中循环

下面是我一直在研究的一些SQL Server代码.我现在知道使用光标通常是一个坏主意,但我无法弄清楚我还能做些什么.光标的性能很糟糕.我真的只是使用一些简单的IF语句逻辑与循环,但不能将其转换为SQL.我正在使用SQL Server 2012.

IF [Last Employee] = [Employee] AND [Action] = '1-HR'
    SET [Employee Record] = @counter + 1
ELSE IF [Last Employee] != [Employee] OR [Last Employee] IS NULL
    SET [Employee Record] = 1
ELSE
    SET [Employee Record] = @counter
Run Code Online (Sandbox Code Playgroud)

基本上,如何在没有光标的情况下保持这个@counter.我觉得解决方案很简单,但我迷失了自己.谢谢你的期待.

declare curr cursor for
select WORKER, SEQUENCE, ACTION
FROM [DB].[Transactional History]
order by WORKER ,SEQUENCE asc

declare @EmployeeID as nvarchar(max);
declare @SequenceNum as nvarchar(max);
declare @LastEEID as nvarchar(max);
declare @action as nvarchar(max);
declare @currentEmpRecord int
declare @counter …
Run Code Online (Sandbox Code Playgroud)

sql t-sql sql-server sql-server-2012

5
推荐指数
2
解决办法
1196
查看次数

标签 统计

sql ×1

sql-server ×1

sql-server-2012 ×1

t-sql ×1