Re: Stored procedure to check the syntax of stored procs, views, ...

From: Andrew (nonsemantic_at_aol.com)
Date: 07/17/04


Date: 16 Jul 2004 18:44:56 -0700

Here's what I'd do:

1.) I use a stored procedure called sp_grep. It'll search through the
syscomments table and look for references to your target string. You
can get it here:

http://examples.oreilly.com/wintrnssql/

(d/l the file called "example.zip" and look for sp_grep)

I would use this in a cursor to:

2.) Loop through all the objects in the sysobjects table and do an
sp_grep
on each object's name. This will create a list of all stored procs,
user functions, views, etc. that contain a reference to this object.
Since you're looping through sysobjects, you can filter out any object
types you don't want to include (maybe, for instance, you're not
interested in stored procedures that call the system tables).

3.) Insert this list into a temp table or some other suitable object.
If you're concerned about the table size, then you can use another
table (perhaps a memory table) to get the raw results, then insert
into the temp table where memory table entry is not in the temp table.

4.) Once this sucker has finshed executing (I suspect it'd take some
time to run, i.e. maybe 10 minutes or more), you have a table of all
the objects that are getting called by stored procedures, views, user
functions, etc.

5.) Finally, do something like "select * from sysobjects where name
not in (select distinct name from #tempUsed)". This will give you a
list of objects in your database that aren't being called by stored
procs, user functions, etc. You may want to exclude system objects
just so there's no confusion (system objects have their own xtypes, I
believe, for all the different types of objects).

This is a very rough method and not very fast, but if I had to do a
quick-and-dirty check on my database, that's how I'd do it. I'd be
happy to post example code (I can't post the sp_grep code, though --
but you can download that yourself) if any of this is unclear.

    -Andrew-

veerleverbr@hotmail.com (Veerle) wrote in message news:<16c55915.0407142217.7c565f94@posting.google.com>...
> Hi,
>
> It is only possible to create a stored procedure, views, triggers and
> user defined functions if
> (a) the syntax is correct
> (b) the objects (tables, views, ...) used, exist in the db
>
> For example, when a stored procedure is created, it is possible to
> rename one of the tables that is used, but not rename it in the code
> of the stored procedure. This makes the stored procedure invalid: when
> you try to execute it, you get an error.
>
> What I would like to have is a script that finds all the views, stored
> procedures, triggers and functions that use not existing column names,
> not existing tables, not existing views, not existing stored
> procedure, stored procedures with a wrong number of parameters, ...
>
> Finding all the views, stored procedures, triggers and functions
> through a select on sysobjects is quite easy, but how can i check
> them? Trying to execute them all is not an option because (a) there
> are too many of them and (b) this has an impact on the data which we
> do not want. Any idea's?
>
> Veerle



Relevant Pages

  • Re: Can a Table be Invalidated?
    ... khatri wrote: ... Just think in reference to the following: If an object is referenced through a stored procedure then when you access that procedure all the referenced objects are compiled at run time. ... But in a later course, if you alter the procedure, at the time all the referenced objects are invalidated. ... within a stored procedure it means, stored procedure references this table and not the table references that stored procedure. ...
    (comp.databases.oracle.misc)
  • Re: how to know the table is exist in the database
    ... executing sp_tables stored procedure is better since sysobjects ... >> structure or accesibility might change at some point. ... AFAIK it isn't recommended accessing sysobjects directly because it ...
    (microsoft.public.dotnet.framework.adonet)
  • Re: Return Results of sp_helptext via ADO
    ... 'basic sql for getting stored procedure text ... strSQLStoredProcBase = "select syscomments.text From sysobjects, ... then loop through recordset, appending row to prvious: ...
    (microsoft.public.data.ado)
  • RE: syntax to print list of SPs
    ... select name from sysobjects where type='u' ... For Stored Procedure ... "Vyshnavi" wrote: ... > This query will give you the objects shipped by Microsoft during installation. ...
    (microsoft.public.sqlserver.programming)
  • Re: Renaming Stored Prcedures via EM
    ... Internally SQL Server keeps the stored procedure name in two places. ... Database object names are in table called sysobjects. ... stored in syscomments. ...
    (microsoft.public.sqlserver.programming)