{"id":1703,"date":"2014-09-18T13:55:40","date_gmt":"2014-09-18T11:55:40","guid":{"rendered":"http:\/\/www.switchbl8.nl\/blog\/?p=1703"},"modified":"2014-09-18T16:20:03","modified_gmt":"2014-09-18T14:20:03","slug":"using-row_number-in-sql-server-update-statement","status":"publish","type":"post","link":"https:\/\/www.switchbl8.nl\/blog\/2014\/09\/using-row_number-in-sql-server-update-statement\/","title":{"rendered":"Using ROW_NUMBER() in SQL Server update statement"},"content":{"rendered":"<p>You can&#8217;t use ROW_NUMBER() in an update statement in SQL Server, so:<\/p>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">\r\nUPDATE TheTable\r\nSET    TheColumn = ROW_NUMBER();\r\n<\/pre>\n<p>won&#8217;t work. But sometimes that&#8217;s just what you want. This will do the trick:<\/p>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">\r\nUPDATE  Target\r\nSET     TheColumn = RowNum\r\nFROM\r\n(\r\n    SELECT  t.TheColumn, ROW_NUMBER() OVER(ORDER BY t.ID) AS RowNum\r\n    FROM    TheTable t\r\n\tWHERE    ...\r\n) AS Target;\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>You can&#8217;t use ROW_NUMBER() in an update statement in SQL Server, so: UPDATE TheTable SET TheColumn = ROW_NUMBER(); won&#8217;t work. But sometimes that&#8217;s just what you want. This will do the trick: UPDATE Target SET TheColumn = RowNum FROM ( SELECT t.TheColumn, ROW_NUMBER() OVER(ORDER BY t.ID) AS RowNum FROM TheTable t WHERE &#8230; ) AS <a href='https:\/\/www.switchbl8.nl\/blog\/2014\/09\/using-row_number-in-sql-server-update-statement\/' class='excerpt-more'>[&#8230;]<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[120,108,81],"class_list":["post-1703","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-sql","tag-sqlserver","tag-tips","category-1-id","post-seq-1","post-parity-odd","meta-position-corners","fix"],"_links":{"self":[{"href":"https:\/\/www.switchbl8.nl\/blog\/wp-json\/wp\/v2\/posts\/1703","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.switchbl8.nl\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.switchbl8.nl\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.switchbl8.nl\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.switchbl8.nl\/blog\/wp-json\/wp\/v2\/comments?post=1703"}],"version-history":[{"count":3,"href":"https:\/\/www.switchbl8.nl\/blog\/wp-json\/wp\/v2\/posts\/1703\/revisions"}],"predecessor-version":[{"id":1706,"href":"https:\/\/www.switchbl8.nl\/blog\/wp-json\/wp\/v2\/posts\/1703\/revisions\/1706"}],"wp:attachment":[{"href":"https:\/\/www.switchbl8.nl\/blog\/wp-json\/wp\/v2\/media?parent=1703"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.switchbl8.nl\/blog\/wp-json\/wp\/v2\/categories?post=1703"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.switchbl8.nl\/blog\/wp-json\/wp\/v2\/tags?post=1703"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}