Wednesday, April 17, 2013

Quick Code Dynamic Sql server Script


Declare  @strWhere  nvarchar(200),

@strMainQuery nvarchar(400),

@FromIDbigint ,

@ToIDbigint


set @FromID=11111

set @ToID=11251



if(@FromID<> null and @ToID<> null)

      set @strWhere= ' IS NOT NULL'

Else

      set @strWhere= ' between ' + Convert(varchar(20),@FromID) + ' and ' +  Convert(varchar(20),@ToID)

           

      set @strMainQuery ='SELECT

            d.ID,

            name,

            address,

            telephone

      FROM

            dbo.Customer p

            left outer join dbo.CustomerService d on p.ID = d.ID

      WHERE d.ID' + @strWhere +

      ' ORDER BY

            d.ID'

select @strMainQuery

exec(@strMainQuery)

No comments :