Current Database Context?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Shift4SMS
    New Member
    • Feb 2007
    • 14

    Current Database Context?

    This is probably an easy one but I'm banging my head against the wall trying to sculpture the perfect Google search. Anyway...

    Is there a way to determine the current connection's database context? By database context, I mean the current database selected by the last USE or the default database for the user.

    This may sound dumb (heck, don't you know the last USE statement you issued?) but I'm trying to debug a multi-threaded application and the context seems to be getting confused and I want to include some audit assert statements. Something like:

    if @@DATABASE != 'Northwind' begin
    RAISERROR 20000 'Database context is incorrect - currently ' + @@DATABASE + ', expecting Northwind'
    RETURN(1)
    end
    insert into xyz...
  • Shift4SMS
    New Member
    • Feb 2007
    • 14

    #2
    After posting my question, I finally crafted the Google phrase that found my answer: db_name()

    It was probably included in my original search results but when your search returns back 50,000+ results, it's a little hard to sift through the non-related results.

    if db_name() != N'Northwind' begin
    RAISERROR 20000 'Database context is incorrect - currently ' + db_name() + ', expecting Northwind'
    RETURN(1)
    end
    insert into xyz...

    Comment

    Working...