Last active
March 24, 2019 06:24
-
-
Save pujansrt/6c138fd38028f1b49731b45e423a9f5d to your computer and use it in GitHub Desktop.
Dynamic Order by (SQL)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- Make sure whatever _order is passed, is selected in SELECT. | |
@delimiter %%%; | |
CREATE PROCEDURE prime1(_order VARCHAR(32)) NOT DETERMINISTIC READS SQL DATA | |
BEGIN | |
SET @order = _order; | |
SELECT ID, TITLE, SUMMARY FROM articles a ORDER BY | |
CASE WHEN _order = 'SUMMARY ASC' THEN SUMMARY END ASC, | |
CASE WHEN _order = 'ID ASC' THEN ID END ASC, | |
CASE WHEN _order = 'TITLE ASC' THEN TITLE END ASC, | |
CASE WHEN _order = 'SUMMARY DESC' THEN SUMMARY END DESC, | |
CASE WHEN _order = 'ID DESC' THEN ID END DESC, | |
CASE WHEN _order = 'TITLE DESC' THEN TITLE END DESC | |
; | |
END; | |
%%% | |
@delimiter ; | |
%%% |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment