{"id":56,"date":"2008-05-27T14:08:14","date_gmt":"2008-05-27T20:08:14","guid":{"rendered":"https:\/\/jackdonnell.com\/?p=56"},"modified":"2008-05-30T08:01:07","modified_gmt":"2008-05-30T14:01:07","slug":"t-sql-alternatives-to-using-cursors","status":"publish","type":"post","link":"https:\/\/jackdonnell.com\/?p=56","title":{"rendered":"T-SQL: Alternatives to Using Cursors"},"content":{"rendered":"<p><strong>[UPDATE]<\/strong> &#8212;<em> <\/em><a title=\"Older Article from 2002 on Temp Tables    \" href=\"http:\/\/www.sqlservercentral.com\/articles\/Performance+Tuning\/temptabl\/662\/\" target=\"_blank\"><em>SQLServerCentral.com<\/em><\/a><em>\u00a0has a great article( from 2002) on the use of Temp Tables. You may find the information useful.<\/em><\/p>\n<p>I have gotten a ton of hits some<a href=\"https:\/\/jackdonnell.com\/articles\/index.htm\"> links <\/a>that I created years ago on <a href=\"https:\/\/jackdonnell.com\/articles\/SQL_CURSOR.htm\" target=\"xcvxb\">T-SQL Cursors<\/a>. Sometimes they are the the best course of action , but may times they are not the most efficient. This posting has some a possible alternative.<\/p>\n<p><strong>Creating Tables with Identity Columns<\/strong><br \/>\nI like to create tables and temp tables with identity columns. You can then do row by row process the data using the id column. In another post, I will show you how to create and some uses for Table-Valued functions to provide similar utility found with using cursors. Oh, don&#8217;t forget our friend the Case Statement, either.<\/p>\n<p><!--more--><\/p>\n<p>Below is a an Example of using a temp table with an identity column:<br \/>\nI created a example database then created some very basic and useless views.<br \/>\nThe temp table is populated with the values from the sysobjects table for the views.<\/p>\n<p>I then step through the temp table using the identity column id and do a select on the type<br \/>\nand count of each of the views:<br \/>\n\ufffd<br \/>\n<code><br \/>\nUSE ExampleDB<br \/>\nGO<br \/>\n\/*<br \/>\nCreated a small datbase and created views of the<br \/>\nsysobjects table based upon the n<\/code><\/p>\n<p>*\/<br \/>\nIF OBJECT_ID (&#8216;objects_start_with_sys&#8217;,&#8217;v&#8217;) IS NOT NULL<br \/>\nBEGIN<br \/>\nDROP VIEW dbo.objects_start_with_sys<br \/>\nEND<br \/>\nGO<\/p>\n<p>CREATE VIEW dbo.[objects_start_with_sys]<br \/>\nAS<br \/>\nSelect name ,type<br \/>\nfrom sysobjects where name like &#8216;sys%&#8217;<br \/>\nGO<br \/>\nIF OBJECT_ID (&#8216;objects_start_with_ob&#8217;,&#8217;v&#8217;) IS NOT NULL<br \/>\nBEGIN<br \/>\nDROP VIEW objects_start_with_ob<br \/>\nEND<br \/>\nGO<\/p>\n<p>CREATE VIEW dbo.[objects_start_with_ob]<br \/>\nAS<br \/>\nSelect name ,type<br \/>\nfrom sysobjects where name like &#8216;ob%&#8217;<br \/>\nGO<\/p>\n<p>IF OBJECT_ID (&#8216;objects_start_with_que&#8217;,&#8217;v&#8217;) IS NOT NULL<br \/>\nBEGIN<br \/>\nDROP VIEW objects_start_with_que<br \/>\nEND<br \/>\nGO<br \/>\nCREATE VIEW dbo.[objects_start_with_que]<br \/>\nAS<br \/>\nSelect name ,type<br \/>\nfrom sysobjects where name like &#8216;que%&#8217;<br \/>\nGO<\/p>\n<p>IF OBJECT_ID (&#8216;objects_start_with_e&#8217;,&#8217;v&#8217;) IS NOT NULL<br \/>\nBEGIN<br \/>\nDROP VIEW objects_start_with_e<br \/>\nEND<br \/>\nGO<br \/>\nCREATE VIEW dbo.[objects_start_with_e]<br \/>\nAS<br \/>\nSelect name ,type<br \/>\nfrom sysobjects where name like &#8216;e%&#8217;<br \/>\nGO<\/p>\n<p>&#8212; Drop\/Create Temp Table with Identity Column for Order<br \/>\nIF OBJECT_ID (&#8216;TEMPDB..#GetViews&#8217;,&#8217;u&#8217;) IS NOT NULL<br \/>\nBEGIN<br \/>\nDROP TABLE #GetViews<br \/>\nEND<br \/>\nBEGIN<br \/>\nCREATE TABLE #GetViews(<br \/>\nid INT IDENTITY(1,1),<br \/>\nviewName VARCHAR(40) NOT NULL<br \/>\n)<br \/>\nEND<\/p>\n<p>INSERT INTO #GetViews<br \/>\nSelect RTRIM(name)viewName from sysobjects<br \/>\nwhere type=&#8217;v&#8217;<br \/>\norder by NAME DESC<\/p>\n<p>SELECT * from #GetViews<\/p>\n<p>DECLARE @I INT , @MaxI INT<\/p>\n<p>SELECT @I = 1<br \/>\n,@MaxI = MAX(id) from #GetViews<\/p>\n<p>WHILE @I &lt;= @MaxI<br \/>\nBEGIN<br \/>\nDECLARE @sqlStatement NVARCHAR(150)<br \/>\n&#8212; Build Select Statement<br \/>\nSELECT @sqlStatement = &#8216;Select type, Count(type) as [Count_items from_&#8217;+UPPER(RTRIM(viewName)) +&#8217;] from &#8216; + RTRIM(viewName)+ &#8216; Group By Type Order by Count(type) DESC&#8217;<br \/>\nfrom #GetViews where id = @I<br \/>\n&#8212; Run Select Statement<br \/>\nEXECUTE sp_ExecuteSQL @sqlStatement<br \/>\n&#8212; Increment @I to Next Value<br \/>\nSelect @I = @I +1<br \/>\nEND<\/p>\n<p>Select id<br \/>\n,viewname<br \/>\nfrom #GetViews order by viewname<\/p>\n<p>&#8212; Drop Temp Table with Identity Column for Order<br \/>\nIF OBJECT_ID (&#8216;TEMPDB..#GetViews&#8217;,&#8217;u&#8217;) IS NOT NULL<br \/>\nBEGIN<br \/>\nDROP TABLE #GetViews<br \/>\nEND<\/p>\n","protected":false},"excerpt":{"rendered":"<p>[UPDATE] &#8212; SQLServerCentral.com\u00a0has a great article( from 2002) on the use of Temp Tables. You may find the information useful. I have gotten a ton of hits some links that I created years ago on T-SQL Cursors. Sometimes they are &hellip;<\/p>\n<p class=\"read-more\"><a href=\"https:\/\/jackdonnell.com\/?p=56\">Read more &raquo;<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_exactmetrics_skip_tracking":false,"_exactmetrics_sitenote_active":false,"_exactmetrics_sitenote_note":"","_exactmetrics_sitenote_category":0,"footnotes":""},"categories":[138,5],"tags":[218,221,291,219,220],"class_list":["post-56","post","type-post","status-publish","format-standard","hentry","category-programming","category-t-sql","tag-cursor","tag-example","tag-t-sql","tag-temp","tag-views"],"_links":{"self":[{"href":"https:\/\/jackdonnell.com\/index.php?rest_route=\/wp\/v2\/posts\/56","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jackdonnell.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jackdonnell.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jackdonnell.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/jackdonnell.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=56"}],"version-history":[{"count":0,"href":"https:\/\/jackdonnell.com\/index.php?rest_route=\/wp\/v2\/posts\/56\/revisions"}],"wp:attachment":[{"href":"https:\/\/jackdonnell.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=56"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jackdonnell.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=56"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jackdonnell.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=56"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}