{"id":19,"date":"2014-11-01T20:05:00","date_gmt":"2014-11-01T20:05:00","guid":{"rendered":""},"modified":"2016-01-30T23:24:34","modified_gmt":"2016-01-30T23:24:34","slug":"t-sql-syntactic-sugar-for-updating-current-row-in-cursor","status":"publish","type":"post","link":"https:\/\/tpodolak.com\/blog\/2014\/11\/01\/t-sql-syntactic-sugar-for-updating-current-row-in-cursor\/","title":{"rendered":"T-SQL &#8211; syntactic sugar for updating current row in cursor"},"content":{"rendered":"<p>I am not a big fan of cursors, however for time to time I have to write one. In my case cursor usually is responsible for some complicated updates. Until yesterday I&#8217;ve been writing my update statements within the cursor the standard way<\/p>\n<pre lang=\"sql\">\r\n-- cursor code omitted for brevity\r\n-- some complicated update\r\nUPDATE Person.Person set FirstName = 'some logic goes here' where Person.BusinessEntityID = @Id\r\nwhere @Id variable is set by cursor. \r\n-- cursor code omitted for brevity\r\n<\/pre>\n<p>There is nothing wrong with that, however we can do the same thing using specialized syntax &#8211; <i>current of<\/i> keyword(s)<\/p>\n<pre lang=\"sql\">\r\nUPDATE Person.Person set FirstName = 'some logic goes here' where current of personCursor\r\n<\/pre>\n<p>The entire cursor statement then looks that way<\/p>\n<pre lang=\"sql\">\r\nDECLARE personCursor CURSOR FOR SELECT top 1000 * FROM Person.Person\r\nOPEN personCursor\r\nFETCH NEXT FROM personCursor\r\nWHILE @@FETCH_STATUS = 0\r\nBEGIN\r\n   -- some complicated update goes here\r\n   UPDATE Person.Person set FirstName = 'some logic goes here' where current of personCursor\r\n   FETCH NEXT FROM personCursor\r\nEND\r\nCLOSE personCursor\r\nDEALLOCATE personCursor\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>I am not a big fan of cursors, however for time to time I have to write one. In my case cursor usually is responsible for some complicated updates. Until yesterday I&#8217;ve been writing my update statements within the cursor the standard way &#8212; cursor code omitted for brevity &#8212; some complicated update UPDATE Person.Person [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[37,41],"tags":[185,187],"class_list":["post-19","post","type-post","status-publish","format-standard","hentry","category-sql","category-t-sql","tag-sql","tag-t-sql"],"_links":{"self":[{"href":"https:\/\/tpodolak.com\/blog\/wp-json\/wp\/v2\/posts\/19","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/tpodolak.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/tpodolak.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/tpodolak.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/tpodolak.com\/blog\/wp-json\/wp\/v2\/comments?post=19"}],"version-history":[{"count":2,"href":"https:\/\/tpodolak.com\/blog\/wp-json\/wp\/v2\/posts\/19\/revisions"}],"predecessor-version":[{"id":117,"href":"https:\/\/tpodolak.com\/blog\/wp-json\/wp\/v2\/posts\/19\/revisions\/117"}],"wp:attachment":[{"href":"https:\/\/tpodolak.com\/blog\/wp-json\/wp\/v2\/media?parent=19"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/tpodolak.com\/blog\/wp-json\/wp\/v2\/categories?post=19"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/tpodolak.com\/blog\/wp-json\/wp\/v2\/tags?post=19"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}