Pagina 1 di 2 1 2 ultimoultimo
Visualizzazione dei risultati da 1 a 10 su 13
  1. #1

    [SQL-Server] when case e ORDER BY

    codice:
    	order by
    		case @P_ORDER_BY
    		when  1 then Nome ASC 
    		when  2 then Nome DESC
    		when  3 then Cognome ASC
    		when  4 then Cognome DESC
    		when  5 then Tipologia ASC
    		when  6 then Tipologia DESC
    		when  7 then Stato ASC
    		when  8 then Stato DESC
    		when  9 then DataInserimento ASC
    		when  10 then DataInserimento DESC
    		when  11 then Profilo ASC
    		when  12 then Profilo DESC
    		else Utenti.Id
    		end
    come posso far funzionare questo order by dinamico?
    "0 è tutto finito. 1 è solo l'inizio"
    HO IL CERTIFICATO DI RESISTENZA.

  2. #2
    Utente di HTML.it L'avatar di Gioba66
    Registrato dal
    Jun 2002
    Messaggi
    2,189
    non puoi mettrlo così

    ricavati la stringa 'order by qualchecosa' prima di fare la select
    e poi la accodi alla select
    Tutti vogliono parlare, nessuno sa ascoltare.

  3. #3

    no fatto così

    codice:
    order by
    	case @P_ORDER_BY
    	when  1 then Nome 
    	when  3 then Cognome 
    	when  5 then Tipologia 
    	when  7 then Stato 
    	when  9 then DataInserimento
    	when  11 then Profilo 
    	else null
    	end asc,	
    	
            case @P_ORDER_BY
    	when  2 then Nome
    	when  4 then Cognome 
    	when  6 then Tipologia 
    	when  8 then Stato
    	when  10 then DataInserimento
    	when  12 then Profilo
    	else null
    	end desc
    però ho un problema, quando ordino per DataInserimento (casi 9 e 10) ottengo un errore di questo tipo:
    Syntax error converting character string to smalldatetime data type.

    nb: nella select recupero il campo DataInserimento così(per normalizzarlo in gg/mm/yyyy):
    convert(varchar, DataInserimento,103) as DataInserimento

    grazie dell'aiuto
    "0 è tutto finito. 1 è solo l'inizio"
    HO IL CERTIFICATO DI RESISTENZA.

  4. #4
    Utente di HTML.it L'avatar di Gioba66
    Registrato dal
    Jun 2002
    Messaggi
    2,189
    ma l'errore è nella select o nell'order by?

    prova a levare tutto il pezzo di order by e vedi se intanto quello non dà erroe
    Tutti vogliono parlare, nessuno sa ascoltare.

  5. #5
    codice:
    order by
    	case @P_ORDER_BY
    	when  1 then Nome 
    	when  3 then Cognome 
    	when  5 then Tipologia 
    	when  7 then Stato 
    	--when  9 then DataInserimento
    	when  11 then Profilo 
    	else null
    	end asc,	
    	
            case @P_ORDER_BY
    	when  2 then Nome
    	when  4 then Cognome 
    	when  6 then Tipologia 
    	when  8 then Stato
    	--when  10 then DataInserimento
    	when  12 then Profilo
    	else null
    	end desc
    l'errore è nell'order by, perchè se commento i due case (vedi sopra) che interessano la data tutto funziona.
    penso che l'RDBMS tenti di convertire in smalldatetime il campo DataInserimento che ho convertito in varchar, ma non ci riesce.
    è una cosa un po' assurda ma è così. non riesco ad ordinare per data se lo stesso campo nella select lo converto per leggibilità in varchar. :master:
    "0 è tutto finito. 1 è solo l'inizio"
    HO IL CERTIFICATO DI RESISTENZA.

  6. #6
    io porterei questa logica fuori dalla SP...

  7. #7
    Utente di HTML.it L'avatar di Gioba66
    Registrato dal
    Jun 2002
    Messaggi
    2,189
    allora metti


    when 10 then convert(varchar, DataInserimento,103)
    Tutti vogliono parlare, nessuno sa ascoltare.

  8. #8
    Originariamente inviato da optime
    io porterei questa logica fuori dalla SP...
    in che senso? :master:

    Originariamente inviato da Gioba66
    allora metti

    when 10 then convert(varchar, DataInserimento,103)
    si ma così non mi da errore, ma mi converte la data in stringa e ad esempio 27/12/2004 risulterà maggiore di 25/01/2005 !
    ho provato a fare anche questa conversione ma ritorna errore:
    when 10 then convert(datetime, DataInserimento,103)

    "0 è tutto finito. 1 è solo l'inizio"
    HO IL CERTIFICATO DI RESISTENZA.

  9. #9
    nel senso che la order by andrebbe inviata come parametro esplicito alla SP

  10. #10
    pare che sql server abbia dei grossi problemi con order by dinamici e case
    http://sqlteam.com/forums/topic.asp?TOPIC_ID=5942

    VVoVe:
    "0 è tutto finito. 1 è solo l'inizio"
    HO IL CERTIFICATO DI RESISTENZA.

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.