Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
SQL
ALTER PROCEDURE [dbo].[Profile_GETCity ]
    @PageSize int = null,
    @CurrentPage int = null,
    @SortExpression    nvarchar(max) = null
	
AS

BEGIN
    SET NOCOUNT ON

    DECLARE @SqlString nvarchar(max)
    Declare @UpperBand int
    Declare @LowerBand int        
    
    SET @LowerBand  = (@CurrentPage - 1) * @PageSize
    SET @UpperBand  = (@CurrentPage * @PageSize) + 1    

    BEGIN
	

	SET @SqlString='WITH tempProfile AS
        (                    
            SELECT 
                [ID],
                [Name],
				state=(select Name from State_District where City.StateId=State_District.ID),
				District=(select Name from State_District where City.DistrictId=State_District.ID),
				[PinCode],
                [status] = CASE [IsActive] WHEN 1 THEN ''label label-sm label-success'' WHEN 0 THEN ''label label-sm label-danger'' END, 
				[commandname] = CASE [IsActive] WHEN 1 THEN ''Active'' WHEN 0 THEN ''InActive'' END ,                             
                ROW_NUMBER() OVER (ORDER BY '+ @SortExpression+' ) AS RowNumber                 
                FROM [dbo].[City] 
        )     

        SELECT 
		       [ID],
               [Name],
			    state,
				District,
				[PinCode],
                [status],
                [commandname]                                       
        FROM 
            tempProfile 
        WHERE 
            RowNumber > ' + CONVERT(VARCHAR,@LowerBand) + 'AND RowNumber <' +CONVERT(VARCHAR, @UpperBand)
            + 'ORDER BY ' + @SortExpression  
	end
    EXEC sp_executesql @SqlString

  
END


sub query column is not reading as a column on sorting

What I have tried:

this link
Posted
Updated 7-Mar-16 22:27pm
v2

You can't order by state in the row_number because it isn't assessed before the row_number column. The aliases are applied after the query.

You have two options:
Create another cte before the tempProfile cte that does the alias assignments first:
SQL
SET @SqlString='
WITH 
aliases as (
SELECT 
    [ID],
    [Name],
    state=(select Name from State_District where City.StateId=State_District.ID),
    District=(select Name from State_District where City.DistrictId=State_District.ID),
    [PinCode],
    [status] = CASE [IsActive] 
        WHEN 1 THEN ''label label-sm label-success'' 
        WHEN 0 THEN ''label label-sm label-danger'' END, 
    [commandname] = CASE [IsActive] 
        WHEN 1 THEN ''Active'' 
        WHEN 0 THEN ''InActive'' END               
FROM [dbo].[City]       
),
tempProfile AS
(                    
    SELECT 
        [ID],
        [Name],
        state,
        District,
        [PinCode],
        [status],
        [commandname],                               
        ROW_NUMBER() OVER (ORDER BY '+ @SortExpression+' ) AS RowNumber
    FROM aliases   
)     
 
SELECT 
[ID],
[Name],
state,
District,
[PinCode],
[status],
[commandname]                                       
FROM 
     tempProfile 
WHERE 
    RowNumber > ' + CONVERT(VARCHAR,@LowerBand) + 'AND RowNumber <' +CONVERT(VARCHAR, @UpperBand)
            + 'ORDER BY ' + @SortExpression  


Or use the entire (select Name from State_District where City.StateId=State_District.ID) in the sort expression. That won't really suit your use in this case, though.

Hope that helps
Andy ^_^


UPDATE: Missed a comma
 
Share this answer
 
v2
Comments
itsathere 8-Mar-16 1:03am    
this query is not returning any data.
Andy Lanng 8-Mar-16 1:53am    
try removing the "where" clause and see what data you get. There is nothing else in the query that would cause data to be omitted.
I'm guessing that you are trying to set up a paging query?
itsathere 8-Mar-16 2:11am    
Yes, I am trying for paging with sorting in asp.net webforms.After removing where clause I am getting error incorrect syntax near '('
Andy Lanng 8-Mar-16 2:22am    
Could you post the new stored proc with the Where clause removed please. There is no reason for that error with only the Where clause removed.

With paging you will need to sort by a secondary unique field. This is because I doubt that 'state' for eg is unique and therefore the sorting may be inconsistent. Use ROW_NUMBER() OVER (ORDER BY '+ @SortExpression+', ID ) AS RowNumber instead.
Andy Lanng 8-Mar-16 2:29am    
Off to work - I'll pick this up then
Small modification in column alias in CTE tempProfile. Try with below query:
SQL
ALTER PROCEDURE [dbo].[Profile_GETCity ]
    @PageSize int = null,
    @CurrentPage int = null,
    @SortExpression    nvarchar(max) = null	
AS 
BEGIN
    SET NOCOUNT ON
 
    DECLARE @SqlString nvarchar(max)
    Declare @UpperBand int
    Declare @LowerBand int        
    
    SET @LowerBand  = (@CurrentPage - 1) * @PageSize
    SET @UpperBand  = (@CurrentPage * @PageSize) + 1    
 
    SET @SqlString='WITH tempProfile AS
        (                    
            SELECT 
                [ID],
                [Name],
				(select Name from State_District where City.StateId=State_District.ID) AS state,
				(select Name from State_District where City.DistrictId=State_District.ID) AS District,
				[PinCode],
                CASE [IsActive] 
					WHEN 1 THEN 'label label-sm label-success' 
					WHEN 0 THEN 'label label-sm label-danger' END  AS status, 
				CASE [IsActive] 
					WHEN 1 THEN 'Active' 
					WHEN 0 THEN 'InActive' END AS commandname,                             
                ROW_NUMBER() OVER (ORDER BY '+ @SortExpression+' ) AS RowNumber                 
                FROM [dbo].[City] 
        )     
 
        SELECT 
		       [ID],
               [Name],
			    [state],
				[District],
				[PinCode],
                [status],
                [commandname]                                       
        FROM 
            tempProfile 
        WHERE 
            RowNumber > ' + CONVERT(VARCHAR,@LowerBand) + 'AND RowNumber <' +CONVERT(VARCHAR, @UpperBand)
            + 'ORDER BY ' + @SortExpression  
	
    EXEC sp_executesql @SqlString
   
END
 
Share this answer
 
v2
Comments
itsathere 8-Mar-16 1:10am    
I have already tried this is not working.
Andy Lanng 8-Mar-16 1:55am    
There are a couple of ways to write column aliases. It makes no difference which one you choose
SQL
ALTER PROCEDURE [dbo].[Profile_GETCity]
@PageSize int = null,
@CurrentPage int = null,
@SortExpression nvarchar(max) = null

AS

BEGIN
SET NOCOUNT ON

DECLARE @SqlString nvarchar(max)
Declare @UpperBand int
Declare @LowerBand int 

SET @LowerBand = (@CurrentPage - 1) * @PageSize
SET @UpperBand = (@CurrentPage * @PageSize) + 1 




SET @SqlString='
WITH 
aliases as (
SELECT 
[ID],
[Name],
state=(select Name from State_District where City.StateId=State_District.ID),
District=(select Name from State_District where City.DistrictId=State_District.ID),
[PinCode],
[status] = CASE [IsActive] 
WHEN 1 THEN ''label label-sm label-success'' 
WHEN 0 THEN ''label label-sm label-danger'' END, 
[commandname] = CASE [IsActive] 
WHEN 1 THEN ''Active'' 
WHEN 0 THEN ''InActive'' END 
FROM [dbo].[City] 
),
tempProfile AS
( 
SELECT 
[ID],
[Name],
state,
District,
[PinCode],
[status],
[commandname] ,
ROW_NUMBER() OVER (ORDER BY '+ @SortExpression+', ID ) AS RowNumber
FROM aliases 
) 

SELECT 
[ID],
[Name],
state,
District,
[PinCode],
[status],
[commandname] 
FROM 
tempProfile  WHERE 
            RowNumber > ' + CONVERT(VARCHAR,@LowerBand) + 'AND RowNumber <' +CONVERT(VARCHAR, @UpperBand)
            + 'ORDER BY ' + @SortExpression 



end
EXEC sp_executesql @SqlString
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900