The reason for the error has been identified.
Please tell me, what is the worng with SP?
However nobody really addressed this question. Honestly I see a lot of things not necessarily wrong but far from optimal in here.
select * from [a number of table valued functions] You should avoid select * in code, it is fine for testing but NOT fine in production.
SplitString function is a potential for performance issue depending on how it is coded. Given the mass of cursors in the rest of this I would suspect this function is using a while loop?
c1 cursor. This looks like you could use STUFF instead of a cursor. More select *
In general I think you will find that in the future your naming conventions will cause you grief. They don't have anything that help identify what it is/does.
c2 cursor. This again looks like you could use STUFF instead of the cursor. More select *. the big change here would be to convert GetDataType from a scalar function to an iTVF.
c3 cursor. Another place you could use STUFF. More select *
c4 cursor. Another place you could use STUFF. More select *
Last but not least...you are wide open to sql injection attack. You generate a lot of dynamic sql and then execute it starting around line 154. What happens when somebody passes you 'MyTable; drop database tempdb;--'? Don't try that!!!
I don't think you need any cursors to do this and you need to read up a bit on sql injection and how to properly execute dynamic sql with parameters.
Need help? Help us help you.
Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/
for best practices on asking questions.
Need to split a string? Try Jeff Moden's splitter
.Cross Tabs and Pivots, Part 1 – Converting Rows to Columns Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs Understanding and Using APPLY (Part 1)Understanding and Using APPLY (Part 2)