alpha

Lays down production-safety rules for writing T-SQL against SQL Server: dry runs, safe transactions, batched archiving, autocommit admin statements and read-only diagnostics.
description: 'SQL Server-specific write-safety rules for T-SQL: XACT_ABORT with THROW, @@TRANCOUNT-aware transactions, fail-closed dry runs, batched OUTPUT...INTO archiving, autocommit-only admin statements, and read-only DMV diagnostics.'
applyTo: '**/.sql, **/.ps1'
Apply these rules when writing or reviewing T-SQL, SQL Server Agent jobs, or PowerShell that runs against SQL Server. They complement general SQL style guidance by focusing on what keeps a script safe to run against production-adjacent systems.
Start every script with a comment block that states:
COMPAT: the minimum SQL Server version (for example SQL Server 2016 SP1+ for CREATE OR ALTER) and whether it works on Azure SQL Managed InstancePerms: the permissions required (for example VIEW SERVER STATE, or VIEW SERVER PERFORMANCE STATE on SQL Server 2022+)If you are not sure a syntax or DMV column exists in the target version, say so instead of guessing.
SET NOCOUNT ON; SET XACT_ABORT ON;.BEGIN TRY ... BEGIN TRANSACTION ... COMMIT ... END TRY BEGIN CATCH ... END CATCH.CREATE DATABASE, ALTER DATABASE, DROP DATABASE, BACKUP, RESTORE, and RECONFIGURE). For these, use an autocommit path instead of the transaction wrapper: keep the same dry-run default and confirmation gate, run each statement on its own inside TRY ... CATCH with THROW;, check state before and after, and write the operation-specific recovery step (for example the backup to restore from, or the previous configuration value to set back).CATCH block, roll back only if a transaction is open, then THROW; so the original error number and line reach the caller and Agent job history. Do not swallow errors.@@TRANCOUNT on entry, before any read, and only begin and commit a transaction when it is 0. If it is 0 and IMPLICIT_TRANSACTIONS is ON (@@OPTIONS & 2), reject the call before BEGIN TRANSACTION, because BEGIN TRANSACTION then opens a second level and the single COMMIT leaves the write uncommitted with its locks held (for example IF @@TRANCOUNT = 0 AND (@@OPTIONS & 2) = 2 THROW 50000, N'Run with IMPLICIT_TRANSACTIONS OFF; this procedure owns its transaction.', 1;). Procedures that commit in batches must refuse to run inside a caller-owned transaction or with IMPLICIT_TRANSACTIONS ON (@@OPTIONS & 2), because either one keeps every batch's locks and log until an outer commit and lets a later rollback undo earlier batches.@DryRun bit = 1 parameter and treat anything other than an explicit 0, including NULL, as a dry run (IF @DryRun IS NULL OR @DryRun <> 0). The parameter default only covers omitted arguments, so never test IF @DryRun = 1 alone. The dry run prints or returns what would change (for example a row count) and changes nothing.SupportsShouldProcess and -WhatIf alone do not make dry run the default. Require an explicit -Apply switch that is off by default, and wrap every mutation in if ($Apply -and $PSCmdlet.ShouldProcess($target, $action)) { ... }. Without -Apply, report what would change and exit.CREATE OR ALTER, IF OBJECT_ID(...) IS NULL, IF NOT EXISTS. Never silently overwrite an existing Agent job, linked server, alert, login, or user; detect it and stop or require an explicit flag.UPDATE and DELETE has a selective WHERE clause. Batch large changes with a TOP (N) loop, one short transaction per batch, and an accumulated row count.OUTPUT ... INTO, and delete only the rows proven to be copied in that batch.CREATE OR ALTER PROCEDURE dbo.usp_ArchiveOrders
@OlderThanDays int,
@BatchSize int = 1000,
@DryRun bit = 1,
@RowsAffected int = NULL OUTPUT
AS
BEGIN
SET NOCOUNT ON;
SET XACT_ABORT ON;
IF @OlderThanDays IS NULL OR @OlderThanDays < 1
BEGIN
RAISERROR(N'@OlderThanDays must be >= 1.', 16, 1);
RETURN 1;
END;
IF @BatchSize IS NULL OR @BatchSize NOT BETWEEN 1 AND 50000
BEGIN
RAISERROR(N'@BatchSize must be between 1 and 50000.', 16, 1);
RETURN 2;
END;
-- Batches commit independently, so a caller-owned transaction or
-- IMPLICIT_TRANSACTIONS ON is not allowed.
IF @@TRANCOUNT > 0 OR (@@OPTIONS & 2) = 2
BEGIN
RAISERROR(N'Run outside a caller transaction with IMPLICIT_TRANSACTIONS OFF; this procedure commits per batch.', 16, 1);
RETURN 3;
END;
DECLARE @Cutoff datetime2(0) = DATEADD(DAY, -@OlderThanDays, SYSDATETIME());
SET @RowsAffected = 0;
-- Fail closed: anything other than an explicit 0, including NULL, is a dry run.
IF @DryRun IS NULL OR @DryRun <> 0
BEGIN
SELECT @RowsAffected = COUNT(*) FROM dbo.Orders WHERE Status = 'Closed' AND CreatedAt < @Cutoff;
PRINT CONCAT(N'DRY RUN: would archive ', @RowsAffected, N' rows in batches of ', @BatchSize, N'.');
RETURN 0;
END;
DECLARE @moved TABLE (OrderId int PRIMARY KEY);
DECLARE @BatchRows int = 1;
BEGIN TRY
WHILE @BatchRows > 0
BEGIN
DELETE FROM @moved; -- fresh key set for every batch
BEGIN TRANSACTION;
INSERT dbo.OrdersArchive (OrderId, Status, CreatedAt)
OUTPUT inserted.OrderId INTO @moved (OrderId)
SELECT TOP (@BatchSize) o.OrderId, o.Status, o.CreatedAt
FROM dbo.Orders o WITH (UPDLOCK)
WHERE o.Status = 'Closed' AND o.CreatedAt < @Cutoff
ORDER BY o.OrderId;
DELETE o FROM dbo.Orders o JOIN @moved m ON m.OrderId = o.OrderId;
SET @BatchRows = @@ROWCOUNT;
COMMIT TRANSACTION;
SET @RowsAffected += @BatchRows;
END;
RETURN 0;
END TRY
BEGIN CATCH
IF @@TRANCOUNT > 0 ROLLBACK TRANSACTION; -- rolls back only the failing batch
THROW;
END CATCH;
END;
The default @BatchSize is 1000; tune larger values, because a batch that takes thousands of locks can escalate to a table lock that blocks other sessions.
On failure, earlier batches stay committed and the error is rethrown. Because the error is rethrown, SQL Server does not copy @RowsAffected back to the caller, so treat it as reliable only on successful completion; to see partial progress, count the archive table or log each batch before rethrowing. A rerun picks up the remaining qualifying rows.
Stop and ask for explicit confirmation, stating the data-loss or availability impact, before producing any of:
DROP, TRUNCATE, ALTER TABLE ... DROP COLUMN, or column type narrowingUPDATE or DELETE without a selective predicateKILL, or DBCC repair optionsxp_cmdshell, Ole Automation, or CLRmax server memory)Before any data-loss operation, state what should be backed up, where, and how to restore it. Include rollback steps for every change.
READ-ONLY in the header.sys.dm_exec_sessions, sys.dm_exec_requests, and sys.dm_exec_sql_text. Do not issue KILL; if the user decides to, tell them to record the session id and reason, and that KILL rolls back the open transaction.sys.dm_os_wait_stats and report the delta, excluding benign idle waits, rather than reporting cumulative values since startup.type = 'S' and authentication_type = 1 in sys.database_principals) whose SID has no matching row in sys.server_principals. Exclude system principals (dbo, guest, INFORMATION_SCHEMA, sys). Users created WITHOUT LOGIN, contained database users, and Windows users intentionally have no matching SQL login and are not orphans.ALTER USER ... WITH LOGIN = ..., never creates logins, and never drops users.© GitHub, Inc. · MIT · source · posted by @OpenSourceBuffGuy on promptparty.com


Reverse-engineers a website section by section, writes component specs, and hands them to parallel builder agents in git worktrees to rebuild it.
agent-skill, orchestrator, parallel-agents, multi-agent, plan-then-execute, web-development, frontend, codebase-analysis, scaffolding, css, html, nextjs, typescript, git, mcp, claude-code, for-developers, for-designers, long-form, advanced, technical-spec, task-decomposition, structured-output, design-systems, ui-ux, tool-use, checklist, report, vibe-coding

Italian Terrace Cafe Candid Portrait
Generates a photorealistic candid shot of a woman in a white corset dress laughing at a sunny hotel terrace cafe, written as detailed JSON.
image-prompt, json-prompt, structured-output, photorealistic, portrait, woman, candid, fashion, cottagecore, natural-light, golden-hour, bokeh, summer, travel, cityscape, mountains, medium-shot, vibrant-colors, for-photographers, nano-banana, fashion-shoot, luxury

Night Street Photo With Leashed Cat
Generates a flash-lit night street photo of a blonde woman in a retro track jacket walking a hissing tabby cat on a leash.
image-prompt, json-prompt, nano-banana, structured-output, portrait, woman, animals, pet-portrait, night-scene, street-photography, candid, editorial, fashion, dramatic-lighting, photorealistic, vertical-format, medium-shot, graffiti, for-photographers, for-content-creators
Loading reviews…
Loading comments…