Skip to content

Instantly share code, notes, and snippets.

View kmoormann's full-sized avatar

Kevin Moormann kmoormann

View GitHub Profile
@kmoormann
kmoormann / StartSQLAgentJob.sql
Created September 27, 2012 15:15
Start a SQL Agent Job
USE msdb ;
GO
EXEC dbo.sp_start_job N'The name of the job here';
GO
@kmoormann
kmoormann / CheckIfSQLAgentJobIsRunning
Created September 27, 2012 15:16
Check if a SQL Agent Job is Running
SET NOCOUNT ON
DECLARE @isRunning BIT
DECLARE @xp_results TABLE (job_id UNIQUEIDENTIFIER NOT NULL,
last_run_date INT NOT NULL,
last_run_time INT NOT NULL,
next_run_date INT NOT NULL,
next_run_time INT NOT NULL,
next_run_schedule_id INT NOT NULL,
requested_to_run INT NOT NULL, -- BOOL
request_source INT NOT NULL,
@kmoormann
kmoormann / CombineFiles.ps1
Created September 28, 2012 15:05
Powershell Combine Files Together
#http://www.brangle.com/wordpress/2009/08/combine-join-two-text-files-using-powershell/
@kmoormann
kmoormann / IsSSISJobRunning.sql
Created November 1, 2012 20:50
Check if SSIS Job is Running
DECLARE @JOB_NAME SYSNAME = N'Daily update of indexes & statistics';
IF NOT EXISTS(
select 1
from msdb.dbo.sysjobs_view job
inner join msdb.dbo.sysjobactivity activity on job.job_id = activity.job_id
where
activity.run_Requested_date is not null
and activity.stop_execution_date is null
and job.name = @JOB_NAME
@kmoormann
kmoormann / TSqlUserDefinedFunctionDefinitions.sql
Created January 17, 2013 16:27
user defined function definition for sql server
USE DER_CommProg;
GO
CREATE TABLE #functionInfo
(
ID BIGINT NOT NULL
,Name VARCHAR(200) NULL
,[SchemaName] VARCHAR(200) NULL
,[Description] VARCHAR(200) NULL
,[Definition] VARCHAR(MAX) NULL
/*============================================================================
Script to report Memory usage details of a SQL Server instance
Author: Sakthivel Chidambaram, Microsoft http://blogs.msdn.com/b/sqlsakthi
Date: June 2012
Version: V2
V1: Initial Release
V2: Added PLE, Memory grants pending, Checkpoint, Lazy write,Free list counters
@kmoormann
kmoormann / SQLServerProductInfo.sql
Created July 12, 2013 20:22
A simple query to determine the SQL Server version level and edition
SELECT SERVERPROPERTY('productversion'), SERVERPROPERTY ('productlevel'), SERVERPROPERTY ('edition')
@kmoormann
kmoormann / ForeignKeysForSQLDatabase.sql
Created July 26, 2013 15:45
Determine all foreign keys for a sql server database. Tested on SQL 2008 R2
USE <REPLACE WITH DATABASE NAME>
SELECT
foreignKeyConstraintObject.Name as ForeignKeyConstraint
,foreignKeySchema.Name as ForeignKeySchema
,foreignKeyTable.Name as ForeignKeyTable
,foreignKeyColumn.Name as FoeignKeyName
,referencedSchema.Name as ReferencedSchema
,referencedTable.Name as ReferencedTable
,referencedColumn.Name as ReferencedColumn
--USE <REPLACE WITH DATABASE NAME>
CREATE TABLE #tablesOfInterest (TableName VARCHAR(2000))
INSERT INTO #tablesOfInterest
SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_NAME
IN ('table1'
,'table2'
)
@kmoormann
kmoormann / RemoveNULLfromDatabaseOutputFile.fsx
Created September 6, 2013 20:10
An F# script that will remove the string "NULL" from a text delimited file. Often when saving a SQL query result to a file NULL values will be saved as the string "NULL". This removes them so that they are easier to process
open System.IO
let lines path =
System.IO.File.ReadAllLines(path)
let writeFile lines path =
System.IO.File.Delete(path)
System.IO.File.AppendAllLines(path, lines)
let replaceNulls path =
let origFileNameAndExtension = System.IO.Path.GetFileName(path)