SELECT customerid, firstname, lastname, statecode, statedescription, totalsales FROM dbo.Customer ORDER BY CASE @sortdirection WHEN 'asc' THEN CASE @sortby WHEN 'firstname' THEN firstname WHEN 'lastname' THEN lastname END END ASC, CASE @sortdirection WHEN 'desc' THEN CASE @sortby WHEN 'firstname' THEN firstname WHEN 'lastname' THEN lastname END END DESC GO EXEC dbo.getCustomerData 'lastname', 'desc'