Thursday 8 July 2010

Creating a 'System' Stored Procedure

Want to create a procedure that is available in any database (and will run in the context of that database) ?
  1. Create the procedure in the master database
  2. Prefix the name with sp_

USE master
GO

CREATE PROCEDURE sp_myproc AS
BEGIN
SELECT DB_NAME() AS CurrentDatabase
END
GO

USE tempdb
GO

EXEC sp_myproc
GO

No comments: