sql server - SQL insert statement for each update row -


now made cursor update in 2 tables , insert in 1 table based on specific select statement select statement return 2 columns x , y need x update in table "px" because x primary key in table , need x update in table "fx" because x foreign key in table insert in third table x data.

i need change cursor , use update , insert script tried found need make loop achieve target if 1 know if can change cursor .

and in advance

declare @id int declare @clientid uniqueidentifier declare @code int declare @wtime int  declare @closecomplaint cursor set @closecomplaint = cursor fast_forward      select complaintid, [clientid]     complaint     complaintstatusid = 5         , (waitingforcutomerclosedatetime < getdate() or            waitingforcutomerclosedatetime = getdate())     open @closecomplaint  fetch next @closecomplaint @id, @clientid  while @@fetch_status = 0 begin     select          waitingforcutomerclosetime = @wtime              systemconfiguration              clientid = @clientid      select          [code] = @code              [dbo].[resp_users]              clientid = @clientid      update activity      set activitystatus = 4,          completiondate = getdate(),         closedby = @code      [complaintid] = @id      update [dbo].[complaint]      set complaintstatusid = 2      [complaintid] = @id      insert [dbo].[note] ([note_description], [clientid], [user_code], [visible_internal],                               [complaintid], [note_datetime], [complainantid],                               [onestopdesk_customeremail], [onestopdesk_customerusername], [private])     values (n'automatically closed system after ' + @wtime, @clientid, @code, 1,             @id, getdate(), null, null, null, 1)      fetch next @closecomplaint @id, @clientid end  close @closecomplaint deallocate @closecomplaint 

i'm not entirely sure got right (you didn't post table structures, can guess @ times how tables connected) - should able of in 3 simple, set-based statements - , should lot faster cursor!

-- declare table variable declare @input table (compaintid int, clientid int)  -- save rows table variable insert @input (complaintid, clientid)    select complaintid, clientid    dbo.complaint    complaintstatusid = 5      , waitingforcustomerclosedatetime <= getdate()  update  set activitystatus = 4,      completiondate = getdate(),     closedby = u.code dbo.activity inner join @input on a.complaintid = i.complaintid inner join dbo.resp_users u on i.clientid = u.clientid  update dbo.complaint set complaintstatusid = 2       complaintstatusid = 5     , waitingforcustomerclosedatetime <= getdate()  insert dbo.note ([note_description], [clientid], [user_code], [visible_internal],                       [complaintid], [note_datetime], [complainantid],                       [onestopdesk_customeremail], [onestopdesk_customerusername], [private])     select         n'automatically closed system after ' + sc.waitingforcustomerclosetime,          i.clientid, u.code, 1,         i.complaintid, getdate(), null, null, null, 1             @input     inner join         dbo.systemconfiguration sc on i.clientid = sc.clientid     inner join         dbo.resp_user u on u.clientid = i.clientid 

Comments

Popular posts from this blog

c# - Binding a comma separated list to a List<int> in asp.net web api -

Delphi 7 and decode UTF-8 base64 -

html - Is there any way to exclude a single element from the style? (Bootstrap) -