{"id":133,"date":"2010-02-03T11:52:51","date_gmt":"2010-02-03T17:52:51","guid":{"rendered":"https:\/\/jackdonnell.com\/?p=133"},"modified":"2016-01-20T16:50:33","modified_gmt":"2016-01-20T22:50:33","slug":"t-sql-using-paramter-table-like-a-cursor","status":"publish","type":"post","link":"https:\/\/jackdonnell.com\/?p=133","title":{"rendered":"T-SQL  Using Parameter Table like a Cursor"},"content":{"rendered":"<p>The Google gods seem to like a very old page of mine about using <a title=\"Google Search: T-SQl Cursor Result\" href=\"https:\/\/jackdonnell.com\/articles\/SQL_CURSOR.htm\">Cursors in T-SQL<\/a>. So, from time to time\u00a0I get commnets via email.\u00a0 Thought\u00a0I \u00a0would share an alternative to using cursors.<\/p>\n<p><!--more--><\/p>\n<p>Here is a bit of code works like a cursor, but perhaps a bit more less intense on your SQl Server System.\u00a0\u00a0I cobbled it pretty quick and it seems to run ok on SQL Server 2005.<\/p>\n<p>Thanks &#8211; Jack<\/p>\n<p>USE MASTER<br \/>\nGo<br \/>\n\/*<br \/>\nCONSUME PARAMETER TABLE Like a CURSOR<br \/>\nJCD 2-3-2010<\/p>\n<p>SWITCH TO TEMP Table when working<br \/>\nwith large Data sets<\/p>\n<p>*\/<br \/>\nDECLARE\u00a0 @ID INT &#8212; Begining Record<br \/>\n,@maxID INT &#8212; Max ID from Parameter Table<br \/>\n&#8212; DECLARE\/CREATE THE TABLE<\/p>\n<p>DECLARE @ObjectTable TABLE(<br \/>\nID INT IDENTITY(1,1) NOT NULL<br \/>\n,ObjectName VARCHAR(75) NOT NULL<br \/>\n,SumofallChars BIGINT DEFAULT (0)<br \/>\n)<\/p>\n<p>\/*<br \/>\nTo replace with Temp Table<br \/>\nA. TEST for #ObjectTable Table and DROP<br \/>\nand Create #ObjectTable<\/p>\n<p>IF OBEJCT_ID(&#8216;tempdb.dbo.#ObjectTable&#8217;,&#8217;u&#8217;) IS NOT NULL<br \/>\nBEGIN<br \/>\nDROP #ObjectTable<br \/>\nEND<\/p>\n<p>BEGIN<br \/>\nCREATE TABLE #ObjectTable<br \/>\n(<br \/>\nID INT IDENTITY(1,1) NOT NULL<br \/>\n,ObjectName VARCHAR(75) NOT NULL<br \/>\n,SumofallChars BIGINT DEFAULT (0)<br \/>\n)\ufffd<br \/>\nEND<\/p>\n<p>B. Then replace all the references to #ObjectTable<\/p>\n<p>*\/<\/p>\n<p>&#8212; POPULATE PARMETER TABLE<br \/>\n&#8212; Just Grabbing top 15 Records<br \/>\nINSERT INTO @ObjectTable (ObjectName)<br \/>\nSELECT DISTINCT<br \/>\nTOP 15<br \/>\nRTRIM([name]) as [ObjectName]<br \/>\nFROM\u00a0\u00a0 sys.sysobjects where type=&#8217;s&#8217;<br \/>\nORDER BY\u00a0 RTRIM([name])<br \/>\n&#8212; Part That uses the Table Row By Row<br \/>\n&#8212; Find the number or Rows<br \/>\nSELECT @ID = 1<br \/>\n,@MaxID = MAX(ID)<br \/>\nFROM @ObjectTable<\/p>\n<p>&#8212; Test For Empty Data Set<\/p>\n<p>If (Select Count(1)\u00a0FROM\u00a0 @ObjectTable) = 0<br \/>\nBEGIN<br \/>\nPRINT &#8216;No Records Found&#8217;<br \/>\nGOTO ENDPROC<br \/>\nEND<br \/>\n\/*<br \/>\nDisplay ALL Data<br \/>\nRETURN all Rows in PARAMETER TABLE<br \/>\nNormall Comment out except for Debugging<br \/>\n*\/<br \/>\nSELECT ID, ObjectName , SumofallChars<br \/>\nFROM\u00a0 @ObjectTable<br \/>\nORDER BY ID<br \/>\n&#8212; Start to Loop through records<\/p>\n<p>WHILE @ID &lt;= @MaxID<br \/>\nBEGIN<br \/>\nSET NOCOUNT ON<\/p>\n<p>&#8212; Bit of Code to Pretend this is useful<br \/>\n\/*<br \/>\nA. Select Data<br \/>\nB. Update Data Row<br \/>\n1. reverse Name<br \/>\n2. Create a Cumlative Sount of all objectname<br \/>\nCharacters then add length of current row<br \/>\n3. Return Data<br \/>\n*\/<\/p>\n<p>&#8212;Do a Weird Useless update<br \/>\nUPDATE\u00a0 @ObjectTable<br \/>\nSET [ObjectName] = REVERSE([ObjectName])<br \/>\n,SumofallChars = (SELECT SUM(SumofallChars) from @ObjectTable )\u00a0 + LEN(ObjectName)<br \/>\nFROM @ObjectTable<br \/>\nWHERE ID = @ID<\/p>\n<p>&#8212; Increment @ID to step through Records<br \/>\nSET @ID=@ID +1<br \/>\nEND<br \/>\n&#8212; RETURN &#8216;Updated&#8217; Rows in PARAMETER TABLE<br \/>\nSELECT\u00a0 ID, ObjectName , SumofallChars<br \/>\nFROM\u00a0 @ObjectTable<br \/>\nORDER BY ID DESC<br \/>\nENDPROC:<\/p>\n<p>GO<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Searching Google about Cursors in t-SQl might get this result https:\/\/jackdonnell.com\/articles\/SQL_CURSOR.htm. Here is an alternative<\/p>\n<p class=\"read-more\"><a href=\"https:\/\/jackdonnell.com\/?p=133\">Read more &raquo;<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_exactmetrics_skip_tracking":false,"footnotes":""},"categories":[266,138,22,51,5],"tags":[218,261,373,315,249,35,36,295,291,343],"class_list":["post-133","post","type-post","status-publish","format-standard","hentry","category-dba","category-programming","category-sql-server","category-ssrs","category-t-sql","tag-cursor","tag-loop","tag-object_id","tag-query","tag-script","tag-select","tag-sql","tag-sql-server","tag-t-sql","tag-tempdb"],"_links":{"self":[{"href":"https:\/\/jackdonnell.com\/index.php?rest_route=\/wp\/v2\/posts\/133","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=133"}],"version-history":[{"count":8,"href":"https:\/\/jackdonnell.com\/index.php?rest_route=\/wp\/v2\/posts\/133\/revisions"}],"predecessor-version":[{"id":635,"href":"https:\/\/jackdonnell.com\/index.php?rest_route=\/wp\/v2\/posts\/133\/revisions\/635"}],"wp:attachment":[{"href":"https:\/\/jackdonnell.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=133"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jackdonnell.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=133"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jackdonnell.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=133"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}