Skip to content

Instantly share code, notes, and snippets.

@SomTripathi
SomTripathi / sync_all_orphans.sql
Created May 19, 2015 11:05
Sync all orphans in a database
--+---------------------------------------------------------------------------------------------------------------+--
--| Sync All Orphans in a Database
--+---------------------------------------------------------------------------------------------------------------+--
select 'sp_change_users_login ''auto_fix'',''' + name + '''' + char(13) + char(10) + 'GO'
from sysusers
where sid NOT IN
(select sid from master..syslogins )
AND
islogin = 1
@SomTripathi
SomTripathi / sync_orphans.sql
Created May 19, 2015 11:00
Sync an orphan user in all databases
--+---------------------------------------------------------------------------------------------------------------+--
--| Sync an Orphan in all Databases
--+---------------------------------------------------------------------------------------------------------------+--
SET NOCOUNT ON
exec sp_MSforeachdb
'
use ?
select db_name()
exec sp_change_users_login ''update_one'' , ''<login_name>'' , ''<login_name>''
@SomTripathi
SomTripathi / get_job_history.sql
Created May 19, 2015 10:54
Get Job History of a Job
--+---------------------------------------------------------------------------------------------------------------+--
--| Get a Job-Hostory
--+---------------------------------------------------------------------------------------------------------------+--
--| Date : 11:38 PM 9/10/2009
--| Purpose : Get a job history. Place the Job-name and get results soon. In some cases when you have a huge
--| historical info, SSMS-Job history fails due to time-out - this batch could help you.
--| Compatible : SQL Server 2000, SQL Server 2005
--+---------------------------------------------------------------------------------------------------------------+--
--| Author : Som Dutt Tripathi
--+---------------------------------------------------------------------------------------------------------------+--
@SomTripathi
SomTripathi / table_report.sql
Created May 19, 2015 10:49
Get Table Report
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[psp_table_size_report]
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[psp_table_size_report]
@SomTripathi
SomTripathi / fragmentation_report.sql
Created May 19, 2015 10:45
Fragmentation Report
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[psp_fragmentation_report]
(
@recipients varchar(300),
@subject varchar(800)='Fragmentation Report'
@SomTripathi
SomTripathi / disk_size.sql
Last active August 29, 2015 14:21
Disk Size Report
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[psp_disk_size_report]
(
@threshold int=20,
@recipients varchar(300),
@SomTripathi
SomTripathi / db_size.sql
Last active August 29, 2015 14:21
Database Size Report
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[psp_db_size_report]
(
@threshold int=20,
@recipients varchar(300),
@subject varchar(800)='Database Size Report'
@SomTripathi
SomTripathi / jobs_scheduled_next.sql
Created May 19, 2015 10:20
Get Jobs Schedeule in next 2 hours
DECLARE @HowManyHoursAhead int
Set @HowManyHoursAhead = 2; -- <-- Change the hour here.
WITH OurJobs AS (
SELECT job.job_id, job.[name]
, CASE job.[description] WHEN 'No description available.' THEN NULL ELSE job.description END AS Description
, job.date_modified
, CASE sched.next_run_date
WHEN 0 THEN 'Never'
@SomTripathi
SomTripathi / setPrecision.ps1
Last active August 29, 2015 14:21
Function to set Precision
cls
function setPrecision([Float]$number, [int]$precision )
{
$prec="{0:N$precision}"
$val=$($prec -f $number)
return $val
}