Name of current database?

  • I'm certain this shouldn't be so difficult to find but, alas, I don't seem to be able to figure this one out. Can anyone shed some light for me?

    I have an administrative proc that could be run from any database. I'd prefer to have it determine what database it's being run from rather than specify it on the command line. Though I can can manipulate every database and table on a whim, I seem unable to figure out where I'm doing it from...

  • Here is one way but not very simple. I'm guessing there is an easier way:

    -- What is the database name?????

    create table #tmp_sfs (

    fileid int,

    filegroup int,

    totalextents int,

    usedextents int,

    name varchar(1024),

    filename varchar(1024)

    )

    insert into #tmp_sfs execute('DBCC SHOWFILESTATS')

    declare @filename varchar(1024)

    select @filename = filename from #tmp_sfs

    declare @dbname varchar(1024)

    select @dbname=name from master.dbo.sysdatabases where filename =@filename

    Print 'This is the current database: ' + @dbname

    drop table #tmp_sfs

    -------------------------

    Gregory Larsen, DBA

    If you looking for SQL Server Examples check out my website at http://www.geocities.com/sqlserverexamples

    Gregory A. Larsen, MVP

  • select db_name()

    will give you the current database name

    Diane

  • See I told you there was an easier way, then my method. Thanks Diane

    -------------------------

    Gregory Larsen, DBA

    If you looking for SQL Server Examples check out my website at http://www.geocities.com/sqlserverexamples

    Gregory A. Larsen, MVP

  • Well, golllleeee. Thanks! I *knew* there was an easy way but I couldn't find it.

Viewing 5 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic. Login to reply