Results 1 to 4 of 4
  1. #1
    Join Date
    Apr 2002
    Location
    Sunnyvale, CA USA
    Posts
    78

    Unanswered: How do I write dbo.getCurrentDbName()

    I am writing a TSQL script which will run from osql (where the database name is passed in as a command-line parameter). Within that SQL I need to figure the database name to use it as data in the script.

    In other words, I want to do something like this:-

    declare @my_db varchar(80)

    set @my_db = dbo.getCurrentDbName()

    insert into myTable
    select @my_db , current_timestamp

    Any suggestions how to figure out the database name?
    Thanks

  2. #2
    Join Date
    Apr 2002
    Location
    Sunnyvale, CA USA
    Posts
    78
    I have found one answer. It's not 100% reliable, but it's probably good enough for what I need. But any better suggestions would be appreciated:-

    SELECT DB_NAME(dbid)
    FROM master..sysprocesses
    where stmt_end=-1
    and hostname = HOST_NAME ( )

  3. #3
    Join Date
    Apr 2002
    Location
    Sunnyvale, CA USA
    Posts
    78
    Even better

    declare @dwh_dbname varchar(16)
    set @dwh_dbname=DB_NAME(DB_ID())

  4. #4
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    47
    why the db_name(db_id()) ?
    Just select db_name() will do
    -----------------
    KH


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •