9

Apologies if this has been asked before, the suggested 'related' questions weren't relevant and my own search turned up very little.

I need to list the definitions (code) of all my stored procedures, so that I can start working through all 450 of them and adding in semi-colons to work toward v2014 standards.

I found the following code here and adapted it slightly:

SELECT
    obj.Name AS SPName,
    REPLACE(modu.definition, 'CREATE PROC', 'ALTER PROC') + 'GO' AS SPDefinition
FROM
    sys.sql_modules modu INNER JOIN
    sys.objects obj ON modu.object_id = obj.object_id
WHERE
    (obj.type = 'P')
ORDER BY
    obj.name;

The code above is run using the SA account.

I assumed that this provided exactly what I needed, but, I've just noticed that somebody on MSDN is claiming it doesn't show the full text as expected.

When pasting the results into the SSMS editor, I have 46,000 lines of code to work through, so its not apparent to me if anything is missing. I'd like to be sure I've started this process correctly before wasting a day in the editor.

Therefore, I am reaching out to you pro's to ask if this is a genuine shortcoming of this approach, or is there an alternative method rather than one-by-one?

3
  • 1
    An alternative method is Powershell and SMO (simple-talk.com/sql/database-administration/…). Add the options for conditional drop and permissions.
    – Dan Guzman
    Commented Jul 31, 2015 at 11:26
  • 1
    Any reason why you wouldn't just script them out from SSMS? It is very good at creating scripts, either from Tasks/Generate Scripts.. or from Obj.Explorer Details, select all SPs and right click, Script stored procedure as ...drop and create. <-- that last one is what I would do if I wasn't going to use a tool to add the semicolons :) Commented Jul 31, 2015 at 12:36
  • 1
    No reason at all @MisterMagoo apart from that my tiny brain was reeling at the thought of the day ahead, and missed the simple things...
    – EvilDr
    Commented Jul 31, 2015 at 17:00

3 Answers 3

9

The problem mentioned on MSDN has nothing to do with sys.sql_modules or OBJECT_DEFINITION(); they've misinterpreted the problem. What actually happened is they were thwarted by an output limitation in Management Studio, which by default will only show 255 characters and at most 8192 in any output tuple in Results to Text.

So first, make sure you change this to 8192:

Tools > Options > Query Results > SQL Server > Results to Text >
Maximum number of characters displayed in each column

Now you can make one script for all the procedures that are less than 4K:

SELECT REPLACE(m.definition, N'CREATE PROC', N'ALTER PROC') 
  + CHAR(13) + CHAR(10) + N'GO'
FROM sys.sql_modules AS m
INNER JOIN sys.procedures AS p
ON m.[object_id] = p.[object_id]
WHERE LEN(m.definition) <= 4000
ORDER BY p.name;

(Of course, your replacement is naïve - what if they have create procedure (more than one space) or you are on a case sensitive collation? You may have to fix some of those manually.)

Now, you only have to worry about manually deriving the script for the larger procedures. First, see if you have any:

SELECT name 
  FROM sys.procedures 
  WHERE LEN(OBJECT_DEFINITION([object_id])) > 4000; 

I talk a little about some workarounds here (in the context of dynamic SQL, but the same would be true if you assigned OBJECT_DEFINITION() to a variable).

Mr. Magoo is right though, you will get the full definition without having to worry about truncation if you use the scripting functionality within Management Studio:

  • right-click a procedure, modify or script as > alter to >
  • in Object Explorer Details, do the same as above but for multiple
  • right-click a database, tasks > generate scripts > (but this does CREATE, not ALTER, so you'll need to perform replacements still)

As an aside, while you are adding semi-colons everywhere, I also suggest ensuring that all of your procedures are created with schema prefixes (e.g. dbo.procedurename, not just procedurename), and also for all object references inside the procedure. Those are likely to cause problems long before a lack of semi-colons ever will. Also a few general things to keep in mind while performing your changes.

9
  • @Hector Why do you think naïve is a typo? Commented Jul 31, 2015 at 13:01
  • Fantastic post, thank you. I ran the > 4000 script, and it pulled out about 80 proc's. When I checked one though in my orginial code, ALL of the code seems to be there (no truncation). Why would that be? In my SSMS Tools > Options, the Maximum Characters Retrieved value is 65535. Is that larger than normal (not sure I've change it a long time ago).
    – EvilDr
    Commented Jul 31, 2015 at 14:15
  • @EvilDr That's results to grid, which shouldn't be very useful for you since it swaps out things like carriage returns and line feeds for spaces and tabs or something (so that everything can appear on one line). I have the same setting and in both Results to Grid and Results to Text my procedure body was truncated. As for why there was no truncation in your specific case, I don't know. Maybe there is a ton of trailing white space in the procedure body? Maybe it was 4001 characters and the last character was a space? Commented Jul 31, 2015 at 14:22
  • @EvilDr Unlikely, I'm using the very most recent version of Management Studio and it still truncates the text, so it's not something that would have been recently fixed. Commented Jul 31, 2015 at 16:32
  • Thanks @Aaron. Just to help me get my head around this... After I had pasted the results into an editor and checked for the create issue (just one occurrence), I executed the query and it completed (47,800 lines of code in all and 550 proc's compiled). I then ran an alternate query with LEN(m.definition) sorted in descending order. The largest proc was 33,222 (characters?) in size. I located that in the object browser and went to modify it. It loaded up in all its former glory; no truncation, line breaks and tabs all in tact, everything (AFAIK). Am I just being extremely lucky here?
    – EvilDr
    Commented Jul 31, 2015 at 16:52
2

I would use the Object Explorer in SQL Server Management Studio and go from the top, one procedure at a time.

  1. Right click on the procedure.
  2. Select Modify.
  3. Edit the procedure code.
  4. List item
  5. Press F5 to execute the modification of the procedure.
  6. Close the tab.
  7. Go to 1.

Makes it easy to divide the work between you and your friend that does the same going from the bottom of the list of procedures. And it will not leave you with a half done gigantic wall of code when you need to go on a break or something.

You also minimize the time span where you can end up with editing conflicts between what you do and what someone else did with the same procedure. If you after 4 days of editing the 46,000 lines of code eventually execute the thing you could overwrite something someone else has done during the time of your editing. If you use source control (and you should) you would "only" end up with having to merge the changes.

1
  • There's only me, so that's not a problem. And there's no other requirements to modify stored procedures at this time, so a large file is not a problem. One-by-one is going to take an age just for the time spent opening each one. I have to replicate this on the live server once I'm done.
    – EvilDr
    Commented Jul 31, 2015 at 9:42
-2

In this scenario system table has always been useful.

Please use this query.

Select * From SYS.Comments Where Text = 'Stored Procedure Name'

Although this table as multiple rows for a single object_id but putting a concat function on the table helps to get a single ROW for a single object_id.

2
  • 1
    It's sys.syscomments, not sys.comments, and aside from the problems caused by single objects spanning rows (and the fact that your where clause won't work), it is deprecated and Microsoft says not to use it right at the top of the documentation. Unless you are stuck on ancient versions like SQL Server 2000, there are better, more modern ways to retrieve procedure definitions. Commented Jul 31, 2015 at 13:11
  • My answer had typos. I was not intended to write anything wrong. Thanks for bringing this up.
    – ADAVE19
    Commented Aug 12, 2015 at 12:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.