sp_refreshviews for Stored Procs and Triggers?

sp_refreshviews for Stored Procs and Triggers?

Post by Amos Som » Mon, 10 Feb 2003 03:42:41



I have found sp_refreshview quite useful in helping determine which of my
views might be broke because of schema changes etc.  Is there something
equivalent for stored procedures and triggers?  I would like some way to
proactive about validating my stored procs and triggers instead of learning
about problems when my app runs.  Is anything available that does this?

Thanks very much.

 
 
 

sp_refreshviews for Stored Procs and Triggers?

Post by Dan Guzma » Mon, 10 Feb 2003 05:36:06


You can execute procedures with SET FMTONLY ON to identify those with
potentially problems.  The script below (SQL 2000) will generate a
script to do this for all user stored procedures in the current
database.  I'm not aware of a method to identify trigger errors, though.

SELECT
    N'SET FMTONLY ON EXEC ' +
    QUOTENAME(ROUTINE_SCHEMA) +
    N'.' +
    QUOTENAME(ROUTINE_NAME) AS ProcedureName,
    REPLICATE(N'NULL,',
    ISNULL((SELECT COUNT(*) AS Parameters
FROM INFORMATION_SCHEMA.PARAMETERS p
WHERE p.SPECIFIC_SCHEMA = r.ROUTINE_SCHEMA AND
    p.SPECIFIC_NAME = r.ROUTINE_NAME), 0)) AS Parameters
INTO #FmtOnlyExecutes
FROM INFORMATION_SCHEMA.ROUTINES r
WHERE r.ROUTINE_TYPE = 'PROCEDURE' AND
    OBJECTPROPERTY(OBJECT_ID(QUOTENAME(ROUTINE_SCHEMA) +
    N'.' +
    QUOTENAME(ROUTINE_NAME)), 'IsMSShipped') = 0
GO

--remove trailing comma from parameter list
UPDATE #FmtOnlyExecutes
SET Parameters = LEFT(Parameters, LEN(Parameters) - 1)
WHERE RIGHT(Parameters, 1) = N','
GO

--generate EXEC script
SELECT RTRIM(ProcedureName) +
    N' ' +
    Parameters + N'
GO' --GO must be on separate line
FROM #FmtOnlyExecutes
ORDER BY ProcedureName
GO

DROP TABLE #FmtOnlyExecutes
GO

--
Hope this helps.

Dan Guzman
SQL Server MVP

-----------------------
SQL FAQ links (courtesy  Neil Pike):

http://www.ntfaq.com/Articles/Index.cfm?DepartmentID=800
http://www.sqlserverfaq.com
http://www.mssqlserver.com/faq
-----------------------


Quote:> I have found sp_refreshview quite useful in helping determine which of
my
> views might be broke because of schema changes etc.  Is there
something
> equivalent for stored procedures and triggers?  I would like some way
to
> proactive about validating my stored procs and triggers instead of
learning
> about problems when my app runs.  Is anything available that does
this?

> Thanks very much.


 
 
 

sp_refreshviews for Stored Procs and Triggers?

Post by Amos Som » Tue, 11 Feb 2003 22:53:23


Dan,

Thanks for the code, but I'm not certain what it does, or whether it does
what I want.  I created a test stored procedure that Selects against a table
that doesn't exist. The code below didn't detect any problems with the
stored proc.

Amos.


> You can execute procedures with SET FMTONLY ON to identify those with
> potentially problems.  The script below (SQL 2000) will generate a
> script to do this for all user stored procedures in the current
> database.  I'm not aware of a method to identify trigger errors, though.

> SELECT
>     N'SET FMTONLY ON EXEC ' +
>     QUOTENAME(ROUTINE_SCHEMA) +
>     N'.' +
>     QUOTENAME(ROUTINE_NAME) AS ProcedureName,
>     REPLICATE(N'NULL,',
>     ISNULL((SELECT COUNT(*) AS Parameters
> FROM INFORMATION_SCHEMA.PARAMETERS p
> WHERE p.SPECIFIC_SCHEMA = r.ROUTINE_SCHEMA AND
>     p.SPECIFIC_NAME = r.ROUTINE_NAME), 0)) AS Parameters
> INTO #FmtOnlyExecutes
> FROM INFORMATION_SCHEMA.ROUTINES r
> WHERE r.ROUTINE_TYPE = 'PROCEDURE' AND
>     OBJECTPROPERTY(OBJECT_ID(QUOTENAME(ROUTINE_SCHEMA) +
>     N'.' +
>     QUOTENAME(ROUTINE_NAME)), 'IsMSShipped') = 0
> GO

> --remove trailing comma from parameter list
> UPDATE #FmtOnlyExecutes
> SET Parameters = LEFT(Parameters, LEN(Parameters) - 1)
> WHERE RIGHT(Parameters, 1) = N','
> GO

> --generate EXEC script
> SELECT RTRIM(ProcedureName) +
>     N' ' +
>     Parameters + N'
> GO' --GO must be on separate line
> FROM #FmtOnlyExecutes
> ORDER BY ProcedureName
> GO

> DROP TABLE #FmtOnlyExecutes
> GO

> --
> Hope this helps.

> Dan Guzman
> SQL Server MVP

> -----------------------
> SQL FAQ links (courtesy  Neil Pike):

> http://www.ntfaq.com/Articles/Index.cfm?DepartmentID=800
> http://www.sqlserverfaq.com
> http://www.mssqlserver.com/faq
> -----------------------



> > I have found sp_refreshview quite useful in helping determine which of
> my
> > views might be broke because of schema changes etc.  Is there
> something
> > equivalent for stored procedures and triggers?  I would like some way
> to
> > proactive about validating my stored procs and triggers instead of
> learning
> > about problems when my app runs.  Is anything available that does
> this?

> > Thanks very much.

 
 
 

sp_refreshviews for Stored Procs and Triggers?

Post by BP Margoli » Wed, 12 Feb 2003 04:15:27


Amos,

Check out the section "Deferred Name Resolution and Compilation" in the SQL
Server Books Online.

-------------------------------------------
BP Margolin
Please reply only to the newsgroups.
When posting, inclusion of SQL (CREATE TABLE ..., INSERT ..., etc.) which
can be cut and pasted into Query Analyzer is appreciated.


> Dan,

> Thanks for the code, but I'm not certain what it does, or whether it does
> what I want.  I created a test stored procedure that Selects against a
table
> that doesn't exist. The code below didn't detect any problems with the
> stored proc.

> Amos.



> > You can execute procedures with SET FMTONLY ON to identify those with
> > potentially problems.  The script below (SQL 2000) will generate a
> > script to do this for all user stored procedures in the current
> > database.  I'm not aware of a method to identify trigger errors, though.

> > SELECT
> >     N'SET FMTONLY ON EXEC ' +
> >     QUOTENAME(ROUTINE_SCHEMA) +
> >     N'.' +
> >     QUOTENAME(ROUTINE_NAME) AS ProcedureName,
> >     REPLICATE(N'NULL,',
> >     ISNULL((SELECT COUNT(*) AS Parameters
> > FROM INFORMATION_SCHEMA.PARAMETERS p
> > WHERE p.SPECIFIC_SCHEMA = r.ROUTINE_SCHEMA AND
> >     p.SPECIFIC_NAME = r.ROUTINE_NAME), 0)) AS Parameters
> > INTO #FmtOnlyExecutes
> > FROM INFORMATION_SCHEMA.ROUTINES r
> > WHERE r.ROUTINE_TYPE = 'PROCEDURE' AND
> >     OBJECTPROPERTY(OBJECT_ID(QUOTENAME(ROUTINE_SCHEMA) +
> >     N'.' +
> >     QUOTENAME(ROUTINE_NAME)), 'IsMSShipped') = 0
> > GO

> > --remove trailing comma from parameter list
> > UPDATE #FmtOnlyExecutes
> > SET Parameters = LEFT(Parameters, LEN(Parameters) - 1)
> > WHERE RIGHT(Parameters, 1) = N','
> > GO

> > --generate EXEC script
> > SELECT RTRIM(ProcedureName) +
> >     N' ' +
> >     Parameters + N'
> > GO' --GO must be on separate line
> > FROM #FmtOnlyExecutes
> > ORDER BY ProcedureName
> > GO

> > DROP TABLE #FmtOnlyExecutes
> > GO

> > --
> > Hope this helps.

> > Dan Guzman
> > SQL Server MVP

> > -----------------------
> > SQL FAQ links (courtesy  Neil Pike):

> > http://www.ntfaq.com/Articles/Index.cfm?DepartmentID=800
> > http://www.sqlserverfaq.com
> > http://www.mssqlserver.com/faq
> > -----------------------



> > > I have found sp_refreshview quite useful in helping determine which of
> > my
> > > views might be broke because of schema changes etc.  Is there
> > something
> > > equivalent for stored procedures and triggers?  I would like some way
> > to
> > > proactive about validating my stored procs and triggers instead of
> > learning
> > > about problems when my app runs.  Is anything available that does
> > this?

> > > Thanks very much.

 
 
 

1. Stored Procs vs. Triggers

I have tables that, due to the data model, cannot have FK constraints bound
to their columns; hence, I need to either incorporate referential integrity
either via insert and delete triggers, or via the stored procs that users
will call to insert/update/delete info.

Since all database access is through stored procs (no embedded SQL allowed),
does it make sense then to just write all of my RI into the stored
procedures and not use triggers?  Are there performance implications between
using SPs versus triggers?

TIA,
Blake

2. updating datefield with no date

3. Triggers, Stored Procs and Remote Data

4. Where can I find Net8 patch?

5. Deferred name resolution in stored procs and triggers but not views

6. 'Like' operator fails for no apparent reason

7. Need assistance with a trigger and stored procs

8. Job Scheduling Enterprise Manager

9. Error 1203 and Stored Procs from Triggers

10. renaming database in views,triggers, and stored procs

11. Triggers/Stored Procs/Permissions

12. Creating a unique identifier - triggers or stored procs?

13. Triggers, Stored Procs and SQL Mail problem