ecco quà :) è propio tutta
nata da un articolo su devleap e il bellissimo libro di dino esposito
ciao marco
ALTER PROCEDURE sp_Image_Paging
@ImageID int = Null,
@GalleryID int = NULL,
@Description varchar(50) = '',
@PageSize int = 10,
@SortExpression varchar(100) = NULL,
@CurrentPage int = 1,
@RecordCount int OUTPUT
AS
DECLARE @SqlStatement nvarchar(1000)
DECLARE @SqlCondition nvarchar(1000)
DECLARE @CustomColumn nvarchar(100)
DECLARE @Prefix nvarchar(7)
DECLARE @PrefixA nvarchar(7)
DECLARE @SerchDateColumn nvarchar(100)
SET @Prefix = ''
SET @PrefixA = ''
SET @SqlCondition = ''
------------------------------------------------------------------------------------------------------------------------------
-- condizioni di ricerca --
------------------------------------------------------------------------------------------------------------------------------
--se ho il filtro sulla categoria
IF (NOT @ImageID IS NULL)
BEGIN
SET @SqlCondition = @SqlCondition + @Prefix + ' ImageID = ' + CONVERT(nvarchar,@ImageID)
SET @Prefix = ' AND '
END
--se ho il filtro sulla categoria
IF (NOT @GalleryID IS NULL)
BEGIN
SET @SqlCondition = @SqlCondition + @Prefix + ' GalleryID = ' + CONVERT(nvarchar,@GalleryID)
SET @Prefix = ' AND '
END
--filtro sul titolo
IF (@Description <> '')
BEGIN
SET @SqlCondition = @SqlCondition + @Prefix + ' Name LIKE ''%' + CONVERT(nvarchar(30), REPLACE(@Description,'''','''''')) + '%'''
SET @Prefix = ' AND '
END
---------------------------------------------------------------------------------------------------------------------------------
-- imposta ordinamento --
---------------------------------------------------------------------------------------------------------------------------------
IF (@SortExpression IS NULL)
SET @CustomColumn = 'ImageID'
ELSE
SET @CustomColumn = 'CONVERT(nvarchar(100), ' + @SortExpression + ', 20)'
-------------------------------------------------------------------------------------------------------------------------------
IF (@SqlCondition <> '')
SET @Prefix = ' WHERE '
IF (@SqlCondition <> '')
SET @PrefixA = ' AND '
----------------------------------------------------------------------------------------------------------------------------------
-- costruisce la query vera e propia --
----------------------------------------------------------------------------------------------------------------------------------
IF (@CurrentPage > 1)
SET @SqlStatement = 'SELECT TOP ' + CONVERT(nvarchar, @PageSize) +
' * FROM Image_Image WHERE ImageID NOT IN
(SELECT TOP ' + CONVERT(nvarchar, (@PageSize * (@CurrentPage - 1))) + ' ImageID FROM Image_Image ' + @Prefix + @SqlCondition + ' ORDER BY ' + @CustomColumn + ' DESC)'
+ @PrefixA + @SqlCondition + ' ORDER BY ' + @CustomColumn + ' DESC'
ELSE
SET @SqlStatement ='SELECT TOP ' + CONVERT(nvarchar, @PageSize) + '
* FROM Image_Image ' + @Prefix + @SqlCondition + ' ORDER BY ' + @CustomColumn + ' DESC'
PRINT @SqlStatement
EXEC sp_executesql @SqlStatement
----------------------------------------------------------------------------------------------------------------------------------
-- costruisco la query per contare gli item che rientrano nel criterio di riecerca --
----------------------------------------------------------------------------------------------------------------------------------
SET @SqlStatement = 'SELECT @RC = Count(*) FROM Image_Image' + @Prefix + @SqlCondition
EXEC sp_executesql @SqlStatement, N'@RC int OUTPUT', @RC = @RecordCount OUTPUT
RETURN