Monday, January 11, 2016

How to check if column exists in SQL Server table

IF EXISTS(SELECT * FROM sys.columns 
WHERE Name = N'columnName' AND Object_ID = Object_ID(qualified.table.name')) 
BEGIN 
    PRINT 'Column Exists'
END
--OR
IF COL_LENGTH('qualified.table.name','column_name') IS NULL
BEGIN
    PRINT 'Column does not exist or caller does not have permission to view the object'
END

Tuesday, December 22, 2015

How do I reset a sequence in sql

ALTER SEQUENCE Schema.SequenceName
RESTART WITH 1

Identity jump issue in sql server


  1. Open "SQL Server Configuration Manager"
  2. Click "SQL Server Services" on the left pane 
  3. Right-click on your SQL Server instance name on the right pane ->Default: SQL Server(MSSQLSERVER)
  4. Click "Properties"
  5. Click "Startup Parameters"
  6. On the "specify a startup parameter" textbox type "-T272" 
  7. Click "Add" 
  8. Confirm the changes

Monday, November 16, 2015

Set the width of select2 input

$("select").select2({ width: 'resolve' });
OR
$("select").select2({ width: '100%' });
OR
$("select").select2({ dropdownAutoWidth : true });

Thursday, November 12, 2015

Determine Whether a Table Has an Identity Column

SQL Server 2005+

SELECT OBJECTPROPERTY(object_id('TableToBeChecked'), 'TableHasIdentity')

SMO

Server srv = new Server("(local)");
Database db = srv.Databases["AdventureWorks2012"];

Table tb = new Table(db, "Test Table");
Column col1 = new Column(tb, "TableIdentifier", DataType.Int);
col1.Identity = true;
tb.Columns.Add(col1); 
try
{
foreach (String s in tb.CheckIdentityValue())
{
   Console.WriteLine(s);
}
}
catch
{
}