Monday, December 01, 2003

Back from a long weekend. Had some in-laws up, Clint and Beth Shrom. Had a wonderful, fun, weekend with them. But now it's back to work.

I'm working on our wedding site here at work. For any who would like to see the current version, it's www.theimageconnection.com.
I am working on some enhancements including the introduction of grouping for pictures as well as paged image viewing for previews. I am using a SQL stored procedure for this which gets passed the order number for the images to view, as well as the current page requested, number of images per page, and the group to show. It also has logic in it that if the group is All, it shows the next page of all the images. Kinda tricky, complex, yet delightfully functional. I got this from another page and enhanced it.
Here's where I got the stored procedure from (Dot Net Junkies, of course. The easiest to navigate and search.):
http://www.dotnetjunkies.com/Tutorial/70E24E50-C179-4563-B053-2742516BF05B.dcik

And here it is in all it's glory:
CREATE PROCEDURE [Get_Photos_By_Page]
@CurrentPage int,
@PageSize int,
@Job int,
@Group nvarchar(50),
@TotalRecords int output
AS
--Create a temp table to hold the current page of data
--Add and ID column to count the records
CREATE TABLE #TempTable
(
ID int IDENTITY PRIMARY KEY,
ImageName nvarchar(40)
)
--Fill the temp table with the Photo data
If @Group = 'All'
INSERT INTO #TempTable
(
ImageName
)
SELECT
ImageName
FROM
Photos
WHERE
Hide = 1 AND JobNumber = @Job
Else
INSERT INTO #TempTable
(
ImageName
)
SELECT
ImageName
FROM
Photos
WHERE
Hide = 1 AND JobNumber = @Job AND mGroup = @Group
--Create variable to identify the first and last record that should be selected
DECLARE @FirstRec int, @LastRec int
SELECT @FirstRec = (@CurrentPage - 1) * @PageSize
SELECT @LastRec = (@CurrentPage * @PageSize + 1)
--Select one page of data based on the record numbers above
SELECT
ImageName
FROM
#TempTable
WHERE
ID > @FirstRec
AND
ID < @LastRec
--Return the total number of records available as an output parameter
SELECT @TotalRecords = COUNT(*) FROM #TempTable

No comments: