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
Tuesday, February 3, 2009
Paged exec stored procedure
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…
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…
Thursday, December 11, 2008
IMDB Movie Catalog with Adobe AIR
Hey guys,
I recently needed a program to lookup the IMDB ratings of the movies in my Movies folder. My movies are each in its own folder. It goes like this:
Casablanca
Click
Cloverfield
Conspiracy Theory
Die Hard 1
Die Hard 3
Eastern Promises
.
.
.
and so on. So regarding the number of movies it is a very difficult thing to find a movie to watch which has a good IMDB rating. So I decided to write a program which iterates through all the folder names in my Movies folder and looks them up in IMDB.
[UPDATE] - I recently updated the program to be able to read the filenames like: "Saving.Private.Ryan.720p.HDTV.Premiere.DTS.x264-ESiR.mkv". Even if the movie folders and files are in the same directory it should work fine.
the code can be seen here.
Also I am attaching a windows setup file for non-developers out there. You can contact me for its bugs. I will try to update it time to time.
have fun.
Note: Double click on items opens the IMDB page of that movie!

I recently needed a program to lookup the IMDB ratings of the movies in my Movies folder. My movies are each in its own folder. It goes like this:
Casablanca
Click
Cloverfield
Conspiracy Theory
Die Hard 1
Die Hard 3
Eastern Promises
.
.
.
and so on. So regarding the number of movies it is a very difficult thing to find a movie to watch which has a good IMDB rating. So I decided to write a program which iterates through all the folder names in my Movies folder and looks them up in IMDB.
[UPDATE] - I recently updated the program to be able to read the filenames like: "Saving.Private.Ryan.720p.HDTV.Premiere.DTS.x264-ESiR.mkv". Even if the movie folders and files are in the same directory it should work fine.
the code can be seen here.
Also I am attaching a windows setup file for non-developers out there. You can contact me for its bugs. I will try to update it time to time.
Windows Setup File
Adobe Air File Format
have fun.
Note: Double click on items opens the IMDB page of that movie!


Tuesday, November 18, 2008
Code documentation with SandCastle
1- Install sandcastle (latest release)
2- Sandcastle is command line driven. No GUI. Install this GUI tool to make life easy.
3- You must enable XML documentation file check box for each project. This will create an xml file along with each assembly.
4- Take each assembly and xml file drag and drop it into the tool.
5- Third party LIBS and assemblies that you do not have xml doc for can be excluded using BUILD/Dependencies section.
6- Use VISIBILITY/ApiFilter to cut down on what gets compiled into the help file. Otherwise it becomes too overwhelming
7- Doc for XML tags
Doc1
Doc2
Doc3
2- Sandcastle is command line driven. No GUI. Install this GUI tool to make life easy.
3- You must enable XML documentation file check box for each project. This will create an xml file along with each assembly.
4- Take each assembly and xml file drag and drop it into the tool.
5- Third party LIBS and assemblies that you do not have xml doc for can be excluded using BUILD/Dependencies section.
6- Use VISIBILITY/ApiFilter to cut down on what gets compiled into the help file. Otherwise it becomes too overwhelming
7- Doc for XML tags
Doc1
Doc2
Doc3
Friday, October 31, 2008
Using group by the right way
Here are two great articles about the usage of Group By:
SQL GROUP BY techniques
More on GROUP BY; Examining SUM(Distinct)
Thanks to Jeff Smith.
In a nut-shell, instead of this:
Use this:
This way you don’t need to include bunch of fields in your group by clause unnecessarily.
SQL GROUP BY techniques
More on GROUP BY; Examining SUM(Distinct)
Thanks to Jeff Smith.
In a nut-shell, instead of this:
SELECT
C.CustomerID, C.CustomerName,
C.CustomerType, C.Address1, C.City,
C.State, SUM(S.Sales) as TotalSales
FROM
Customers C
INNER JOIN Sales S
ON C.CustomerID = S.CustomerID
GROUP BY
C.CustomerID, C.CustomerName,
C.CustomerType, C.Address1, C.City, C.State
Use this:
SELECT
C.CustomerID, C.CustomerName,
C.CustomerType, C.Address1, C.City,
C.State, S.TotalSales
FROM
Customers C
INNER JOIN
(SELECT
CustomerID, SUM(Sales) as TotalSales
FROM
Sales
GROUP BY
CustomerID) S
ON
C.CustomerID = S.CustomerID
This way you don’t need to include bunch of fields in your group by clause unnecessarily.
Wednesday, October 15, 2008
How to find out which process is locking a DLL
1- Start->Run and Open Cmd box
2- Type and execute the following
tasklist /m mydll.dll
and it will return a process list that have loaded your DLL!
Cool ha!
"tasklist /?" will give you a list of other things you can do with this utility.
2- Type and execute the following
tasklist /m mydll.dll
and it will return a process list that have loaded your DLL!
Cool ha!
"tasklist /?" will give you a list of other things you can do with this utility.
Tuesday, September 30, 2008
Macro for Visual Studio 2005 Keyboard Shortcuts
Most people don't know this, but there are actually over 450 keyboard shortcuts in Visual Studio by default. But there is no easy way to find out all the keyboard shortcuts inside Visual Studio. You can find out what all the default keyboard shortcuts are by writing a simple macro to enumerate all of them. The following is the code for this.
To use this macro, go to Tools, select Macros, and then choose Macros IDE. . . to launch the Macros IDE. Expand the MyMacros project, MyMacros namespace and double-click on Module1. Simply copy Listing 1 to the Macros IDE and run the macro. After running the macro, you would have produced a keyboard shortcuts reference for Visual Studio. Open your output at C:\demo\Shortcuts.html.
Imports System
Imports EnvDTE
Imports EnvDTE80
Imports EnvDTE90
Imports System.Diagnostics
Imports System.IO
Public Module Module1
Public Sub ListShortcutsInHTML()
'Declare a StreamWriter
Dim sw As System.IO.StreamWriter
sw = New StreamWriter("c:\\demo\\Shortcuts.html")
'Write the beginning HTML
WriteHTMLStart(sw)
' Add a row for each keyboard shortcut
For Each c As Command In DTE.Commands
If c.Name <> "" Then
Dim bindings As System.Array
bindings = CType(c.Bindings, System.Array)
For i As Integer = 0 To bindings.Length - 1
sw.WriteLine("<tr>")
sw.WriteLine("<td>" + c.Name + "</td>")
sw.WriteLine("<td>" + bindings(i) + "</td>")
sw.WriteLine("</tr>")
Next
End If
Next
'Write the end HTML
WriteHTMLEnd(sw)
'Flush and close the stream
sw.Flush()
sw.Close()
End Sub
Public Sub WriteHTMLStart(ByVal sw As System.IO.StreamWriter)
sw.WriteLine("<html>")
sw.WriteLine("<head>")
sw.WriteLine("<title>")
sw.WriteLine("Visual Studio Keyboard Shortcuts")
sw.WriteLine("</title>")
sw.WriteLine("</head>")
sw.WriteLine("<body>")
sw.WriteLine("<h1>Visual Studio 2005 Keyboard Shortcuts</h1>")
sw.WriteLine("<font size=""2"" face=""Verdana"">")
sw.WriteLine("<table border=""1"">")
sw.WriteLine("<tr BGCOLOR=""#018FFF""><td align=""center""><b>Command</b></td><td align=""center""><b>Shortcut</b></td></tr>")
End Sub
Public Sub WriteHTMLEnd(ByVal sw As System.IO.StreamWriter)
sw.WriteLine("</table>")
sw.WriteLine("</font>")
sw.WriteLine("</body>")
sw.WriteLine("</html>")
End Sub
End Module
To use this macro, go to Tools, select Macros, and then choose Macros IDE. . . to launch the Macros IDE. Expand the MyMacros project, MyMacros namespace and double-click on Module1. Simply copy Listing 1 to the Macros IDE and run the macro. After running the macro, you would have produced a keyboard shortcuts reference for Visual Studio. Open your output at C:\demo\Shortcuts.html.
Subscribe to:
Posts (Atom)