Getting Started with Adobe After Effects - Part 6: Motion Blur


Upload Image Close it
Select File

Browse by Tags · View All
SQL server 7
Database 6
Tips 5
Tricks 5
#SQLServer 3
Backup 2
MSSQL 1
'E0' 1
isnumeric 1
Issues 1

Archive · View All
December 2011 4
March 2012 2
January 2012 1

Raghunath Bhandari's Blog

Dynamic Order by issue

Mar 11 2012 9:48PM by Raghunath Bhandari   

Today one of my friend ask me, can we use Order by clause dynamically ? I said yes, but there are few limitations while using case in order by clause see the below example.


This script will return Error:


DECLARE @TOP INT = 10, @FLD VARCHAR(20)='AGENT_ID'

SELECT
    TOP(@TOP)
    *
FROM
(
    SELECT 1 AS AGENT_ID, 'RAM' AS AGENT_NAME
    UNION ALL
    SELECT 2, 'SHYAM'
    UNION ALL
    SELECT 3, 'HARI'
)A
ORDER BY
   CASE WHEN @FLD ='AGENT_ID' THEN AGENT_ID
   WHEN @FLD = 'AGENT_NAME' THEN AGENT_NAME END
DESC

/* 
    Msg 245, Level 16, State 1, Line 7
    Conversion failed when converting the varchar value 'RAM' to data type int.
*/

 

This Will not return Error:

 

DECLARE @TOP INT = 10, @FLD VARCHAR(20)='AGENT_ID'

SELECT
    TOP(@TOP)
    *
FROM
(
    SELECT 1 AS AGENT_ID, 'RAM' AS AGENT_NAME
    UNION ALL
    SELECT 2, 'SHYAM'
    UNION ALL
    SELECT 3, 'HARI'
)A
ORDER BY
CASE WHEN @FLD ='AGENT_ID' THEN AGENT_ID END DESC,
CASE WHEN @FLD = 'AGENT_NAME' THEN AGENT_NAME END DESC


The Problem or limitation is Order by clause wants same data type in Each Case statement so we have to use multiple case in each data type group fields.

 

Tags: Database, SQL server, Tricks, Tips, MSSQ, Raghu, Dynamic Order by issue,


Raghunath Bhandari
183 · 1% · 253
4
 
0
Lifesaver
 
0
Refreshed
 
0
Learned
 
0
Incorrect



Submit

Your Comment


Sign Up or Login to post a comment.

"Dynamic Order by issue" rated 5 out of 5 by 4 readers
Dynamic Order by issue , 5.0 out of 5 based on 4 ratings
    Copyright © Rivera Informatic Private Ltd Contact us      Privacy Policy      Terms of use      Report Abuse      Advertising      [ZULU1097]