Loop in SQL Server without a Cursor -


below sql server code have been working on. know using cursor bad idea in general, cannot figure out how else can make work. performance terrible cursor. i'm using simple if statement logic loop, can't translate sql. i'm using sql server 2012.

if [last employee] = [employee] , [action] = '1-hr'     set [employee record] = @counter + 1 else if [last employee] != [employee] or [last employee] null     set [employee record] = 1 else     set [employee record] = @counter 

basically, how can keep @counter going without cursor. feel solution simple, i've lost myself. looking.

declare curr cursor select worker, sequence, action [db].[transactional history] order worker ,sequence asc  declare @employeeid nvarchar(max); declare @sequencenum nvarchar(max); declare @lasteeid nvarchar(max); declare @action nvarchar(max); declare @currentemprecord int declare @counter int;  open curr fetch next curr @employeeid, @sequencenum, @action; while @@fetch_status=0  begin      if @lasteeid=@employeeid , @action='1-hr'     begin         set @sql = concat('update [db].[transactional history]         set emprecord=',+ @currentemprecord, '+1          worker=', @employeeid, ' , sequence=', @sequencenum)         execute sp_executesql @sql          set @counter=@counter+1;         set @lasteeid=@employeeid;         set @currentemprecord=@currentemprecord+1;     end     else if @lasteeid null or @lasteeid<>@employeeid         begin             set @sql = concat('update [db].[transactional history]             set emprecord=1             worker=', @employeeid, ' , sequence=', @sequencenum)             execute sp_executesql @sql             set @counter=@counter+1;             set @lasteeid=@employeeid;             set @currentemprecord=1         end     else         begin             set @sql = concat('update [db].[transactional history]             set emprecord=', @currentemprecord, '              worker=', @employeeid, ' , sequence=', @sequencenum)             execute sp_executesql @sql             set @counter=@counter+1;         end     fetch next curr @employeeid, @sequencenum, @action;     end  close curr; deallocate curr; 

below code build sample table. want increase emprecord every time record '1-hr', reset each new worker. before code executed, emprecord null records. table shows target output.

create table [db].[transactional history-test](     [worker] [nvarchar](255) null,     [source] [nvarchar](50) null,     [tab] [nvarchar](25) null,     [effective_date] [date] null,     [action] [nvarchar](5) null,     [sequence] [numeric](26, 0) null,     [emprecord] [numeric](26, 0) null,     [manager] [nvarchar](255) null,     [payrate] [nvarchar](20) null,     [salary_plan] [nvarchar](1) null,     [hourly_plan] [nvarchar](1) null,     [last_manager] [nvarchar](255) null ) on [primary]  go insert [db].[transactional history-test] ([worker], [source], [tab], [effective_date], [action], [sequence], [emprecord], [manager], [payrate], [salary_plan], [hourly_plan], [last_manager]) values (n'1', null, n'emp-position mgt', cast(n'2004-01-01' date), n'1-hr', cast(1 numeric(26, 0)), cast(1 numeric(26, 0)), n'3', n'hourly', null, null, null) go insert [db].[transactional history-test] ([worker], [source], [tab], [effective_date], [action], [sequence], [emprecord], [manager], [payrate], [salary_plan], [hourly_plan], [last_manager]) values (n'1', null, n'change job', cast(n'2004-05-01' date), n'5-jc', cast(2 numeric(26, 0)), cast(1 numeric(26, 0)), n'4', null, null, null, n'3') go insert [db].[transactional history-test] ([worker], [source], [tab], [effective_date], [action], [sequence], [emprecord], [manager], [payrate], [salary_plan], [hourly_plan], [last_manager]) values (n'1', null, n'emp-terminations', cast(n'2005-01-01' date), n'6-tr', cast(3 numeric(26, 0)), cast(1 numeric(26, 0)), n'4', null, null, null, n'4') go insert [db].[transactional history-test] ([worker], [source], [tab], [effective_date], [action], [sequence], [emprecord], [manager], [payrate], [salary_plan], [hourly_plan], [last_manager]) values (n'1', null, n'change job', cast(n'2010-05-01' date), n'5-jc', cast(4 numeric(26, 0)), cast(1 numeric(26, 0)), n'3', null, null, null, n'4') go insert [db].[transactional history-test] ([worker], [source], [tab], [effective_date], [action], [sequence], [emprecord], [manager], [payrate], [salary_plan], [hourly_plan], [last_manager]) values (n'1', null, n'emp-position mgt', cast(n'2011-05-01' date), n'1-hr', cast(5 numeric(26, 0)), cast(2 numeric(26, 0)), n'3', n'hourly', null, null, null) go insert [db].[transactional history-test] ([worker], [source], [tab], [effective_date], [action], [sequence], [emprecord], [manager], [payrate], [salary_plan], [hourly_plan], [last_manager]) values (n'1', null, n'cwr-position mgt', cast(n'2012-01-01' date), n'1-hr', cast(6 numeric(26, 0)), cast(3 numeric(26, 0)), null, null, null, null, null) go insert [db].[transactional history-test] ([worker], [source], [tab], [effective_date], [action], [sequence], [emprecord], [manager], [payrate], [salary_plan], [hourly_plan], [last_manager]) values (n'1', null, n'organizations', cast(n'2015-01-01' date), n'3-org', cast(7 numeric(26, 0)), cast(3 numeric(26, 0)), null, null, null, null, null) go insert [db].[transactional history-test] ([worker], [source], [tab], [effective_date], [action], [sequence], [emprecord], [manager], [payrate], [salary_plan], [hourly_plan], [last_manager]) values (n'1', null, n'organizations', cast(n'2015-01-01' date), n'3-org', cast(8 numeric(26, 0)), cast(3 numeric(26, 0)), null, null, null, null, null) go insert [db].[transactional history-test] ([worker], [source], [tab], [effective_date], [action], [sequence], [emprecord], [manager], [payrate], [salary_plan], [hourly_plan], [last_manager]) values (n'2', null, n'emp-terminations', cast(n'2001-01-01' date), n'6-tr', cast(9 numeric(26, 0)), cast(1 numeric(26, 0)), null, null, null, null, null) go insert [db].[transactional history-test] ([worker], [source], [tab], [effective_date], [action], [sequence], [emprecord], [manager], [payrate], [salary_plan], [hourly_plan], [last_manager]) values (n'2', null, n'emp-terminations', cast(n'2001-05-01' date), n'6-tr', cast(10 numeric(26, 0)), cast(1 numeric(26, 0)), null, null, null, null, null) go insert [db].[transactional history-test] ([worker], [source], [tab], [effective_date], [action], [sequence], [emprecord], [manager], [payrate], [salary_plan], [hourly_plan], [last_manager]) values (n'2', null, n'change job', cast(n'2004-01-01' date), n'5-jc', cast(11 numeric(26, 0)), cast(1 numeric(26, 0)), n'3', null, null, null, null) go insert [db].[transactional history-test] ([worker], [source], [tab], [effective_date], [action], [sequence], [emprecord], [manager], [payrate], [salary_plan], [hourly_plan], [last_manager]) values (n'2', null, n'change job', cast(n'2004-01-01' date), n'5-jc', cast(12 numeric(26, 0)), cast(1 numeric(26, 0)), n'3', null, null, null, n'3') go insert [db].[transactional history-test] ([worker], [source], [tab], [effective_date], [action], [sequence], [emprecord], [manager], [payrate], [salary_plan], [hourly_plan], [last_manager]) values (n'2', null, n'emp-position mgt', cast(n'2014-01-01' date), n'1-hr', cast(13 numeric(26, 0)), cast(2 numeric(26, 0)), n'4', n'salary', null, null, null) go insert [db].[transactional history-test] ([worker], [source], [tab], [effective_date], [action], [sequence], [emprecord], [manager], [payrate], [salary_plan], [hourly_plan], [last_manager]) values (n'3', null, n'emp-terminations', cast(n'2012-01-01' date), n'6-tr', cast(14 numeric(26, 0)), cast(1 numeric(26, 0)), null, null, null, null, null) go insert [db].[transactional history-test] ([worker], [source], [tab], [effective_date], [action], [sequence], [emprecord], [manager], [payrate], [salary_plan], [hourly_plan], [last_manager]) values (n'4', null, n'emp-position mgt', cast(n'2012-01-01' date), n'1-hr', cast(15 numeric(26, 0)), cast(1 numeric(26, 0)), null, null, null, null, null) go  select * db.[transactional history-test] 

this should reproduce logic of cursor in more efficient way

with t      (select *,                 iif(first_value([action]) on (partition worker                                                      order [sequence]                                     rows unbounded preceding) = '1-hr', 0, 1) +                  count(case                         when [action] = '1-hr'                             1                         end) on (partition worker                                         order [sequence]                                    rows unbounded preceding) _emprecord            db.[transactional history-test]) update t set    emprecord = _emprecord;  

Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -