Maxrecursion issue SQL

I am trying to rebuild indexes from a list stored in a table. I am using IndexOptimize stored Procedure from https://ola.hallengren.com/ to accomplish. The difficulty is that it requires a table list and when I go over 100, I get a message from SQL Server regarding max recursion. I know that I can build a sp to do this but need a limit the time it takes to do which is built in. I was wondering If I coule get some help with max recursion error.


DECLARE @listStr VARCHAR(MAX)
DECLARE @myDatabase VARCHAR(55)
DECLARE @formatlist VARCHAR(MAX)
SET @MyDatabase = 'dbname'
SELECT @listStr =  + COALESCE(@listStr+',' , '')+ @myDatabase +'.'+ SchemaName +'.'+ TableName 
FROM IndexedTableList  WHERE id  >= 1 AND ID  < 102
SET @formatlist = @listStr
SELECT @formatlist


 
EXECUTE [master].[dbo].[IndexOptimize] 
@Databases = @MyDatabase, 
@LogToTable = 'Y', 
@FragmentationLow = 'INDEX_REBUILD_ONLINE',
@FragmentationMedium = 'INDEX_REBUILD_ONLINE',
@FragmentationHigh = 'INDEX_REBUILD_ONLINE', 
@MaxDop = 1,
@TimeLimit = 43200,
@Indexes = @formatlist

Open in new window

earngreenAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

arnoldCommented:
Your issue deals with building the long list that exceeds..

There are scripts/sps that use a loop versus building the list and then looping based on the list.
0
earngreenAuthor Commented:
Please provide an example as I am not sure what you mean. I also tried For XML PATH ('')),1,1,'')  with no success. Do you mean loop through 100 at a time.
0
arnoldCommented:
MS has a TSQl that looks at an index fragmentation and reorganizes/rebuilds based on a preset criteria.  The benefit of this it reduces the transactional database growth compared to using the
running the rebuild indexes on the entire DB without regard to its fragmentation.


 http://msdn.microsoft.com/en-us/library/ms188917.aspx
Look at Example D:
0
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

PortletPaulEE Topic AdvisorCommented:
The default recursion of 100 is probably stopping this, so you could add your own maxrecursion hint

I just simulated this using a CTE so it may be different using a table (or you could use a CTE of the table)

DECLARE @listStr VARCHAR(MAX)
DECLARE @myDatabase VARCHAR(55)
DECLARE @formatlist VARCHAR(MAX)
SET @MyDatabase = 'dbname'

;with cte (id, SchemaName) as (
  select 1 as id, cast(1 as varchar) + ' a long string that follows after a number' as astring
  union all
  select id + 1 , cast(id as varchar) + ' a long string that follows after a number'
  from cte
  where id < 103
  )

SELECT @listStr =  + COALESCE(@listStr+',' , '')+ @myDatabase +'.'+ SchemaName --+'.'+ TableName 
FROM cte  WHERE id  >= 1 AND ID  < 102
OPTION (MAXRECURSION 200)

SET @formatlist = @listStr
SELECT @formatlist

Open in new window

BUT, take care that the wanted string isn't truncated!!!
0
earngreenAuthor Commented:
I think that generating the list is not the problem. I was able to put that in the stored procedure and run it. I am running this from SQL Agent by the way. The problem is when the list is generated and passed to the sp in @formatlist variable. I am getting the error. Otherwise when I execte the portion that gets the list alone there is no error.


EXECUTE [master].[dbo].[IndexOptimize] 
@Databases = @MyDatabase, 
@LogToTable = 'Y', 
@FragmentationLow = 'INDEX_REBUILD_ONLINE',
@FragmentationMedium = 'INDEX_REBUILD_ONLINE',
@FragmentationHigh = 'INDEX_REBUILD_ONLINE', 
@MaxDop = 1,
@TimeLimit = 43200,
@Indexes = @formatlist

Open in new window

0
PortletPaulEE Topic AdvisorCommented:
Then I presume the issue is inside Ola's stored proc. Have you considered contacting him?

.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
earngreenAuthor Commented:
I dont think its the stored procedure as I can run this from ssms but not in a sql agent job.
0
PortletPaulEE Topic AdvisorCommented:
so, it isn't in the piece immediately above the SP, and it's not in the SP.
? ...
afraid I am out of ideas
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2008

From novice to tech pro — start learning today.