Check if database exists or not in server

Little script which checks database exists in server or not

DECLARE @dbname nvarchar(128)
SET @dbname = N’Databasename’

FROM master.dbo.sysdatabases
WHERE (‘[‘ + name + ‘]’ = @dbname
OR name = @dbname)))

Posted in SQL Server

Reset Password from membership aspnet_users

first get User Name, application Name, PasswordFormat and Salt from apsnet database.

au.username, aa.ApplicationName, password, passwordformat, passwordsalt
aspnet_membership am
aspnet_users au ON (au.userid = am.userid)
aspnet_applications aa ON (au.applicationId = aa.applicationid)
au.UserName = ‘jignesh.patel’

Use following script to reset password.

SET @changeDate = GETDATE()

EXEC aspnet_Membership_setPassword

Posted in SQL Server

Create SQL script for Tables in Database

select  'create table [' + + '] (' + o.list + ')' + CASE WHEN tc.Constraint_Name IS NULL THEN '' ELSE 'ALTER TABLE ' + so.Name + ' ADD CONSTRAINT ' + tc.Constraint_Name  + ' PRIMARY KEY ' + ' (' + LEFT(j.List, Len(j.List)-1) + ')' END
from    sysobjects so
cross apply
        '  ['+column_name+'] ' + 
        data_type + case data_type
            when 'sql_variant' then ''
            when 'text' then ''
            when 'ntext' then ''
            when 'xml' then ''
            when 'decimal' then '(' + cast(numeric_precision as varchar) + ', ' + cast(numeric_scale as varchar) + ')'
            else coalesce('('+case when character_maximum_length = -1 then 'MAX' else cast(character_maximum_length as varchar) end +')','') end + ' ' +
        case when exists ( 
        select id from syscolumns
        where object_name(id)
        and name=column_name
        and columnproperty(id,name,'IsIdentity') = 1 
        ) then
        'IDENTITY(' + 
        cast(ident_seed( as varchar) + ',' + 
        cast(ident_incr( as varchar) + ')'
        else ''
        end + ' ' +
         (case when IS_NULLABLE = 'No' then 'NOT ' else '' end ) + 'NULL ' + 
          case when information_schema.columns.COLUMN_DEFAULT IS NOT NULL THEN 'DEFAULT '+ information_schema.columns.COLUMN_DEFAULT ELSE '' END + ', ' 

     from information_schema.columns where table_name =
     order by ordinal_position
    FOR XML PATH('')) o (list)
left join
    information_schema.table_constraints tc
on  tc.Table_name       = so.Name
AND tc.Constraint_Type  = 'PRIMARY KEY'
cross apply
    (select '[' + Column_Name + '], '
     FROM   information_schema.key_column_usage kcu
     WHERE  kcu.Constraint_Name = tc.Constraint_Name
     FOR XML PATH('')) j (list)
where   xtype = 'U'
AND name    NOT IN ('dtproperties')
Tagged with: ,
Posted in SQL Server

Create and apply Patch in git

Creating Patch in git is quite easy and useful when you want to transfer your changes from one branch to another or from one machine to another.

so here is the command you use:

once you clone repo from git using :

git clone git://
cd xyx
git checkout -b branch_1

git checkout -b : it will create new branch with branch_1 name

creating patch

git log --pretty=oneline -3
git format-patch master --stdout > patch_name.patch

above command create new patch file with name patch_name.patch.

now applying patch:
use following command to easily apply patch to branch, apply command shows what are the changes patch will do in your current branch

git apply --check patch_name.patch

use following command to apply the patch

git am --signoff < patch_name.patch
Tagged with:
Posted in SQL Server

TSQL Cursor Syntext

declare cursor cust_cursor fast_forward for
select custid from tblCustomer
Open cust_cursor
fetch next from cust_cursor into @custId
while @@fetch_status = 0
fetch next from cust_cursor into @custId
close cust_cursor
deallocate cust_cursor
Tagged with: ,
Posted in SQL Server

Table compression in SQL Server

If you are using SQL Server 2008 or SQL Server 2008R2, or 2012 you can use table compression in addition to the indexes to get more efficient storage.

Basically table compression has two levels:

1. Row :row-level compression, sql server applies compact storage format for each row in table
2. Page: page-level compression, sql server apply compression algorithms to get more efficient storage on page level.

here is syntax to apply table compression while creating table:

Create table [tablename] (column_name column_type) with (DATA_COMPRESSION = ROW).

To measure how efficient is row or page compression is, you can use sp_estimate_data_compression_savings stored procedure.

The stored procedure accepts table_name, index_id, partition_type and type of compression you would like to apply and return how much data space would be allocated with given compression.

Usually with page compression you can get more efficient storage rather than row based compression as SQL server use compression algorithm for storage.

Tagged with:
Posted in SQL Server

Difference between Date Datatypes in SQL Server 2012


What is basic difference between different date data types in sql server 2012. In SQL 2012, there are new date datatypes have been included such as Datetime2, DateTimeOffset, smalldatetime, Time, Date.

Major difference between each is date time precision and storage space. So, Let look by changing one date data type to other how much space we could save. And saving more space that also means when we index, retrieve or filter data using where condition it also have performance benefits.

The best way to find out how much memory being using by datatype is to create table with datatypes and check the column description using SP_helptext [table_name].

create table tblDate (dt1 datetime, dt2 datetime2,
t1 time, d1 date, dtoff datetimeoffset, sdt smalldatetime)




Select Table name and press Alt+F1 or use sp_helptext tblDate.

It’s clear from the data filed length that if you want to store just date then use DATE field and if your looking to store time then go for SMALLDATETIME, for little more sophesticated datetime then just follow the conventional DATETIME.

But if you are looking for precision then it’s advise to use DATETIME2(7) use it with precision so that you are not over burdening the server to store more precise values.

DATETIMEOFFSET is the most expensive date datatype in sql 2012, having said that it might be more appropriate to use when your application is dealing with multiple time zones.

I also found this link which gives you max date value supported by sql server.

check it out

Tagged with: , , ,
Posted in SQL Server
What people say about post?
Jignesh on barcode code 128 in classic…

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 36 other followers


Get every new post delivered to your Inbox.

Join 36 other followers

%d bloggers like this: