Saturday, January 25, 2020

SQL Server Basic Queries

SQL Server Basic Queries


How to See Active SQL Server Connections For Database?

SELECT DB_NAME(dbid) AS DBName, COUNT(dbid) AS NumberOfConnections, loginame FROM    sys.sysprocesses GROUP BY dbid, loginame ORDER BY DB_NAME(dbid);

-- Get Current DatabaseID
SELECT DB_ID() DatabaseID;

-- Get Current DatabaseName
SELECT DB_NAME() DatabaseName;

-- Get DatabaseName from DatabaseID
SELECT DB_NAME(4) DatabaseName;

-- Get DatabaseID from DatabaseName
SELECT DB_ID('tempdb') DatabaseID;

-- Get all DatabaseName and DBID
SELECT name,database_id FROM sys.databases;

No comments:

Post a Comment