sql server - Update Cursor SQL Statement with If/Then/Else -
sql server - Update Cursor SQL Statement with If/Then/Else -
i struggling figure out how update if/then/else scenario. cursor grabbing records in select statement , compare them different table in linked server. if finds matching record, update count cursor. if doesn't find matching record insert row table.
the cursor below , current update command updating count record found match.
declare @cono int declare @repno nvarchar(10) declare @date datetime declare @count int declare cur cursor select '1' ppcono, b.new_salesrepid pprep1, max(convert(varchar(8), (d.scheduledstart - c.timezonebias / cast(24 * 60 float)), 112)) ppdate, count(b.new_salesrepid) ppcount, max(convert(varchar(8), (a.actualend - c.timezonebias / cast(24 * 60 float)), 112)) closedon activitypointerbase bring together systemuserextensionbase b on b.systemuserid = a.ownerid bring together usersettingsbase c on c.systemuserid = b.systemuserid bring together activitypartybase d on d.activityid = a.activityid b.new_salesrepid <> '99999999' , a.activitytypecode = '4201' , b.new_salesrepid not null , a.statecode = '1' , d.participationtypemask = '9' , convert(varchar(8), a.actualend - c.timezonebias / cast(24 * 60 float), 112) >= dateadd(day,datediff(day,1,convert(varchar(8), getdate(), 112)),0) , convert(varchar(8), a.actualend - c.timezonebias / cast(24 * 60 float), 112) < dateadd(day,datediff(day,0,convert(varchar(8), getdate(), 112)),0) /* , convert(varchar(8), a.actualend - c.timezonebias / cast(24 * 60 float), 112) < convert(varchar(8), getdate(), 112) */ grouping b.new_salesrepid, convert(varchar(8), (d.scheduledstart - c.timezonebias / cast(24 * 60 float)), 112) order b.new_salesrepid asc; fetch next cur @cono, @repno, @date, @count while (@@fetch_status=0) begin update [as400].s062f7ar.aplus83mds.pepappts01 set ppcount = ppcount + @count ppcono = @cono , pprep1 = @repno , ppdate = @date fetch next cur @cono, @repno, @date, @count end close cur deallocate cur
not sure how accomplish if/then/else scenario in sql. did little research , maybe using case way go can update commands? on sql 2008.
you can check if record exists , if perform update, else perform insert. example:
declare @cono int declare @repno nvarchar(10) declare @date datetime declare @count int declare @closedon datetime declare cur cursor select '1' ppcono, b.new_salesrepid pprep1, max(convert(varchar(8), (d.scheduledstart - c.timezonebias / cast(24 * 60 float)), 112)) ppdate, count(b.new_salesrepid) ppcount, max(convert(varchar(8), (a.actualend - c.timezonebias / cast(24 * 60 float)), 112)) closedon activitypointerbase bring together systemuserextensionbase b on b.systemuserid = a.ownerid bring together usersettingsbase c on c.systemuserid = b.systemuserid bring together activitypartybase d on d.activityid = a.activityid b.new_salesrepid <> '99999999' , a.activitytypecode = '4201' , b.new_salesrepid not null , a.statecode = '1' , d.participationtypemask = '9' , convert(varchar(8), a.actualend - c.timezonebias / cast(24 * 60 float), 112) >= dateadd(day,datediff(day,1,convert(varchar(8), getdate(), 112)),0) , convert(varchar(8), a.actualend - c.timezonebias / cast(24 * 60 float), 112) < dateadd(day,datediff(day,0,convert(varchar(8), getdate(), 112)),0) /* , convert(varchar(8), a.actualend - c.timezonebias / cast(24 * 60 float), 112) < convert(varchar(8), getdate(), 112) */ grouping b.new_salesrepid, convert(varchar(8), (d.scheduledstart - c.timezonebias / cast(24 * 60 float)), 112) order b.new_salesrepid asc; open cur; fetch next cur @cono, @repno, @date, @count, @closedon while (@@fetch_status=0) begin if exists (select 1 [as400].s062f7ar.aplus83mds.pepappts01 ppcono = @cono , pprep1 = @repno , ppdate = @date) begin update [as400].s062f7ar.aplus83mds.pepappts01 set ppcount = ppcount + @count ppcono = @cono , pprep1 = @repno , ppdate = @date end else begin insert [as400].s062f7ar.aplus83mds.pepappts01 (ppcount, ppcono, pprep1, ppdate) values (@count, @cono, @repno, @date) end fetch next cur @cono, @repno, @date, @count end close cur deallocate cur
sql sql-server sql-server-2008 cursor sql-update
Comments
Post a Comment