Tuesday, February 3, 2009

Paged exec stored procedure



CREATE PROCEDURE [dbo].[PagedExec]
(
@sqlIn NVARCHAR(4000),
@PageIndex INT=1,
@PageSize INT=10,
@OrderBy NVARCHAR(255)='',
@OrderByType NVARCHAR(10)='ASC',
@NumberOfTotalPages int out,
@NumberOfTotalRecords int out
)
AS
SET NOCOUNT ON
DECLARE @sql NVARCHAR(4000)
DECLARE @sqlInLower NVARCHAR(4000)
DECLARE @SelectIndex int;
DECLARE @FromIndex int;
DECLARE @StartExtractIndex int;
DECLARE @EndExtractLen int;
DECLARE @WhereIndex int;
DECLARE @GroupByPresent int

Select @sqlInLower = Lower(@sqlIn)
SELECT @SelectIndex=CHARINDEX ( 'select', @sqlInLower)
SELECT @FromIndex=CHARINDEX ( 'from', @sqlInLower)
SELECT @GroupByPresent=CHARINDEX ( 'group by', @sqlInLower)
Set @StartExtractIndex = @SelectIndex + LEN('Select')
-- Add the row count stuff
-- The Order By in the ROW_NUMBER functiuonality is dummy. Overriden by the order by in select statement
Set @sql = N'Select ROW_NUMBER() OVER (ORDER BY ' + @OrderBy + N' ' + @OrderByType + N') AS RowNumber, '
Set @sql = @sql + right(@sqlIn, LEN(@sqlIn) - @StartExtractIndex)
-- Now wrap the with stuff
set @sql = N'WITH sqlWithRowNumbers AS(' + @sql + N')'
-- Find the selected fields
Declare @sqlSelectedFields NVARCHAR(2000)
Declare @sqlOriginalFields NVARCHAR(2000)
set @sqlOriginalFields = substring (@sqlIn, @StartExtractIndex, @FromIndex-@StartExtractIndex)
set @sqlSelectedFields = ' '
Declare @loop int
set @loop=1
declare @index1 int
declare @index2 int
SELECT @index1=CHARINDEX ( '''', @sqlOriginalFields)
SELECT @index2=CHARINDEX ( '''', @sqlOriginalFields, @index1+1)

while @loop=1
Begin
BEGIN TRY
Declare @FieldName NVARCHAR(500)
Set @FieldName = '"' + Substring(@sqlOriginalFields, @index1+1, @index2-@index1-1) + '"'
print @FieldName
if @FieldName <> '" "'
Begin
if @sqlSelectedFields <> ' '
set @sqlSelectedFields = @sqlSelectedFields + ', '
Set @sqlSelectedFields = @sqlSelectedFields + @FieldName
End
set @index2 = @index2+1
set @index1 = @index2;
SELECT @index1=CHARINDEX ( '''', @sqlOriginalFields, @index1+1)
if @index1=0
Begin
Set @loop=0
break;
End
SELECT @index2=CHARINDEX ( '''', @sqlOriginalFields, @index1+1)
END TRY
BEGIN CATCH
Set @loop=0
END CATCH
End
print @sqlSelectedFields
print @PageIndex
set @sql = @sql + N' Select ' + @sqlSelectedFields + N' from sqlWithRowNumbers'
set @sql = @sql + ' Where RowNumber between '
set @sql = @sql + STR(((@PageIndex - 1) * @PageSize) + 1)
set @sql = @sql + N' and '
set @sql = @sql + STR(@PageIndex*@PageSize)
EXEC sp_executesql @sql
DECLARE @ParmDefinition NVARCHAR(500)
SET @ParmDefinition = N'@NumberOfTotalRecords int OUTPUT'
if @GroupByPresent>0
Set @sql = N'select @NumberOfTotalRecords = count(*) from (Select Count(*) as cnt ' + right(@sqlIn, LEN(@sqlIn) - @FromIndex+2) + ') as grpcnt'
else
Set @sql = N'Select @NumberOfTotalRecords = Count(*) ' + right(@sqlIn, LEN(@sqlIn) - @FromIndex+2)
EXEC sp_executesql @sql, @ParmDefinition, @NumberOfTotalRecords out
Declare @floatTemp float
Declare @floatTemp1 float
Declare @floatTemp2 float
-- If we do not use temp float variables, the CEILING function does not work, down below.
Set @floatTemp1 = @NumberOfTotalRecords
Set @floatTemp2 = @PageSize
set @floatTemp = CEILING(@floatTemp1 / @floatTemp2)
Set @NumberOfTotalPages = CEILING(@floatTemp)
if @NumberOfTotalRecords is NULL
set @NumberOfTotalRecords= 0
if @NumberOfTotalPages is NULL
set @NumberOfTotalPages= 0
print @NumberOfTotalRecords
print @NumberOfTotalPages
RETURN 0

Monday, January 26, 2009

About Baytek

We are boutique software Development Company, based in Los Angeles, California. Our forte is .Net development of any kind. Our business model is very simple.
1- We place in house developers for expedited development. This helps us understand your business quickly.
2- We use offshore developers to accompany in-house developers. This cuts cost down.
3- We take on only limited projects, so we don’t dilute our resources.
4- Quality is our main focus. Cost comes second. We only hire good programmers and pay them well. Our experience shows that work done by unqualified people is a waste of money, time and a source of a big mess that is only to be cleaned up by someone else.
We have created sample useful applications for our potential client to download and evaluate our capabilities. These applications are absolutely free and yours to keep. They will prove to be useful to you as well as giving you sense of how we can help you. Enjoy…

Wsdldisco
This is a very useful tool for SOA developers. You can use this tool to create a single access point, a portal, for all your web services. Please visit the link to get more info on the tool.
Wsdldisco is a very good ASPX.Net sample. This tool illustrates our depth of knowledge of ASP.Net platform. Here are some key points:
1- Usage of Reflection technology.
2- Dynamic code generation.
3- Dynamic code compilation.
4- Dynamic assembly manipulation and Web page adaptation.
5- Static and dynamic web page generation.
6- Modular design using UserControl and assemblies.
7- Implementation of Ajax.
8- Implementation of Interfaces and abstract classes.
9- Implementation of documentation using MS Sandcastle.


MovieCatalog
This tool demonstrates our Flash and Flex capabilities. MovieCatalog is an easy-to-use application which helps users to organize their movies.

TraceExtension
Log messages going in and out of a Webservice. ASPX.Net sample
1. Support for multi-thread. Original code would work fine until you had ‘real traffic”. Log file would get corrupted while handling simultaneous request.
2. Log file name is no longer hard coded. Directory name is Web.config driven. File names are stamped with date, so you get a new file for each day.
3. Capability to make the ‘log file’ browser compatible, so you can view it in IE or FireFox.
4. Extended control in Web.config to turn logging on and off.
5. Exception handling. You may want tailor the exception handling to your needs…