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 / 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 / DDLChangeLogServiceBroker..sql
Last active July 6, 2017 21:18
To create DDL Change Log using Service Broker, for multiple databases on a single instance
/*
Script By: Aasim Abdullah/Amna Asif for ConnectSQL
Purpose: To create DDL Change Log using Service Broker,
for multiple databases on a single instance
*/
IF NOT EXISTS (SELECT 1 FROM sys.databases WHERE name = 'ConnectDBA')
CREATE DATABASE ConnectDBA
GO
USE [ConnectDBA]
GO
@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 / SYNC_CreatePrimaryKeys.sql
Created November 29, 2013 18:48
Stored Procedure, to create missing primary keys at target database by comparing both source and target databases.
--DROP IF ALREADY CREATED
IF EXISTS ( SELECT 1
FROM SYS.procedures
WHERE NAME = 'SYNC_CreatePrimaryKeys' )
DROP PROC SYNC_CreatePrimaryKeys
GO
--CREATE PROCEDURE
SET ANSI_NULLS ON
GO
@lionofdezert
lionofdezert / SYNC_Indexes.sql
Created November 29, 2013 18:50
Stored Procedure, to create missing indexes on tables values at target database by comparing both source and target databases.
--DROP IF ALREADY EXISTS
IF EXISTS ( SELECT 1
FROM SYS.PROCEDURES
WHERE NAME = 'SYNC_Indexes' )
DROP PROCEDURE SYNC_Indexes
GO
--CREATE PROCEDURE
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
@lionofdezert
lionofdezert / SYNC_CreateMissingColumns.sql
Created November 29, 2013 18:46
Stored Procedure, to create missing columns at target database by comparing both source and target databases.
--DROP IF ALREADY CREATED
IF EXISTS (SELECT 1 FROM sys.objects WHERE type = 'P' AND name = 'SYNC_CreateMissingColumns')
DROP PROC [dbo].[SYNC_CreateMissingColumns]
GO
--CREATE PROCEDURE
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
/*
@lionofdezert
lionofdezert / SYNC_ColumnsDefaultValues.sql
Created November 29, 2013 18:40
Stored Procedure, to create missing columns default values at target database by comparing both source and target databases.
--DROP IF ALREADY EXISTS
IF EXISTS ( SELECT 1
FROM SYS.procedures
WHERE NAME = 'SYNC_ColumnsDefaultValues' )
DROP PROC SYNC_ColumnsDefaultValues
GO
--CREATE PROCEDURE
SET ANSI_NULLS ON
GO