Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 get current database in T-SQL?

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2004-12-02 : 08:22:19
Howard writes "Using windows 2000 server/SQL Server 2000 sp3

I've been poking around your site and getting a few answers to a script I've been writing. I'm converting a database from one version of our application to a new version. Part of that process requires that I save some data to a text file - found a good way to do that on your site - thanks! I need to call xp_cmdshell and send it an osql query. The osql query needs the fully qualified database..table format and I have no guarantee that the client is running the database name that I think they should be. So, is it possible to get the current database name in the script programatically so I can add it to my query string?

An example:

select @str='osql -Q "select * from test..tablename where <some condition>" -w 300 -E -o C:\test\data.txt'
exec master..xp_cmdshell @str

I don't know for sure that the client would be running this on database 'test' in this case, they could be using 'test_convert' or some other database.

Thanks for the help."

dsdeming

479 Posts

Posted - 2004-12-02 : 08:25:20
Try SELECT DB_NAME() and use the result to build your @str.

Dennis
Go to Top of Page
   

- Advertisement -