I'm not sure what you asked for sure. When you write this procedure for audit, I think you are asking how to get the current database name when the Stored Procedure exists in another database. eg.
USE DATABASE1 GO CREATE PROC spGetContext AS SELECT DB_NAME() GO USE DATABASE2 GO EXEC DATABASE1..spGetContext
This is the correct behavior, but not always what you are looking for. To get around this, you need to create an SP in the Master database and mark the procedure as a System procedure. The way this is done is different from the versions of SQL Server, but the SQL Server 2005 method is used here (this can be done in 2000 using the function master.dbo.sp_MS_upd_sysobj_category ).
USE MASTER CREATE FUNCTION sp_GetContext AS SELECT DB_NAME() GO EXEC sys.sp_MS_marksystemobject sp_GetContext USE DATABASE2 EXEC sp_GetContext
Hope this is what you were looking for
Ollie Sep 24 '08 at 21:23 2008-09-24 21:23
source share