Results 1 to 4 of 4
  1. #1
    Join Date
    Oct 2004
    Location
    New Jersey
    Posts
    65

    Unanswered: Check current database

    I was wondering if it was possible to check what server database you are currently connected to, using T-SQL, when executing commands in Query Analyzer. Even though you choose the server and database when connection, sometimes by habit you may connect to the wrong server & DB, and execute an .SQL file. Is it possible to put a line of T-SQL at the beginning that performs this pseudo-code, as a safety feature:

    -- While in Query Analyzer with a certain .SQL file open:
    If current server <> 'TheCorrectServer' and current DB <> 'TheCorrectDB' then cancel this .SQL file execution.
    Retired Moderator at Xtreme VB Talk, for all of your Visual Basic needs.

  2. #2
    Join Date
    Nov 2003
    Location
    London
    Posts
    169
    Hi there

    In Query analyzer type the following to get server name

    select @@servername


    Unsure how to get database name, you could just always put;

    use <databasename>

    in your code, that way you would always be on the right database

  3. #3
    Join Date
    Nov 2003
    Posts
    2,935
    Provided Answers: 12
    SELECT db_name()

    returns the currently active database

  4. #4
    Join Date
    Oct 2004
    Location
    New Jersey
    Posts
    65
    Thanks a lot! That will help.
    Retired Moderator at Xtreme VB Talk, for all of your Visual Basic needs.

Posting Permissions

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