如何查询当前 SQL Server 数据库实例的名称? [英] How can I query the name of the current SQL Server database instance?

查看:38
本文介绍了如何查询当前 SQL Server 数据库实例的名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这有点像先有鸡还是先有蛋"的查询,但是有人能想出一个可以返回查询执行的当前数据库实例名称的查询吗?当我说我理解这个悖论时,请相信我:如果您已经连接来执行查询,为什么还需要知道数据库实例的名称?在多数据库环境中进行审计.

It is a bit of a "chicken or egg" kind of query, but can someone dreamup a query that can return the name of the current database instance in which the query executes? Believe me when I say I understand the paradox: why do you need to know the name of the database instance if you're already connected to execute the query? Auditing in a multi-database environment.

我查看了联机丛书中的所有 @@ 全局变量.SELECT @@servername"很接近,但我想要数据库实例的名称而不是服务器.

I've looked at all the @@ globals in Books Online. "SELECT @@servername" comes close, but I want the name of the database instance rather than the server.

推荐答案

SELECT
 @@servername AS 'Server Name' -- The database server's machine name
,@@servicename AS 'Instance Name' -- e.g.: MSSQLSERVER
,DB_NAME() AS 'Database Name'
,HOST_NAME() AS 'Host Name' -- The database client's machine name

这篇关于如何查询当前 SQL Server 数据库实例的名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆