Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 SP with variables and ORDER BY

Author  Topic 

landau66
Yak Posting Veteran

61 Posts

Posted - 2013-12-29 : 06:50:31
Hi everyone,

I work on a little program. I want to achieve the following:

A stored procedure which selects records from one table. I will only use one of the variables - never more than one. I want to use "order by" to order the result by the same column alphabetically as I use for search Input.


create proc spGetCustomersByAlphaOrder
@firstName nvarchar(50),
@lastName nvarchar(50),
@email nvarchar(50)
as
begin
select * from Customers
where (firstName like @firstName+'%' or @firstName = null)and
(lastName like @lastName+'%' or @lastName = null)and
(email like @email+'%' or @email = null)

order by ???? I WANT TO ORDER BY SAME COLUMN AS THE "WHERE-SEARCH"

end

Please let me know how to integrate an "Order-By-Possibility"

Many thanks in advance!
landau


visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-12-29 : 12:51:15
your procedure should be like this

create proc spGetCustomersByAlphaOrder
@firstName nvarchar(50),
@lastName nvarchar(50),
@email nvarchar(50)
as
begin
select * from Customers
where (firstName like @firstName+'%' or @firstName is null)and
(lastName like @lastName+'%' or @lastName is null)and
(email like @email+'%' or @email is null)

order by case when @firstName > '' then firstName else 1 end,
case when @lastName > '' then lastName else 1 end,
case when @email > '' then email else 1 end

end


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

waterduck
Aged Yak Warrior

982 Posts

Posted - 2013-12-29 : 21:16:32
[code]
create proc spGetCustomersByAlphaOrder
@firstName nvarchar(50),
@lastName nvarchar(50),
@email nvarchar(50)
as
begin

select * from Customers
where (firstName like @firstName+'%' or @firstName is null)and
(lastName like @lastName+'%' or @lastName is null)and
(email like @email+'%' or @email is null)

order by @firstName + firstName,
@lastName + lastName,
@email + email

end
[/code]
Go to Top of Page

landau66
Yak Posting Veteran

61 Posts

Posted - 2014-01-07 : 05:15:43
Hi
and thank you for your answers. It really helped me a lot.

I still have a question: Why is the solution from visakh16 working? The ORDER BY part uses CASE WHEN ELSE. I dont understand why it Needs the ELSE 1 part - what does it do?

case when @firstName > '' then firstName else 1 end

Thank you very much in advance
landau
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2014-01-07 : 05:26:37
quote:
Originally posted by landau66

Hi
and thank you for your answers. It really helped me a lot.

I still have a question: Why is the solution from visakh16 working? The ORDER BY part uses CASE WHEN ELSE. I dont understand why it Needs the ELSE 1 part - what does it do?

case when @firstName > '' then firstName else 1 end

Thank you very much in advance
landau


it bypasses the cases where particular filter is not selected to consider all values as 1.

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page
   

- Advertisement -