Skip to content

Instantly share code, notes, and snippets.

View jujiro's full-sized avatar

Ashish Kumar jujiro

View GitHub Profile
@jujiro
jujiro / run-single-instance-stored-proc.sql
Created May 20, 2019 17:33
Running only one instance of Sql Server stored procedure
if not exists (select * from dbo.sysobjects
where id = object_id(N'dbo.p_AcquireLock') and
OBJECTPROPERTY(id, N'IsProcedure') = 1)
exec('create procedure dbo.p_AcquireLock as select 1')
go
alter procedure dbo.p_AcquireLock
(
@LockName nvarchar(255)
)
@jujiro
jujiro / initialize-column-in-large-table.sql
Created May 20, 2019 17:29
Initializing a newly added column in a very large table
-- Adding a new column, which you want to initialize with a default value (often true with bit columns)
-- can be tricky. Our gut feeling is to write a quick update statement, or use a construct like the following.
ALTER TABLE dbo.a_very_large_table
ADD test INT NOT NULL DEFAULT(0)
GO
/*
Both these methods should be avoided at all cost. They create a very large transaction, creating a situation of blocking or getting blocked. If blocked, rollback process can take hours.
The following script proposes two methods.
First one uses a controlled update using row count and the second one uses a cursor.
@jujiro
jujiro / find-identity-columns.sql
Created May 20, 2019 17:18
Find identity columns (Sql server)
select
i.table_catalog,
i.table_schema,
i.table_name,
c.name as column_name
from
(
sys.objects o inner join
sys.columns c
on
@jujiro
jujiro / pass-xml-instad-of-table-to-c-sharp.sql
Last active May 20, 2019 17:12
XML is easier to pass to a sql stored procedure than a table in .Net. Refactor your proc and you will save a ton of code in .Net.
-- There are two examples on this page
-- 1st Example
begin
DECLARE @idoc INT
DECLARE @xml nvarchar(MAX)
SET @xml='<root>
<id>
19
</id>
<prod>
@jujiro
jujiro / sql-to-clr-type-basic.cs
Created May 20, 2019 16:58
Sql data type mappings to some basic clr types
public class TypeMapping
{
public Dictionary<string, string> SqlToCSharpMappings = new Dictionary<string, string>()
{
{"BINARY","byte[]"},
{"BIGINT","long?"},
{"BIT","bool?"},
{"CHAR","string"},
{"DATETIME2","DateTime?"},
{"DECIMAL","decimal?"},
@jujiro
jujiro / sql-to-c-sharp.cs
Last active May 20, 2019 16:55
Sql to C# data type conversion code snippet
public static Type GetClrType(SqlDbType sqlType)
{
switch (sqlType)
{
case SqlDbType.BigInt:
return typeof(long?);
case SqlDbType.Binary:
case SqlDbType.Image:
case SqlDbType.Timestamp:
@jujiro
jujiro / find-default-constraint-names.sql
Created May 20, 2019 16:49
Find default constraint names for tables (Sql Server)
/*
Sql server will not let you dropa table if it has default contraints (like default values for columns.)
This script will let you find them.
1. Determine the constraint name.
*/
if object_id('[dbo].[GetDefaultConstraintName]') is not null
drop function [dbo].[GetDefaultConstraintName]
go
@jujiro
jujiro / find-table-dependencies.sql
Created May 20, 2019 16:37
Find table dependencies (Sql Server)
begin
-- Use this script to find the dependencies of tables.
-- Set the following two variables.
-- ####################################################
declare @TableSchema varchar(255)='Production'
declare @TableName varchar(255)='Product'
-- ####################################################
declare @nCnt1 int