Sorting descending in sp_executesql

I have a stored procedure and the code is posted below. The sp just simply sorts the table depending on the selection.

I execute the sp using the below SQL statement but to no avail any ideas?

DECLARE      @return_value int

EXEC      @return_value = [dbo].[Categories_List_sp]
            @Column = N'CategoryName',
            @Sortoptions = 1,
            @UserID = 1

SELECT      'Return Value' = @return_value
if exists(select * from sys.objects WHERE name = 'Categories_List_sp')
	drop procedure Categories_List_sp;
go
create procedure Categories_List_sp
@Column nvarchar(200)
,@Sortoptions bit
,@UserID bigint
as
	set nocount on
	
	declare @sortOpt bit -- 0 ascending / 1 descending
	declare @col nvarchar(500)
	declare @retVal int
	declare @errMsg nvarchar(100)
	declare @sqlString NVARCHAR(500)
	
	set @col = @Column;
	set @sortOpt = @SortOptions;

	--Check if userId is valid.
	if dbo.Users_Id_FKC_fn(@userId, 0) <> 1
		begin
			set @retVal = dbo.Error_InvalidCredentials_fn()
			set @errMsg = dbo.Error_Message_fn(@retVal)
			raiserror(@errMsg, 16, 1)
			return @retVal
		end
	
	SET @SQLString = 'select  
	 CategoryID
	 ,CategoryName
	 ,[Description]
	 ,ParentCategoryID
	 , ParentCategoryName 
	 , ParentDescription
	 from Categories_GetList_vw ';
	 
	 if (@Col != null)
		begin
			if (@Col = 'CategoryName')
				begin
					set @sqlString += 'order by CategoryName'
					if (@sortOpt >0)
						begin
							set @sqlstring += ' desc'		
						end
				end
			else
				begin
					set @sqlString += 'order by [Description]'
					if (@sortOpt >0)
						begin
							set @sqlstring += ' desc'		
						end	
				end	 		
		end 
		
		exec sp_executesql @SQLString;

Open in new window

LVL 8
PagodNaUtakAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ketansonejiCommented:
Well i tried doing a sort in the sql executed by executeSQL and it works.

Your condition of appending the order by clause is wrong.

Use "if @Col IS NOT NULL" instead of " if (@Col != null)". It should work.

rushShahCommented:
hi try this code

you cannot directly compare value to null..

you can compare only if you have specified SET ANSI_NULLS OFF;
other wise you can do it as i shown in below code using "IS NOT NULL"
if exists(select * from sys.objects WHERE name = 'Categories_List_sp')
	drop procedure Categories_List_sp;
go
create procedure Categories_List_sp
@Column nvarchar(200)
,@Sortoptions bit
,@UserID bigint
as
	set nocount on
	
	declare @sortOpt bit -- 0 ascending / 1 descending
	declare @col nvarchar(500)
	declare @retVal int
	declare @errMsg nvarchar(100)
	declare @sqlString NVARCHAR(500)
	
	set @col = @Column;
	set @sortOpt = @SortOptions;

	--Check if userId is valid.
	if dbo.Users_Id_FKC_fn(@userId, 0) <> 1
		begin
			set @retVal = dbo.Error_InvalidCredentials_fn()
			set @errMsg = dbo.Error_Message_fn(@retVal)
			raiserror(@errMsg, 16, 1)
			return @retVal
		end
	
	SET @SQLString = 'select  
	 CategoryID
	 ,CategoryName
	 ,[Description]
	 ,ParentCategoryID
	 , ParentCategoryName 
	 , ParentDescription
	 from Categories_GetList_vw ';
	 
	 if (@Col IS NOT null)
		begin
			if (@Col = 'CategoryName')
				begin
					set @sqlString += 'order by CategoryName'
					if (@sortOpt >0)
						begin
							set @sqlstring += ' desc'		
						end
				end
			else
				begin
					set @sqlString += 'order by [Description]'
					if (@sortOpt >0)
						begin
							set @sqlstring += ' desc'		
						end	
				end	 		
		end 
		
		exec sp_executesql @SQLString;

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
game-masterCommented:


good afternoon!

change  if (@Col != null) TO if (@Col IS NOT null)

set @sqlString += 'your additional code'  TO set @sqlString = sqlString  + 'your additional code'


PagodNaUtakAuthor Commented:
sds
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.