Monday, March 19, 2012

Bad Proc Template

There is a base-installed template for a stored procedure called "Create Procedure with CURSOR OUT" which has this snippet:

EXEC <Schema_Name, sysname, Schema_Name>.<Procedure_Name, sysname, Procedure_Name> <@.proc_cursor_name, , @.sample_procedure_cursor> = <@.variable_cursor_name, , @.test_cursor_variable> OUTPUT

WHILE (@.@.FETCH_STATUS = 0)

BEGIN

FETCH NEXT FROM <@.variable_cursor_name, , @.test_cursor_variable>

PRINT 'put user defined code here'

END

But even after instantiating it - it doesn't work. The "FETCH_STATUS" must be seeded with a prior "FETCH" before it has any meaning. This leads me to ask: (1) how could such a widely circulated template have such a conspicuous error and (2) why doesn't TSQL have a looping mechanism that does not require such awkward FETCH "pre-seeding"?

p.s. How do I insert code into a forum post that is single-spaced, rather than duoble-spaced (like my ugly double-spaced snippet above)

The template is wrong. You may want to file a bug at http://connect.microsoft.com. Or you could post this in the Tools forum since the template is owned by the tools team and part of SSMS. The looping mechanism without FETCH has been requested frequently by customers and we are considering adding such a syntax in a future version of SQL Server.

As far the copy & paste, if you paste the text from say SSMS query editor as text (not formatted text/html) it will work fine. Quick and dirty way to do it is to paste the text in notepad and copy from there. Of course you will lose the color formatting. I like the behavior of ISQLW and often use it rather than SSMS. You could post about the copy / paste issue with SSMS in the Tools forum also.

Btw, you can also use the utility below to paste some copied content (html, rtf or such formatted text) as plain text easily using different hot key combination:

http://www.stevemiller.net/puretext/

No comments:

Post a Comment