SET NUMERIC_ROUNDABORT OFF
GO
SET ANSI_PADDING, ANSI_WARNINGS, CONCAT_NULL_YIELDS_NULL, ARITHABORT, QUOTED_IDENTIFIER, ANSI_NULLS ON
GO
IF EXISTS (SELECT * FROM tempdb..sysobjects WHERE id=OBJECT_ID('tempdb..#tmpErrors')) DROP TABLE #tmpErrors
GO
CREATE TABLE #tmpErrors (Error int)
GO
SET XACT_ABORT ON
GO
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
GO
BEGIN TRANSACTION
GO
PRINT N'Altering [dbo].[PR_Common_GetListBySortColumn]'
GO
ALTER PROCEDURE [dbo].[PR_Common_GetListBySortColumn]
    @StartRows INT ,
    @PageSize INT ,
    @PrimaryColumn VARCHAR(1000) ,
    @SortColumnDbType VARCHAR(100) ,
    @SortColumn VARCHAR(1000) ,
    @StrColumn VARCHAR(1000) ,
    @Sorts VARCHAR(100) ,
    @Filter VARCHAR(MAX) ,
    @TableName VARCHAR(1000) ,
    @Total INT OUTPUT
AS 
    SET NOCOUNT ON

    BEGIN
        DECLARE @equalOperator CHAR(2)
        IF @StartRows <= 0 
            BEGIN
                SET @StartRows = 1
                SET @equalOperator = '='
            END
        ELSE 
            BEGIN
                SET @equalOperator = ''
            END

/*Set sorting variables.*/
        DECLARE @operator CHAR(2)
        DECLARE @Order INT
        SET @Order = CHARINDEX('DESC', @Sorts)
        IF @Order > 0 
            BEGIN
                SET @operator = '<' + @equalOperator
            END
        ELSE 
            BEGIN
                SET @operator = '>' + @equalOperator
            END
        DECLARE @strFilter VARCHAR(MAX)
        DECLARE @strSimpleFilter VARCHAR(MAX)

        IF @Filter IS NOT NULL
            AND @Filter != '' 
            BEGIN
                SET @strFilter = ' WHERE ' + @Filter + ' '
                SET @strSimpleFilter = ' AND ' + @Filter + ' '
            END
        ELSE 
            BEGIN	
                SET @strFilter = ''
                SET @strSimpleFilter = ''
            END

        DECLARE @Sql NVARCHAR(MAX)
        SET @Sql = N'SELECT @Total=Count(*) FROM ' + @TableName + @strFilter
        EXEC sp_executesql @Sql, N'@Total Int Out', @Total OUT 

        IF @PageSize <= 0 
            SET @PageSize = @Total

        IF @PrimaryColumn != @SortColumn 
            BEGIN
                EXEC(
                'DECLARE @SortId int ' +
                'DECLARE @SortCol '+ @SortColumnDbType + ' '+
                'SET ROWCOUNT '+ @StartRows + '
                SELECT @SortId = '+ @PrimaryColumn + ',@SortCol='+ @SortColumn +' FROM '+ @TableName + @strFilter +' ORDER BY '+ @SortColumn +' ' +@Sorts + ',' +  @PrimaryColumn + ' ' +@Sorts +'
                IF(@SortCol IS NOT NULL)
                begin
                IF('+@Order+'>0)
                begin
                SET ROWCOUNT '+@PageSize+' 
                SELECT '+ @StrColumn +' FROM '+ @TableName + '
                WHERE ('+@SortColumn+' IS NULL OR '+ @SortColumn  + @operator+'@SortCol OR ('+@SortColumn+'=@SortCol AND '+ @PrimaryColumn  + @operator +'@SortId ))' + @strSimpleFilter + ' ORDER BY '+ @SortColumn + ' ' +@Sorts + ',' + @PrimaryColumn + ' '+ @Sorts + '
                end
                ELSE
                BEGIN
                SET ROWCOUNT '+@PageSize+' 
                SELECT '+ @StrColumn +' FROM '+ @TableName + '
                WHERE ('+@SortColumn+' IS NOT NULL AND ('+ @SortColumn  + @operator+' @SortCol OR ('+ @SortColumn  + '= @SortCol AND '+ @PrimaryColumn  + @operator +'@SortId)))' + @strSimpleFilter + ' ORDER BY '+ @SortColumn + ' ' +@Sorts + ',' + @PrimaryColumn + ' '+ @Sorts + '
                END
                END
                ELSE
                begin
                IF('+@Order+'>0)
                begin
                SET ROWCOUNT '+@PageSize+' 
                SELECT '+ @StrColumn +' FROM '+ @TableName + '
                WHERE ('+@SortColumn+' IS NULL AND '+ @PrimaryColumn  + @operator +' @SortId )' + @strSimpleFilter + ' ORDER BY '+ @SortColumn + ' ' +@Sorts + ',' + @PrimaryColumn + ' '+ @Sorts + '
                END
                else
                begin
                SET ROWCOUNT '+@PageSize+' 
                SELECT '+ @StrColumn +' FROM '+ @TableName + '
                WHERE (('+@SortColumn+' IS NULL AND '+@PrimaryColumn+@operator+'@SortId ) OR '+@SortColumn+' IS NOT NULL )' + @strSimpleFilter + ' ORDER BY '+ @SortColumn + ' ' +@Sorts + ',' + @PrimaryColumn + ' '+ @Sorts + '
                end
                end'
                )
            END
        ELSE 
            BEGIN
                EXEC(
                'DECLARE @SortId int ' +
                'SET ROWCOUNT '+ @StartRows + '
                SELECT @SortId = '+ @SortColumn + ' FROM '+ @TableName + @strFilter +' ORDER BY '+ @SortColumn + ' ' +@Sorts +'
                SET ROWCOUNT '+ @PageSize  + '
                SELECT '+ @StrColumn +' FROM '+ @TableName + ' 
                WHERE '+ @SortColumn  + @operator +' @SortId '+ @strSimpleFilter + ' ORDER BY '+ @SortColumn +' '+ @Sorts + ''
                )
            END

        RETURN @Total
    END

    SET ROWCOUNT 0

    SET NOCOUNT OFF
GO
IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
GO
IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors (Error) SELECT 1 BEGIN TRANSACTION END
GO
IF EXISTS (SELECT * FROM #tmpErrors) ROLLBACK TRANSACTION
GO
IF @@TRANCOUNT>0 BEGIN
PRINT 'The database update succeeded'
COMMIT TRANSACTION
END
ELSE PRINT 'The database update failed'
GO
DROP TABLE #tmpErrors
GO
