Skip to content

Instantly share code, notes, and snippets.

@lionofdezert
lionofdezert / LogonTrigger.sql
Created July 24, 2012 07:21
SQL Server: Restrict Login from Valid Machine IPs Only (Using Logon Trigger)
USE master
GO
-- Create table to hold valid IP values
CREATE TABLE ValidIPAddress (IP NVARCHAR(15)
CONSTRAINT PK_ValidAddress PRIMARY KEY)
-- Declare local machine as valid one
INSERT INTO ValidIPAddress
SELECT '<local machine>'
@lionofdezert
lionofdezert / DatabaseGrowthHTMLReport.sql
Created July 24, 2012 08:02
SQL Server: Script to Generate HTML Report/mail for Databses Current Size, Growth Rate and Available Disk Space
/*
Script to send an alert through mail, with information that how many drive
space is required from next databases growth on a specific instance and how many
space is available.
Script By: Amna Asif for ConnectSQL.blogspot.com
*/
@lionofdezert
lionofdezert / TablesRelationshipTSQL.sql
Created July 31, 2012 16:27
SQL Server : Tables Relationship Report Through TSQL
-- Tables Relationship Script
-- Script By: Syed Muhammad Yasir for http://connectsql.blogspot.com
-- Updated August 1, 2012
--
SELECT CASE WHEN a.parent_object_id IS NULL
THEN parent.name + '-1--*-' + child.name
ELSE parent.name + '-1--1-' + child.name
END AS TablesWithRelations
@lionofdezert
lionofdezert / DatabaseRestoreLog.sql
Created August 2, 2012 11:47
Database Restore Log
/************************
Script Purpose: To Keep Database Restore Log
Script By : Aasim Abdullah for https://connectsql.blogspot.com
************************/
USE master
GO
CREATE TABLE DatabaseRestoreLog
(DatabaseName VARCHAR(50), RestoreDate DATETIME, RestoredFrom VARCHAR(500))
@lionofdezert
lionofdezert / CasecadeDelete.sql
Created August 2, 2012 19:38
Casecade Delete in SQL Server
USE AdventureWorks
GO
--============== Supporting function dbo.udfGetFullQualName
IF OBJECT_ID('dbo.udfGetFullQualName') IS NOT NULL
DROP FUNCTION dbo.udfGetFullQualName
GO
CREATE FUNCTION dbo.udfGetFullQualName ( @ObjectId INTEGER )
@lionofdezert
lionofdezert / SQLLogMail.sql
Created August 7, 2012 15:02
SQL Server Log HTML Mail
-- Table variable to hold intermediate data
DECLARE @ReportSQLErrorLogs TABLE
(
[log_date] [datetime] NULL,
[processinfo] [varchar](255) NULL,
[processtext] [text] NULL
)
DECLARE @NumErrorLogs INT,
@CurrentLogNum INT
@lionofdezert
lionofdezert / AutoTrace.sql
Created August 30, 2012 17:19
Create a trace through TSQL and save it to a file
/***************************************************
Purpose: To create a trace and saving it to a file
Created by: AASIM ABDULLAH
For http://connectsql.blogspot.com
Date: 08/29/2012
****************************************************/
-- Create a Queue
DECLARE @rc INT
DECLARE @TraceID INT
@lionofdezert
lionofdezert / StopAutoTrace.sql
Created August 30, 2012 17:22
Stop Auto Trace
DECLARE @TraceID INT
SELECT TOP 1 @TraceID = id FROM sys.traces
WHERE path LIKE '%_AutoTrace%'
exec sp_trace_setstatus @TraceID, 0 --Stop trace
exec sp_trace_setstatus @TraceID, 2 --Close trace
@lionofdezert
lionofdezert / DetectLongRunningProcesses.sql
Created September 5, 2012 13:49
To detect long running processes on SQL Server
/******************************************
Script By: Aasim Abdullah
For : http://connectsql.blogspot.com
Purpose: To detect long running sessions,
send complete information through mail about such sessions
and killing session, which are acceding given limit of execution time.
******************************************/
---BusyProcess Detection
SET NOCOUNT ON
@lionofdezert
lionofdezert / JobsChangeStatusAlert.sql
Created September 12, 2012 08:21
Trigger to get jobs change status alert
USE [msdb]
GO
/* ======================================================
Script By: Aasim Abdullah @http://connectsql.blogspot.com
Script For: Create trigger on msdb.dbo.sysjobs table,
to detect any change in job status by any
user and mail it to DB team
-- =================================================== */
CREATE TRIGGER [dbo].[JobStatusAlert]
ON [dbo].[sysjobs]