Skip to content

Instantly share code, notes, and snippets.

View sqldeployhelmet's full-sized avatar

josh sqldeployhelmet

View GitHub Profile
/* What movies have been nominated for 1, 2 or 3 Oscars? */
SELECT name, releaseDate, oscarNoms
FROM films
/* if any one of the following conditions is true for a row it will be returned */
WHERE oscarNoms = 1
OR oscarNoms = 2
OR oscarNoms = 3;
/* These can more easily be written with the IN clause */
SELECT name releasedDate
@sqldeployhelmet
sqldeployhelmet / sqlquerymathfilters.sql
Last active April 7, 2019 22:19
Using simple mathematical operators for filters in SQL
/* films with exactly 2 oscar nominations */
SELECT name, releaseDate
FROM films
WHERE oscarNoms = 2;
/* films with more than 4 oscar nominations*/
SELECT name, releaseDate
FROM films
WHERE oscarNoms > 4;
@sqldeployhelmet
sqldeployhelmet / sqlquery1.sql
Last active April 7, 2019 22:20
The simplest of queries
SELECT 'MyData';
/*
Best practices of course would be to include the specific columns
in your select list so the SQL query engine can optimize the return
of the necessary data. Selecting * from the table is a lot like
eating an entire pizza because you wanted a single slice.
*/
SELECT *
FROM myTable;
@sqldeployhelmet
sqldeployhelmet / SSRSConfigObjs.ps1
Last active January 9, 2019 23:10
PS commands to get SSRS details
$servername = 'myserver'
$key = "Software\\Microsoft\\Microsoft SQL Server"
$reg = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('LocalMachine', $server)
$regKey = $reg.OpenSubKey($key)
$keys = $regKey.GetSubKeyNames()
$v = 0
/*
Script to remove empty partitions older than 15 months (except partition 1 because we need at least one empty partition)
*/
DECLARE @mergedate DATETIME2
, @sqlcmd VARCHAR(MAX);
CREATE TABLE #mergedates ( m_date DATETIME2 );
INSERT INTO #mergedates ( m_date )
SELECT CAST(sprv.value AS DATETIME2)
/*
Script to identify the oldest partition w/ data and swap it out
</code><code>
1. identify partition
2. script out target table (1 time?)
3. ALTER TABLE audit.ServiceUserAction SWITCH PARTITION # TO
3. truncate table
*/
DECLARE @part_num INT
/* Split upcoming partitions (work up to 3 months ahead of current month) */
DECLARE @nextPart DATE
, @curDate DATE = DATEADD(MONTH, 3, GETDATE()); -- we'll extend our partitions out to this date</code>
SELECT @nextPart = DATEADD(MONTH, 1, CAST(sprv.value AS DATETIME2))
FROM sys.partition_functions AS spf
INNER JOIN sys.partition_range_values sprv ON sprv.function_id = spf.function_id
WHERE spf.name = N'pf_MonthlyWindow'
ORDER BY sprv.boundary_id DESC
OFFSET 1 ROWS FETCH NEXT 1 ROW ONLY;
ALTER TABLE dbo.superbusytable DROP CONSTRAINT PK_BestKeyEver
GO
ALTER TABLE dbo.superbusytable
ADD CONSTRAINT PK_BestKeyEver
PRIMARY KEY CLUSTERED (id, insertdate ) ON ps_MonthlyWindow(insertdate); -- insertdate is an ever increasing date, we'll never be inserting "old" records
GO
USE [VeryBusyDatabase];
GO
CREATE PARTITION FUNCTION [pf_MonthlyWindow] ( DATETIME2(7)) -- This data type must match your partitioning column data type exactly!
AS RANGE RIGHT FOR VALUES ( N'1900-01-01T00:00:00.000'
, N'2016-08-01T00:00:00.000'
, N'2016-09-01T00:00:00.000'
, N'2016-10-01T00:00:00.000'
, N'2016-11-01T00:00:00.000'
, N'2016-12-01T00:00:00.000'
EXEC sp_estimate_data_compression_savings ''
, ''
, NULL
, NULL
, '';