Skip to content

Instantly share code, notes, and snippets.

View sqldeployhelmet's full-sized avatar

josh sqldeployhelmet

View GitHub Profile
@sqldeployhelmet
sqldeployhelmet / pivot_contactsNoPivot.sql
Last active October 4, 2018 16:37
A simple un-pivoted query
/* A list of contacts */
SELECT a.acctNo, c.name, c.email
FROM contacts c JOIN accounts a ON a.acctNo = c.acctNo
ORDER BY a.acctNo, c.contactNo
/* A pivoted list of contacts */
WITH contactsCTE AS (SELECT a.acctNo
, c.name
, c.email
, ROW_NUMBER() OVER (PARTITION BY a.acctNo ORDER BY c.ContactID) AS ConNumber
FROM contacts c JOIN accounts a ON a.acctNo = c.acctNo)
SELECT acctNo
, MIN(name1) AS Name1
, MIN(email1) AS Email1
, MIN(name2) AS Name2
@sqldeployhelmet
sqldeployhelmet / update_partition_readonly_FG
Created October 4, 2018 17:41
a general data warehouse update command
UPDATE DW_DBNAME.dbo.DW_TABLE
SET ETL_JOB_SEQ_NUM = 1027140 ,
ETL_IUD_CD = 'D' ,
ETL_UPD_DTM = CONVERT(DATETIME, '03/16/2016 01:01:17')
WHERE ETL_VERS_START_DTM = CONVERT(DATETIME, '12/27/2014 00:26:24.137')
AND ACCT_NO = '5001194157'
AND ACCT_TYPE = 'ILN';
EXEC master.dbo.xp_backup_database @database = '<database name>',
@filename = 'Y:\SQL_BACKUPS\<name of backup file>', @filegroup = '<filegroup to backup>'
, @compressionlevel = 4, @cryptlevel = 8, @encryptionkey = N'IMMAENCRYPTCHYU'
, @verify = 1, @returndetails = 1, @init = 1, @with = 'CHECKSUM';
EXEC master.dbo.xp_backup_database @database = '<database name>', @filename = 'Y:\SQL_BACKUPS\<name of backup file>'
, @compressionlevel = 4, @cryptlevel = 8, @encryptionkey = N'IMMAENCRYPTCHYU'
, @read_write_filegroups = 1, @verify = 1, @returndetails = 1, @init = 1, @with = 'CHECKSUM';
EXEC master.dbo.xp_backup_database @database = '', @filename = 'Y:\SQL_BACKUPS\'
, @compressionlevel = 4, @cryptlevel = 8, @encryptionkey = N'IMMAENCRYPTCHYU'
, @read_write_filegroups = 1, @verify = 1, @returndetails = 1
, @init = 1, @with = 'CHECKSUM', @with = 'DIFFERENTIAL';
EXEC master.dbo.xp_restore_database @database = '', @filename = ''
, @encryptionkey = N'IMMAENCRYPTCHYU', @read_write_filegroups = 1
, @with = 'NORECOVERY', @returndetails = 1;
EXEC master.dbo.xp_restore_database @database = '', @filename = ''
, @encryptionkey = N'IMMAENCRYPTCHYU', @read_write_filegroups = 1
, @with = 'RECOVERY', @returndetails = 1;
EXEC master.dbo.xp_restore_database @database = '', @filename = ''
, @filegroup = '', @encryptionkey = N'IMMAENCRYPTCHYU', @with = 'RECOVERY'
, @with = 'MOVE '''' TO ''''', @returndetails = 1
/* returns all partitioned tables: */
SELECT OBJECT_NAME(p.[object_id]) AS 'TableName'
, i.[name] AS 'IndexName'
, i.index_id
, p.[object_id]
, au.data_space_id
, p.partition_number
, p.hobt_id
, partitionds.name AS partition_filegroup
INTO #partDist