Below is a stored prodecure that Sunil helped me with (among others) in another forum. It works almost perfectly, except I forgot about adding a sorting-direction.
I added the @sortType variable/parameter that will take in either "ASC" or "DESC". Ideally I wanted to change the lines to just have this parameter at the end of it, so it would read:
However, this does not work. Is there a simple way to make this work without having to re-write everything twice in an If-Else statement checking for the sorting direction value?
Here is the stored procedure:
-Ovatvvon :-Q
I added the @sortType variable/parameter that will take in either "ASC" or "DESC". Ideally I wanted to change the lines to just have this parameter at the end of it, so it would read:
Code:
...
Case @SortOrder When 1 Then C.id + @sortType
When 2 Then C.dba_name + @sortType
When 3 Then C.phone + @sortType
When 4 Then C.fax + @sortType
When 5 Then C.payroll_rep + @sortType
...
However, this does not work. Is there a simple way to make this work without having to re-write everything twice in an If-Else statement checking for the sorting direction value?
Here is the stored procedure:
Code:
CREATE PROCEDURE [dbo].[sql_searchClientAccount]
@company varchar(10),
@id varchar(7),
@name varchar(50),
@status varchar(1),
@phone varchar(16),
@sortOrder int,
@sorttype varchar(4)
AS
IF @status != '0'
Select TBL.id, TBL.dba_name, TBL.phone, TBL.fax, TBL.payroll_rep, TBL.csr,
TBL.cam, employeeCount
FROM
( SELECT DISTINCT
Top 501 C.id, C.dba_name, C.phone, C.fax, C.payroll_rep, CP.csr, CP.cam, COUNT(DISTINCT EC.employee_id) AS employeeCount,
Case @SortOrder When 1 Then C.id
When 2 Then C.dba_name
When 3 Then C.phone
When 4 Then C.fax
When 5 Then C.payroll_rep
When 6 Then CP.csr
When 7 Then CP.cam
When 8 Then CAST(COUNT(DISTINCT EC.employee_id) as varchar)
End SortFld,
@SortOrder SortOption
FROM Client C LEFT OUTER JOIN
CP_CLIENT CP ON C.id = CP.CLIENT_id LEFT OUTER JOIN
EMPLOYEE_CLIENT EC ON C.id = EC.CLIENT_id
Where C.id LIKE @id AND C.company LIKE @company AND C.dba_name LIKE @name AND C.phone LIKE @phone AND C.client_status = @status
GROUP BY C.id, C.dba_name, C.phone, C.fax, C.payroll_rep, CP.csr, CP.cam ) TBL
Order by case When SortOption=8 then cast(SortFld as int) else 0 end,
case When SortOption<>8 then SortFld else '' end
ELSE
Select TBL.id, TBL.dba_name, TBL.phone, TBL.fax, TBL.payroll_rep, TBL.csr, TBL.cam, employeeCount
FROM
(SELECT DISTINCT
Top 501 C.id, C.dba_name, C.phone, C.fax, C.payroll_rep, CP.csr, CP.cam, COUNT(DISTINCT EC.employee_id) AS employeeCount,
Case @SortOrder When 1 Then C.id
When 2 Then C.dba_name
When 3 Then C.phone
When 4 Then C.fax
When 5 Then C.payroll_rep
When 6 Then CP.csr
When 7 Then CP.cam
When 8 Then CAST(COUNT(DISTINCT EC.employee_id) as varchar)
End SortFld,
@SortOrder SortOption
FROM Client C LEFT OUTER JOIN
CP_CLIENT CP ON C.id = CP.CLIENT_id LEFT OUTER JOIN
EMPLOYEE_CLIENT EC ON C.id = EC.CLIENT_id
WHERE C.id LIKE @id AND C.company LIKE @company AND C.dba_name LIKE @name AND
(C.client_status='A' OR C.client_status='P') AND C.phone LIKE @phone
GROUP BY C.id, C.dba_name, C.phone, C.fax, C.payroll_rep, CP.csr, CP.cam) TBL
Order by case When SortOption=8 then cast(SortFld as int) else 0 end,
case When SortOption<>8 then SortFld else '' end
GO
-Ovatvvon :-Q