SQL SERVER DYNAMIC ORDER BY WITH ASC OR DESC

I need to make a stored by procedure

CREATE   procedure emp_count

@ORDER AS VARCHAR(1)

AS

SELECT STORE.STORE_NAME, EMPLOYEE.STORE_CODE, COUNT(EMPLOYEE.STORE_CODE) AS EMP_COUNT
FROM STORE
INNER JOIN EMPLOYEE ON STORE.STORE_CODE = EMPLOYEE.STORE_CODE
GROUP BY STORE.STORE_NAME, EMPLOYEE.STORE_CODE

GO

Now if I execute the stored procedure with the parameter of:

'N' I need it to ORDER BY STORE.STORE_NAME ASC

OR

'C' I need it to ORDER BY COUNT(EMPLOYEE.STORE_CODE) DESC

I would I accomplish this?
microcoopAsked:
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.

HilaireCommented:
Please give it a try

CREATE   procedure emp_count
@ORDER AS VARCHAR(1)
AS
SELECT STORE.STORE_NAME, EMPLOYEE.STORE_CODE, COUNT(EMPLOYEE.STORE_CODE) AS EMP_COUNT
FROM STORE
INNER JOIN EMPLOYEE ON STORE.STORE_CODE = EMPLOYEE.STORE_CODE
GROUP BY STORE.STORE_NAME, EMPLOYEE.STORE_CODE
ORDER BY CASE WHEN @ORDER = 'N' THEN STORE.STORE_NAME ELSE '' END ASC,
CASE WHEN  @ORDER = 'C' THEN COUNT(EMPLOYEE.STORE_CODE) ELSE 0 END DESC
GO
0
izblankCommented:
SELECT STORE.STORE_NAME, EMPLOYEE.STORE_CODE, COUNT(EMPLOYEE.STORE_CODE) AS EMP_COUNT
FROM STORE
INNER JOIN EMPLOYEE ON STORE.STORE_CODE = EMPLOYEE.STORE_CODE
GROUP BY STORE.STORE_NAME, EMPLOYEE.STORE_CODE
ORDER BY CASE @ORDER WHEN 'N' THEN STORE.STORE_NAME ELSE NULL END ASC,
          CASE @ORDER WHEN 'C' THEN COUNT(EMPLOYEE.STORE_CODE) ELSE NULL END DESC
0

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
microcoopAuthor Commented:
They came in so close together I felt like I needed to split them, both answers worked. Thank you.
0
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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.