Friday, 7 February 2020

Currently All running queries in SQL Server

In this article, I am explaining how to find which SQL queries are being run on an SQL Server.

Using the below query, we can get list the queries running on SQL Server


SELECT b.TEXT,
a.session_id,
a.status,
a.command,
a.cpu_time,
a.total_elapsed_time
FROM sys.dm_exec_requests a
CROSS APPLY sys.dm_exec_sql_text(sql_handle) AS b

By using above query, we can find a list of queries. If we want to kill any query. We can use below command-


KILL session_id




Thursday, 30 January 2020

How to TRUNCATE ALL TABLES

In this article, I am explaining, How to delete all data or truncate all tables from the database in SQL Server. By using the below queries delete the data, not the structure.

For this, we can use the sp_MSforeachtable stored procedure:

For TRUNCATE  all tables :


USE DatabaseName
EXEC sp_MSforeachtable 'TRUNCATE TABLE ?'

For delete all data from tables :

USE MyDatabaseName

EXEC sp_MSforeachtable 'DELETE FROM ?'

Thursday, 7 November 2019

Altering a column: null to not null in SQL

In this article, I am explaining how to altering a column null to not null in Sql Server.

Query:-


First make all current NULL values disappear from that column

UPDATE [Table] SET [Column] = '' WHERE [Column] IS NULL

Then, update the column of table to disallow NULLs

ALTER TABLE [Table] ALTER COLUMN [Column] varchar(50) NOT NULL