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.
| Author |
Topic |
|
ias0nas
Starting Member
36 Posts |
Posted - 2008-04-29 : 10:45:42
|
| Hello,I have a stored procedure with dynamic ORDER BY. I would like to use the DISTINCT too. Is it somehow possible?Thank youHere is the stored procedure:SELECT identifier_company + cast(identifier_number as nvarchar(3)) as identifier, CASE WHEN canceled = 'True' THEN 'canceledPO' ELSE '' END AS style, staff.staff_name, purchase.purchase_id, purchase.traveller_name, nominal_department.department_name, purchase.canceled, purchase.travel_date, convert(nvarchar(20), purchase.date_raised, 103) as dated, supplier FROM purchase INNER JOIN purchase_project ON purchase.purchase_id = purchase_project.purchase_id INNER JOIN staff ON purchase.raised = staff.staff_id INNER JOIN nominal_department ON purchase.department = nominal_department.nominal_dep_id WHERE (raised in (SELECT staff_id FROM staff WHERE department like @FromDepartment) or purchase.raised = @raisedBy) and purchase_project.project_number like '%' + @Query + '%' ORDER BY CASE @SortDir WHEN 'ASC' THEN CASE @OrderBy WHEN 'staff_name' THEN cast(staff_name as nvarchar(100)) WHEN 'traveller_name' THEN cast(traveller_name as nvarchar(100)) WHEN 'department_name' THEN cast(department_name as nvarchar(100)) WHEN 'supplier' THEN cast(supplier as nvarchar(100)) WHEN 'canceled' THEN cast(canceled as nvarchar(10)) END END ASC, CASE @SortDir WHEN 'DESC' THEN CASE @OrderBy WHEN 'staff_name' THEN cast(staff_name as nvarchar(100)) WHEN 'traveller_name' THEN cast(traveller_name as nvarchar(100)) WHEN 'department_name' THEN cast(department_name as nvarchar(100)) WHEN 'supplier' THEN cast(supplier as nvarchar(100)) WHEN 'canceled' THEN cast(canceled as nvarchar(10)) END END DESC |
|
|
ias0nas
Starting Member
36 Posts |
Posted - 2008-04-29 : 10:58:53
|
| Ok got it, sorryThe trick is to create a view with the SLECT and then select * from [viewname] with WHERE clause and dynamic ORDER BYThanks |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-04-29 : 10:59:50
|
| This is possible.But why have you repeated the CASE twice within ORDER BY? i think you just need a CASE WHEN inside another CASE |
 |
|
|
ias0nas
Starting Member
36 Posts |
Posted - 2008-04-29 : 11:09:50
|
| Yes I imagine it would be possible with nested case but this was just a copy & paste ;) |
 |
|
|
|
|
|
|
|