Skip to content

Instantly share code, notes, and snippets.

View DanielLoth's full-sized avatar

Daniel Loth DanielLoth

View GitHub Profile
@DanielLoth
DanielLoth / script.sql
Created November 21, 2023 23:10
RiskMan
use RiskMan;
go
set nocount, xact_abort on;
go
drop view if exists
Risk.MyDatabaseRisks_V,
Risk.AllRisks_V,
Risk.AllDatabaseRisks_V;
go
@DanielLoth
DanielLoth / script.sql
Created June 23, 2023 10:46
Reddit answer
use tempdb;
go
set nocount, xact_abort on;
go
drop table if exists Attendance, ClassDate, Person, AttendanceType;
go
drop table if exists Person;
create table Person (
@DanielLoth
DanielLoth / script.sql
Created March 26, 2022 12:35
SG Hierarchy Document question
-- This script can be ran repeatedly on an instance of Microsoft SQL Server.
-- Objects are created in tempdb.
----------------
-- Background --
----------------
-- The document titled 'SG Hierarchy Document' contains the following excerpt:
--
-- Common Table Expression
@DanielLoth
DanielLoth / cycles.sql
Last active February 15, 2022 11:15
Cycle detection in SQL Server schema
-- https://azure.microsoft.com/en-us/blog/finding-circular-foreign-key-references/
use tempdb;
--alter table A drop constraint FK1;
drop table if exists D;
drop table if exists C;
drop table if exists B;
drop table if exists A;
@DanielLoth
DanielLoth / DataFiller.sql
Last active February 1, 2022 11:31
A script that inserts exactly one row in all tables in database
--------------------------------------------------------
-- Insert one row into all tables within the database --
--------------------------------------------------------
-- Works On My Machine (TM) when tested with the AdventureWorks 2019 backup
use AdventureWorks2019;
set nocount on;
go
@DanielLoth
DanielLoth / Messages.sql
Last active August 12, 2021 08:50
Attendance_Add_tr with lost update protection
--------------------------------------------------
-- Transaction misuse messages
--------------------------------------------------
exec sp_addmessage
@msgnum = 100001,
@msgtext = N'%s: is a transaction, which is Atomic. It has been called from within an open transaction, which would render it a non-transaction. That is not allowed.',
@severity = 16,
@lang = 'us_english',
@with_log = 'false',
@replace = 'replace';
create procedure dbo.Attendance_Add_tr
@OrganisationId int,
@PersonId int,
@AttendanceDate date
as
-- MSSQL equivalent of disabling chained transaction
set implicit_transactions off;
set nocount on;
create procedure dbo.Attendance_Add_tr
@OrganisationId int,
@PersonId int,
@AttendanceDate date
as
set nocount on;
------------------------------------------------------------
-- Validation block
@DanielLoth
DanielLoth / gist:6a8777dd978b8d00dbe5d6fa880fed59
Created June 25, 2021 13:33
comp.theory.database 2021-06-25
create procedure dbo.AddAttendance_tr
@OrganisationId int,
@PersonId int,
@AttendanceDate date
as
set nocount on;
------------------------------------------------------------
-- Validation block
@DanielLoth
DanielLoth / git commands
Created March 19, 2021 05:06
Git stuff
# Assumption: Your remotes are configured as 'origin' (for personal fork) and 'upstream' (for shared git repo).
# First, make sure your local workspace is clean
git status
# If it isn't, either create a WIP branch OR stash your changes
# WIP branch:
git checkout -b my-current-WIP-19-mar-2021
git add *
git commit -m 'WIP'