Click here to Skip to main content
15,904,416 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello,
Can anyone tell me the method of finding out the database name with the help of a stored procedure name.? I would like to know on few possible ways of how to find out.

Thanks in Advance
:)
Posted
Comments
KaushalJB 5-May-15 2:18am    
Your req is wierd.. Is you SP name same to DB name ?
Tomas Takac 5-May-15 8:38am    
I don't quite understand what you want? Would this help? SELECT DB_NAME() AS [Current Database][^]

Use this code:

NOTE:Replace ur Sp which you need to search in "Sales"

SQL
DECLARE @SQL NVARCHAR(max)
    ,@spName VARCHAR(100) = 'Sales'

SELECT @SQL = STUFF((
            SELECT ' UNION ALL
SELECT ' + quotename(NAME, '''') + ' AS Db_Name FROM ' + quotename(NAME) + '.INFORMATION_SCHEMA.Routines
WHERE ROUTINE_NAME = @spName AND ROUTINE_TYPE = ''PROCEDURE'''
            FROM sys.databases
            ORDER BY NAME
            FOR XML PATH('')
                ,TYPE
            ).value('.', 'nvarchar(max)'), 1, 11, '')


EXECUTE sp_executeSQL @SQL
    ,N'@spName varchar(100)'
    ,@spName
 
Share this answer
 
Comments
pn46 11-May-15 2:35am    
thank you so much for the help :)
Use this SP.
EXEC sp_databases

or
just use this query to find out the database names
Select * from Sys.Databases
 
Share this answer
 
v3
Comments
pn46 6-May-15 3:00am    
Hello, Thanks for the response..but i wanted to know that using Stored Procedure name, can i find out the related database name? if Yes, then how.?
Member 11039257 6-May-15 3:04am    
Related database name means? tell me the exact criteria you need.
pn46 6-May-15 6:27am    
hie,
i mean, i have a stored procedure name ,for which i dont know the database name. I want to find out the database name to which the stored procedure belong to.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900