Searching Stored SQL – Part 4

There are four main database objects that contain stored SQL: VIEWS, TRIGGERS, FUNCTIONS and STORED PROCEDURES. Earlier today, I created a pattern searching tool for FUNCTIONS. I am going to clone and modify the code so that it will work with VIEWS.

Again, I want to examine the dynamic SELECT statement that retrieves the user defined stored procedure names from a given database. This is the key element of the whole program.

I took the liberty to modify the original code so that it will work standalone. The current code returns the schema name, the object name, and a combination of both called full name. The full name is used by the system stored procedure called sp_helptext that returns the stored SQL code (text) for any object.

Both the sys.objects and sys.schemas system views are local to a given database. Therefore, the TSQL has to be dynamic since we do not know the database name ahead of time.

The WHERE clause has an expression that filters in the correct object type. I will substitute ‘P’ with ‘V’ for this code to search for VIEWS. Please refer to books on line for object type in sys.objects. Both the global and local temporary table names are changed to reflect VIEWS in our new procedure named [usp_get_text4vw].

Enclosed is the full stored procedure for your usage.

A sample call to the tool to list and save all function code in the AdventureWorks database to a user defined table.

We can filter by object name if it is known. I am looking for the user defined view called Additional Contact Information.

We can filter by actual code if we know a key value. I am looking for any views that have the calculated field Full Name.

The image below show the results of this search.

Again, the user defined table created by this procedure stays in the system until tempdb is recreated upon server startup. I hope you enjoyed these free pattern searching tools for Stored SQL.

Related posts

2 Thoughts to “Searching Stored SQL – Part 4”

  1. wut up do you have any other info like this one? im doing a project for my college and I need a few links to put on our school webblog. will it be fine if we refer to this blog from the school blog for reference purposes?

    Sure thing, just put down copyrighted by John Miner, the Crafty DBA and refer to the pages.

  2. Brandon Leach

    I use a very similar procedure at work.

Leave a Reply to Brandon Leach Cancel reply