Visualizzazione dei risultati da 1 a 10 su 10
  1. #1

    where con if interni mssql

    Salve,
    io dovrei modificare questo select inserendo delle condizioni all'interno: nel caso in cui le variabili "livello;regione;universita;facolta" sono nulle o "" non deve calcolare quel campo.

    sSql = "SELECT *, convert(varchar(2),Giorno) + '/' + convert(varchar(2),Mese) + '/' + convert(varchar(4),Anno) AS DataMatta FROM master WHERE (livello='"&livello&"' and regione='"&regione&"' and universita='"&universita&"' and facolta='"&facolta&"') ORDER BY id DESC"

    Grazie mille.

  2. #2
    ragiona così: se il campo contiene qualcosa lo aggiungo, altrimenti no

    SQL = "SELECT .... WHERE 1=1 "
    If Campo1<>"" THEN
    SQL=SQL+" AND campo1=" & campo1
    End If

    e così via


  3. #3
    io ho fatto così:


    sSql = "SELECT *, convert(varchar(2),Giorno) + '/' + convert(varchar(2),Mese) + '/' + convert(varchar(4),Anno) AS DataMatta FROM master WHERE (1=1 "
    if livello<>"" or livello<>NULL then
    sSql = sSql + " AND livello="&livello
    end if
    if regione<>"" or regione<>NULL then
    sSql = sSql + " AND regione="&regione
    end if
    if universita<>"" or universita<>NULL then
    sSql = sSql + " AND universita="&universita
    end if
    if facolta<>"" or facolta<>NULL then
    sSql = sSql + " AND facolta="&facolta
    end if

    sSql = sSql + ") ORDER BY id DESC"



    ma mi da questo errore dove c'è livello:

    Microsoft][ODBC SQL Server Driver][SQL Server]Invalid column name 'secondo'.

  4. #4
    ovviamente devi rispettare gli apici. io ho postato solo un esempio, adattalo

  5. #5
    e poi queste

    if regione<>"" or regione<>NULL then

    sono sbagliate. se ci ragioni ti accorgi che ci vuole AND

  6. #6
    HO corretto così...non mi da errori ma cmq mi da sempre tutti i record del database senza pensare a nessun where.

    sSql = "SELECT *, convert(varchar(2),Giorno) + '/' + convert(varchar(2),Mese) + '/' + convert(varchar(4),Anno) AS DataMatta FROM master WHERE (1=1 "
    if livello<>"" and livello<>NULL then
    sSql = sSql + " AND livello='"&livello
    end if
    if regione<>"" and regione<>NULL then
    sSql = sSql + " AND regione='"&regione
    end if
    if universita<>"" and universita<>NULL then
    sSql = sSql + " AND universita='"&universita
    end if
    if facolta<>"" and facolta<>NULL then
    sSql = sSql + " AND facolta='"&facolta
    end if

    sSql = sSql + ") ORDER BY id DESC"

  7. #7
    matta, funzioni a rate? gli apici aperti vanno chiusi.

  8. #8
    sSql = "SELECT *, convert(varchar(2),Giorno) + '/' + convert(varchar(2),Mese) + '/' + convert(varchar(4),Anno) AS DataMatta FROM master WHERE (1=1 "
    if livello<>"" and livello<>NULL then
    sSql = sSql + " AND livello='"&livello&"'"
    end if
    if regione<>"" and regione<>NULL then
    sSql = sSql + " AND regione='"&regione&"'"
    end if
    if universita<>"" and universita<>NULL then
    sSql = sSql + " AND universita='"&universita&"'"
    end if
    if facolta<>"" and facolta<>NULL then
    sSql = sSql + " AND facolta='"&facolta&"'"
    end if

    sSql = sSql + ") ORDER BY id DESC"


    sempre la stessa cosa...

  9. #9
    Ho corretto così e ora sembra che funge:



    sSql = "SELECT *, convert(varchar(2),Giorno) + '/' + convert(varchar(2),Mese) + '/' + convert(varchar(4),Anno) AS DataMatta FROM master WHERE (1=1 "
    if livello<>"" then
    sSql = sSql + " AND livello='"&livello&"'"
    end if
    if regione<>"" then
    sSql = sSql + " AND regione='"&regione&"'"
    end if
    if universita<>"" then
    sSql = sSql + " AND universita='"&universita&"'"
    end if
    if facolta<>"" then
    sSql = sSql + " AND facolta='"&facolta&"'"
    end if

    sSql = sSql + ") ORDER BY id DESC"

  10. #10
    ----------------------------------------------------
    impariamo a fare un po' di debug

    *subito* dopo la composizione della stringa SQL metti

    Response.Write "maGGica SQL...: " & sql
    Response.Flush

    poi posta quello che viene a video
    ----------------------------------------------------

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.