Code Snippets TSQL

Short texts TSQL 🧑‍💻 Code snippets TSQL ✍️ Reading lists TSQL 👀

HrDbSchema_v1.sql

IF OBJECT_ID(N'[__EFMigrationsHistory]’) IS NULL BEGIN CREATE TABLE [__EFMigrationsHistory] ( [MigrationId] nvarchar(150) NOT NULL, [ProductVersion] nvarchar(32) NOT NULL, CONSTRAINT [PK___EFMigrationsHistory] PRIMARY KEY ([MigrationId]) ); END; GO BEGIN TRANSACTION; GO CREATE TABLE [Employees] ( [Id] int NOT NULL IDENTITY, [FirstName] nvarchar(max) NULL, [LastName] nvarchar(max) NULL, [Email] nvarchar(max) NULL, [Phonenumber] nvarchar(max) NULL, [Street] nvarchar(max) NULL, [City] nvarchar(max) NULL, […]

create schema

CREATE TABLE [meta].[RequiredColumn]( [SchemaName] [varchar](16) NOT NULL, [TableName] [varchar](24) NOT NULL, [ColumnName] [varchar](32) NOT NULL ) ON [PRIMARY] GO

silme.sql

— the following is an example to show what code can be generated to do the table deletionset nocount on; if object_id(‘tempdb..#tmp’) is not null drop table #tmp; create table #tmp (id int, tablename varchar(256), lvl int, ParentTable varchar(256)); insert into #tmp exec dbo.usp_SearchFK @table=’dbo.Moduls’, @debug=0; declare @where varchar(max) =’where Moduls.id=2′ — if @where clause […]

usp_SearchFK.sql

— the following is an example to show what code can be generated to do the table deletionset nocount on; if object_id(‘tempdb..#tmp’) is not null drop table #tmp; create table #tmp (id int, tablename varchar(256), lvl int, ParentTable varchar(256)); insert into #tmp exec dbo.usp_SearchFK @table=’dbo.Moduls’, @debug=0; declare @where varchar(max) =’where Moduls.id=2′ — if @where clause […]

SQL Server clone table: clone columns, contraints, default values

— Stored procedure clone table structure CREATE PROCEDURE [spCloneTableStructure] @SourceSchema nvarchar(255), @SourceTable nvarchar(255), @DestinationSchema nvarchar(255), @DestinationTable nvarchar(255), @RecreateIfExists bit = 0 AS BEGIN /* Clones an existing table to another table (without data) Optionally drops and re-creates target table Copies: * Structure * Primary key * Indexes (including ASC/DESC, included columns, filters) * Constraints (and […]