How to View SQL Server Object Code Easily with sp_showcode

Comments 0

Share to social media

sp_helptext has served SQL Server DBAs for years, but it has its very obvious – and many – limitations: broken lines, missing triggers, no help with encrypted objects, to name just a few. That’s where sp_showcode comes in…

Why DBAs Need Better Code Retrieval

If there’s one thing I did a lot over the years as a SQL Server DBA, it was working with the likes of procedures and views. Straight away with SQL Server I learned to create procedures and, later, other types of objects, such as functions and triggers – objects that have code saved in the database.

It’s very common for those who work with these to frequently deal with the code, whether modifying it or just reading it to understand or confirm some behavior. There are those who prefer to use SQL Server Management Studio, navigate the hierarchical structure, right-click and open it.

And, there are those – like myself – who prefer to display the text directly from the code window with the famous sp_helptext, which I find much faster than using the interface, as demonstrated below:

Limitations of sp_helptext in SQL Server

sp_helptext originally returns the code as one column in multiple lines, whereby each line is a line of the source code. If you copy and paste directly into SSMS, you lose formatting. To bypass this, you can activate text mode in SSMS (shortcut CTRL+T) and voilà: your proc, however complex it may be, returns with the correct formatting and is much easier to copy…in most cases!

This is because, occasionally, the lines may be broken, or in the wrong place, and the column header Text comes along. In 99% of cases it’s still very useful, and I think it’s better than looking for it in the interface, but its limitations don’t stop there:

  • I need to know exactly what I’m looking for. If I only know part of the object’s name, I need to go to sys.objects or sys.procedures, find the exact name, and then call sp_helptext or use a function like the Object Definition.

  • Another pain is that it doesn’t decrypt by itself even if it’s a sysadmin, or if I’m connected as a Dedicated Admin (DAC).

  • And, if I want DDL Triggers, logon trigger? Forget it – sp_helptext won’t find it!

This is where I present to you an evolution after using sp_helptext so much in my life: sp_showcode

Let’s take a closer look at it.

Introducing sp_showcode: The Evolution of sp_helptext in SQL Server

Automatic Text Output

With sp_helptext, if I ask to display the code for the procedure MyProc, it will display it like this by default:

What the code for the procedure 'MyProc' looks like when displayed by default.

Note that it is a table with 1 column called Text, and the result. If I want to display it as text, I press the option in SSMS (or use the shortcut CTRL+T) and:

What happens when the 'text' option is selected in SSMS.
Despite the formatting seeming okay, there’s still the header with the text “text” which, if copied, you need to remove. It seems like a small thing but for someone who does it a lot, it gets repetitive.

Thus the formatting is maintained, but not everything is rosy. This is a more bizarre case, using stpPowerAlert_Log_Full, a standard procedure from the Power Alerts which warns about the use of logs:

An image showing the sp_helptext breaking a line where it shouldn't have.
sp_helptext ended up breaking a line where it shouldn’t have.

sp_showcode comes with several improvements here, the first being that you don’t need to enable SSMS text mode:

Image showing that, even with the default Grid option enabled, sp_showcode sent it to the text output.
Note that, even with the default Grid option enabled, sp_showcode sent it to the text output.

And it keeps the formatting identical:

Image showing that the line remained exactly as it was created.
The line remained exactly as created.

Note that it has everything there to make it easy to copy. There’s only one disadvantage: the maximum line length is 4000 characters (sp_helptext tries to circumvent this problem but makes the situation worse).

If you have an object with code that has a line with more than 4000 characters, see a doctor, sp_showcode will go to the next line and it’ll be different from the original. But, in over 10 years as a DBA, I don’t think I’ve ever seen code with such a long line – not even the internal procs created by Microsoft.

Between us, if someone crammed more than 4000 characters into a single line, it’s because they don’t want you to easily read that proc!

Want more hands-on help with your SQL Server?

SQL Toolbelt Essentials includes 10 ingeniously simple tools that cover your entire database development lifecycle. With no complex set up required, your team can start seeing results immediately.
Learn more & try for free

Using XML Mode with sp_showcode for Easy Copying in SQL Server

If you still don’t want text output for these cases, you can switch to XML:

Image showing that, if you still don't want text output for these cases, you can switch to XML.

Clicking on the returned ObjectDefinition column opens a new tab:

Image showing that clicking on the returned ObjectDefinition column opens a new tab.
Just copy and paste into a SQL tab. This is the same behavior as sp_whoisactive.

In this mode, you’ll only have problems if you use an invalid XML character; the procedure attempts to replace the most probable invalid characters with “?”, but there might be some that we haven’t mapped.

However, these are generally invisible control characters, so you’ll hardly use them in the procedure. If you encounter this problem, report the invalid character as an issue in my git repo (if possible), so we can think about improvements. Again, this is a problem that will only occur with XML output.

Search by Partial Object Name in SQL Server

It’s quite common to work with several procedures in a project, such as prcGetSales or prcNewSale. Sometimes you don’t remember the exact name (due to working with so many) and need to consult before using sp_helptext, which only accepts the full name. With sp_showcode, you can use % to perform a LIKE:

An image showing the use of % to perform a LIKE.
Before sp_showcode, I would do a select * from sys.objects with a name like ‘%sale%’, analyze the result, and only then use sp_helptext. Now, with sp_showcode, everything is right there!

When you specify % and sp_showcode finds more than one option, it understands that you don’t know what you want and simply shows you all the options. However, you can prevent this by copying the correct name, or changing some parameters which alters this behavior. In sp_helptext, though, you cannot do this.

An image showing that, in sp_helptext, you cannot prevent it showing you every single option when you specify %.

Multi-Database Search using sp_showcode in SQL Server

By default, sp_showcode only searches for objects in the current database (again, to avoid causing too much processing). But, you can search in other databases:

An image showing how you can search for objects in other databases.

By using the % filter, you specify that you want to look in other databases beyond the current one. Note that in the image above, sysalerts_performance_counters_view was correctly returned because of the sale string in sysalerts; you can filter this out while reviewing the results.

You can also filter specific databases. For example, suppose you have databases in the format StoreNNNN, where NNNN is a store code:

An image showing filtering the database procedures of Stores starting with 10.
Filtering the database procedures of Stores starting with 10.

If you want to display the text for all, use @all = 1:

An image showing using @all = 1 to display the text for all.

And use the following if you want to display it as a clickable XML in SSMS:

sp_showcode in SQL Server: Automatic Decryption for Encrypted Procedures

Have you tried to view the code of an encrypted procedure with sp_helptext?

An image showing an attempt to view the code of an encrypted procedure with sp_helptext.

If so, you would have noticed that, even if you have DAC (a special connection that a sysadmin can open and can access internal SQL tables), you’re presented with this error:

An image showing the error.

However, with sp_showcode, if you’re connected as DAC it’ll automatically decrypt the proc:

An image showing how, with sp_showcode, it'll automatically decrypt the proc if you're connected as DAC.

On the other hand, if it’s not set as DAC, you’ll see this warning:

An image showing the warning you'll receive if you're not connected to sp_showcode with DAC.

It’s worth remembering that only sysadmins can connect as DAC – another convenience for a DBA who may be investigating something or needs to recover procs created by someone no longer working for the organization.

They would still be able to do this without sp_showcode, but it’s more work. With sp_showcode, the ‘biggest’ hurdle is simply opening a DAC connection.

sp_showcode in SQL Server: Support for DDL Triggers

sp_helptext doesn’t display another common type of SQL Server object: DDL triggers.

An image showing how sp_helptext doesn't display DDL triggers.

With sp_showcode, just do what you learned above – pass the name (or part of it) and it will produce:

An image showing what's produced with sp_showcode

And remember: if you don’t recall the exact name, or even the bank, just use % to search.

How to Filter Multiple Options with the Exception of Others

Another great feature is to filter multiple options with the exception of others. Yes, this is copied from Ola’s maintenance procedures, because I find it a very valuable resource!

For example: suppose you want to find all objects, across all databases, containing ‘sale’ in the name – except those in the msdb or those that contain ‘report’ in the name. This is impossible with sp_helptext, but entirely doable with sp_showcode, as follows:

Image showing the ability to find all objects, across all databases, with 'sp_showcode'

Remember, there are multiple procs, including in msdb. So, in the last example, I’ll demonstrate how you can use negative filters with wildcards to your advantage.

In this case, I used the comma to specify multiple options, and the second one is a negation (starts with a -). The other parameters simply displayed their results as XML. To do this without sp_showcode, you’d spend a lot of time building your queries and looking in various databases.

sp_showcode: A Must-Have Tool for SQL Server DBAs

I created sp_showcode primarily to be used as an auxiliary procedure, especially with Management Studio. But, if you want more advanced scenarios, you have total control over the result with the other parameters.

For example, the export mode returns the original definition of each procedure found, and you can use it to read in your application, or a PowerShell script, etc. You can also remove the headers, and more.

So, although the main use is to facilitate daily life in SSMS, it can still be useful for backing up several procedures such as:

There are many other parameters there, and I’ve left a small documentation inside the procedure script and examples of how to use them. I invite you to explore it!

I am still using, testing, and adjusting it, so it may contain errors and even some performance issues in more extreme cases (e.g. when using wildcard and databases filtered contains multiple objects).

So, if you use it in production, be careful and monitor consumption. The more search options you provide, the more work it will have, and consequently, depending on the number of databases and objects, it may take longer. But, over time, I believe I will make it better and more efficient.

For years, whenever I needed to do these things, I spent a lot of time looking for the object and opening or writing the query in internal tables. It may seem like a small amount but, when you work with a lot of code, these small shortcuts can save you a great deal of time.

The overall objective of the proc is to make it easier to access the code of the objects, with more flexibility in how you do so. Going forward, I plan to add other types – such as Jobs – and allow advanced searches. Who knows, maybe even using natural language?

The procedure is free and available now in my SQL Scripts repository: sqlserver-lib/Modulos/sp.showcode.sql at main · rrg92/sqlserver-lib.

If you have suggestions or discover problems, please submit these and I will review them when I have time. Additionally, feel free to leave comments and feedback down below!

FAQs: sp_showcode in SQL Server

1. What is sp_showcode in SQL Server?

sp_showcode is a custom procedure that retrieves SQL object code with better formatting, search flexibility, and decryption support compared to sp_helptext.

2. How is sp_showcode different from sp_helptext in SQL Server?

Unlike sp_helptext, sp_showcode offers XML output, partial name search, multi-database queries, and automatic decryption for encrypted objects.

3. Can sp_showcode display DDL triggers in SQL Server?

Yes, sp_showcode supports DDL triggers, while sp_helptext does not.

4. Does sp_showcode in SQL Server require special permissions?

For encrypted objects in SQL Server, you’ll need a DAC connection and sysadmin rights. For regular objects, standard permissions apply.

5. Where can I get sp_showcode for SQL Server?

sp_showcode is free and available now in my SQL Scripts repository: sqlserver-lib/Modulos/sp.showcode.sql at main · rrg92/sqlserver-lib

Subscribe to the Simple Talk newsletter

Get selected articles, event information, podcasts and other industry content delivered straight to your inbox every two weeks.
Subscribe now

Article tags

Load comments

About the author

Rodrigo Ribeiro Gomes

See Profile

Rodrigo is a seasoned SQL Server DBA with more than 10 years of experience, having contributed to projects for both private companies and government institutions in Brazil. His work includes performance tuning, complex troubleshooting, and a keen interest in Windows Internals and CPU optimization. As the former Head of the SQL Server team at Power Tuning, he managed critical operations and led the team with a focus on efficiency and reliability. Now, as Head of Innovation, he seeks to bring fresh perspectives while actively studying AI and Machine Learning. Rodrigo can be found on... LinkedIn: http://www.linkedin.com/in/dba-rodrigo GitHub: https://github.com/RRG92 Hugging Face: https://huggingface.co/rrg92 Blog: https://iatalk.ing/

Rodrigo Ribeiro Gomes's contributions