Showing posts with label SQL Server. Show all posts
Showing posts with label SQL Server. Show all posts

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;

SQL Server Basics Commands

SQL Server Basics Commands



How to create a database in SQL Server ?

Creating a new database using the CREATE DATABASE statement
The CREATE DATABASE statement creates a new database. The following shows the minimal syntax of the CREATE DATABASE statement:
CREATE DATABASE database_name;
CREATE DATABASE mqmprod;

Creating a new database using SQL Server Management Studio
First, right-click the Database and choose New Database… menu item.

How to check the database status of SQL Server?

This statement lists all databases in the SQL Server:
SELECT name FROM master.sys.databases ORDER BY name;
(or)
EXEC sp_databases;
(or)
sp_helpdb;

How to connect to a database in SQL Server?

USE database_name;
use mqmprod;