Yesterday while I was writing script for SQL SERVER – 2005 – Find Unused Indexes of Current Database . I realized that I needed SELECT statement where I get the name of the current Database. It was very simple script.
SELECT DB_NAME() AS DataBaseName
It will give you the name the database you are running using while running the query.
Reference : Pinal Dave (https://blog.sqlauthority.com)
113 Comments. Leave new
Thank Q for ur article….. it’s very useful….
Thank you.
Thank you for sharing!
Thank you so much…
Thanl u very useful for us
I look all the time for sql server info, and always see your smiling picture, with a clear answer, so thanks!
Perfect, just what I was looking for, and faster to find your post than searching BOL!
Thanks a lot!!!!!!!!
I needed to run a report that spanned multiple servers, as well as most of their databases. This works very well combined with the undocumented sp_MSforeachDB.
sp_MSforeachDB ‘use [?]; select user_code, u_name, superuser, lastlogin, groups, db_name() as database_name from ?..OUSR;’
Using the above allows me to search every database on the server for information in the OUSR table. If the table is not found it will of course throw an error message. But this works great for running a quick report.
It’s really awesome… Can any one help me how to find path of database stored
@Santhost v Kumar
Run this command in the database whose file location you want to know.
select filename from sysfiles
to get more details
select * from sysfiles
It will display all the files (.mdf and .ldf ), their physical names ( existing on Operating System) and logical names ( used inside sql server ) and location of these files on Operating system.
You can also check this using SSMS.
> Open SQL Server Management Studio.
>Open Object Explorer ( click View located on top ,select Object Explorer )
> Expand Databases
> Right click database name , click properties
> A new Dialogue box will open, Select Files from left side List names. on the right side, slide bar to the right to see more information, you can see Location of database files on Operating System ( this is under Path column ).
Regards
IM.
hi,
how can i get value from one database to another database?
thanks & regards
Sankara Narayanan
Hi,
If that database is on the same server, then you can use
DBNAME.dbo.TableName to access the value from the table
Thanks,
Tejas
Hi….
I wan to get All the DataBase Names in server
Can you give me a Query………
And ther above query is very usefull……..
Thanking u
Regards
Pavan
EXEC sp_databases
how can i get db name from where the record retrieved
for ur info: i use union syntax
for example:
DBName CustNo CustName
db1 cust1 custA
db1 cust2 custB
db2 cust1 custA
db3 cust1 custA
GREAT! You help me another time :)
Thanks,
it was helpfull..
can you help me how can i list all the table names of the database..
Padma
EXEC sp_tables
I am running a stored procedure from the master database in a database named “FOO”. Executing DB_NAME() returns “master”. How do you return “FOO”?
Hi,
Thanks a lot. The DB_NAME() command used in my coding. Thanks once again.
I want to supply db name from ouitside.how its posible
like
declare @DBNAME sysname ,@stm varchar(100)
Set @DBNAME =’database name ‘
use database name
Only thru a Dynamic SQL
Make sure you read this article fully
http://www.sommarskog.se/dynamic_sql