How to ID the current database

  • SQL Server has extended sp's that handle SQL Mail, eg. xp_startmail, xp_sendmail & xp_stopmail. They are all in the master db. xp_sendmail can send emails, and can send query results in the body of the email. But for this work, you must pass to xp_sendmail certain paramters, including

    - the text of the query,

    - the databse in which the query is to be carried out.

    So I've got an sp called by my client app that calls xp_mail and passes all these things, but I'm stumped as to how to pass it the name of the db my sp is in - how does my sp know what it is? Or the current db of my connection , or whatever you want to call it? Bearing in mind this is for a published client app, so the db name could be anything the customer calls it.

    I could query the sysdatabases table, but I still don't have a point of reference as to which db-id or db name my sp is running under at that time.

    All I an think of is manually store the database name in a user table populated by my client app, query it & pass it my sp, which passes it to xp_mail. Surely there is a more elegent solution?

  • select db_id(),db_name()

Viewing 2 posts - 1 through 1 (of 1 total)

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