Skip to content

Instantly share code, notes, and snippets.

View petervandivier's full-sized avatar
🌶️
Why is it spicy?

Peter Vandivier petervandivier

🌶️
Why is it spicy?
View GitHub Profile
@petervandivier
petervandivier / dude_wheres_my_trigger.sql
Created March 13, 2018 10:29
Update multiple base tables from CRUD operation on a view
create table dude (
id int identity not null primary key,
name sysname unique
);
go
create table car (
id int identity not null primary key,
name sysname,
dude_id int not null foreign key references dbo.dude(id)
);
@petervandivier
petervandivier / drop-filegroup-funkiness.sql
Last active March 13, 2018 13:57
Object Definition bound to FG. Data pages bound to file.
use [master]
go
create database FileDrop;
go
use FileDrop
go
alter authorization on database::FileDrop to sa;
go
alter database FileDrop
add filegroup foo;
@petervandivier
petervandivier / LR_PartitionSummary.sql
Last active March 15, 2018 12:00
Which partition does this row live in?
use FunWithPartitions
go
-- select * from LR_PartitionSummary;
create or alter view LR_PartitionSummary
as
-- http://sqlity.net/en/2483/partition-boundaries/
select
file_group_name = f.[name],
table_schema = schema_name(t.[schema_id]),
@petervandivier
petervandivier / object-param.ps1
Created April 13, 2018 08:47
Fun With PS - Object Parameters
$foo = @{
bar = 1
blah = 2
}
function baz {
[cmdletbinding()]param(
$foo
,$which
)
@petervandivier
petervandivier / derived-aggregate.sql
Created April 16, 2018 12:27
Sum of sums & ansi_nulls
select *
from (
select 1 i, cast(null as int) j union all
select null, 1
) x
select sum(i) i_sum, sum(j) j_sum
from (
select 1 i, cast(null as int) j union all
select null, 1
@petervandivier
petervandivier / imo-demo-polling.sql
Last active April 26, 2018 15:55
IMO Requirements Estimate Discrepancies
select pool_id
,[name]
,min_memory_percent
,max_memory_percent
,max_memory_gb = max_memory_kb / power(1024,2)
,max_memory_mb = max_memory_kb / 1024
,used_memory_mb = used_memory_kb / 1024
,target_memory_mb = target_memory_kb / 1024
@petervandivier
petervandivier / partition-next-used-fg.sql
Last active May 30, 2018 12:24
Partition - Next Used FG
use [master]
go
drop database if exists elephants;
create database elephants;
alter authorization on database::elephants to sa;
alter database elephants add filegroup dumbo;
alter database elephants add file (name=dumbo, filename='c:\temp\dumbo.ndf') to filegroup dumbo;
go
use elephants
go
@petervandivier
petervandivier / fun-with-hashtables.ps1
Created July 4, 2018 12:50
yo dawg, I heard you like collections
$myObj = @"
foo,bar
foo,bar
"@ | ConvertFrom-Csv
$nodes = @"
name,value1,value2
a,x,0
b,y,1
c,z,2
@petervandivier
petervandivier / parameterised-insert.ps1
Last active August 17, 2018 12:00
Powershell - return datarows from executed ad-hoc parameterised sql
# https://stackoverflow.com/a/16735220/4709762
$conn=new-object data.sqlclient.sqlconnection "Server=localhost;Initial Catalog=foo;Integrated Security=True"
$sql=@"
insert Deadlocks (fname, dl)
select @filename, @text;
"@
$conn.open()
@petervandivier
petervandivier / move-tempDB.ps1
Created September 12, 2018 09:03
Move TempDB
#Requires -RunAsAdministrator
$get_commands_query = @"
declare @fmt nvarchar(1000) = '
ALTER DATABASE tempdb MODIFY FILE (
NAME = [%s]
,FILENAME = ''%s''
);
GO';