Skip to content

Instantly share code, notes, and snippets.

View alivarzeshi's full-sized avatar

Ali varzeshi alivarzeshi

View GitHub Profile
@alivarzeshi
alivarzeshi / gist:5d642bde069be93136fab4601945dda6
Last active July 31, 2021 04:46
SQL Server - How to find Open Transactions
--open transactions with text and plans
--session ID
--login name
--database context
--transaction begin time
--how many log records have been generated by the transaction
--how much log space has been taken up by those log records
--how much log space has been reserved in case the transaction rolls back
--the last T-SQL that was executed in the context of the transaction
@alivarzeshi
alivarzeshi / Standardize_Arabic_Characters.sql
Last active May 11, 2024 16:41
The code updates textual columns in a SQL Server database to standardize specific Arabic characters across all tables.
USE [SQLdmRepository];
GO
-- Declare variables to store the names of the table and column currently being processed.
DECLARE @TableName NVARCHAR(MAX);
DECLARE @ColumnName NVARCHAR(MAX);
-- Declare a cursor to iterate over all textual columns in the database.
-- This cursor selects the name of the table and column from the system catalog views.
-- Only user tables (xtype = 'U') and specific textual column types are considered.
@alivarzeshi
alivarzeshi / DYNQ_OflineAlldb.sql
Last active June 13, 2024 22:33
T-SQL Script to Take All User Databases Offline
DECLARE @dbName NVARCHAR(255)
DECLARE @sql NVARCHAR(MAX)
-- Create a cursor to iterate over all databases except system databases
DECLARE db_cursor CURSOR FOR
SELECT name
FROM sys.databases
WHERE name NOT IN ('master', 'model', 'msdb', 'tempdb') -- Exclude system databases
OPEN db_cursor
-- Step 1: Create a temporary table to hold the index fragmentation information
IF OBJECT_ID('tempdb..#IndexFragmentation') IS NOT NULL
DROP TABLE #IndexFragmentation;
CREATE TABLE #IndexFragmentation (
DatabaseName NVARCHAR(128),
SchemaName NVARCHAR(128),
TableName NVARCHAR(128),
IndexName NVARCHAR(128),
Fragmentation FLOAT
-- Parameter Declarations
DECLARE @DatabaseName NVARCHAR(128);
DECLARE @SchemaName NVARCHAR(128);
DECLARE @TableName NVARCHAR(128);
DECLARE @IndexName NVARCHAR(128);
DECLARE @Fragmentation FLOAT;
DECLARE @sql NVARCHAR(MAX);
-- Create a table to log index maintenance details
IF OBJECT_ID('dbo.IndexMaintenanceLog', 'U') IS NULL
@alivarzeshi
alivarzeshi / DYNQ_Count_Records_in_Each_Table_with_Schema.sql
Last active June 13, 2024 22:46
This script will provide a comprehensive count of records for each table in the database, considering different schemas. The results are displayed in descending order of record count, which can be useful for identifying the largest tables in terms of data volume.
-- Declare variables to hold table information
DECLARE @SchemaName NVARCHAR(128);
DECLARE @TableName NVARCHAR(128);
DECLARE @sql NVARCHAR(MAX);
-- Create a table to store the results
IF OBJECT_ID('tempdb..#TableRecordCounts') IS NOT NULL
DROP TABLE #TableRecordCounts;
CREATE TABLE #TableRecordCounts (
-- Check if the ErrorLog table already exists. If it does not exist, create it.
IF OBJECT_ID('dbo.ErrorLog', 'U') IS NULL
BEGIN
-- Create the ErrorLog table to store detailed information about errors.
CREATE TABLE dbo.ErrorLog
(
ErrorLogID INT IDENTITY(1,1) PRIMARY KEY, -- Primary key for the error log entry.
ErrorMessage NVARCHAR(4000), -- Detailed error message.
ErrorNumber INT, -- SQL Server error number.
ErrorSeverity INT, -- Error severity level.
Date/Time: 2023-03-03 08:00:00
Log Level: Information
Source: SQL Server
Message: SQL Server is starting at normal priority base (=7). This is an informational message only. No user action is required.
Date/Time: 2023-03-03 18:00:00
Log Level: Information
Source: SQL Server
Date/Time: 2023-03-03 09:00:00
Log Level: Information
Source: SQL Server
Message: Database 'MyNewDatabase' created successfully.
Database Name: MyNewDatabase
Event ID: 1800
Date/Time: 2023-03-03 10:00:00
Log Level: Information
Date/Time: 2023-03-03 10:00:00
Log Level: Information
Source: Backup
Message: Database backed up. Database: MyDatabase, creation date(time): 2023/02/02(14:00:00), pages dumped: 350, first LSN: 73000000009200037, last LSN: 73000000009600001, number of dump devices: 1, device information: (FILE=1, TYPE=DISK: {'C:\Backups\MyDatabase.bak'}).
Date/Time: 2023-03-03 11:00:00
Log Level: Information
Source: Restore
Message: Database restored. Database: MyDatabase, backup start date(time): 2023/03/03(10:00:00), backup end date(time): 2023/03/03(10:30:00), number of backup devices: 1, device information: (FILE=1, TYPE=DISK: {'C:\Backups\MyDatabase.bak'}).