Καλώς ορίσατε στο dotNETZone.gr - Σύνδεση | Εγγραφή | Βοήθεια
σε

 

Αρχική σελίδα Ιστολόγια Συζητήσεις Εκθέσεις Φωτογραφιών Αρχειοθήκες

Dynamic SQL WHERE clauses using conditional operators

Îåêßíçóå áðü ôï ìÝëïò cap. Τελευταία δημοσίευση από το μέλος cap στις 14-01-2005, 18:59. Υπάρχουν 3 απαντήσεις.
Ταξινόμηση Δημοσιεύσεων: Προηγούμενο Επόμενο
  •  14-01-2005, 14:14 862

    Dynamic SQL WHERE clauses using conditional operators

    Ειναι ενα αρθράκι που είχα στείλει παλιότερα στο SqlServerCentral.com και αποτέλεσε και θέμα συζήτησης. Ειναι ενδιαφέρον, τόσο το άρθρο όσο και η συζήτηση που ακολούθησε.

    http://www.sqlservercentral.com/columnists/SFilippidis/conditionalstatementsinwhereclauses.asp


    Σωτήρης Φιλιππίδης

    DotSee Web Services

    View Sotiris Filippidis's profile on LinkedIn

    DotNetNuke them!
  •  14-01-2005, 17:51 864 σε απάντηση της 862

    Smile [:)] Re: Dynamic SQL WHERE clauses using conditional operators

    Καλώς ήλθες,

    Αν θες δημοσίευσε όλο το άρθρο εδώ. Το SQLCentral έχει την περιέργεια να θέλει να έχουν γίνει όλοι registered εκεί και δυστηχώς η διαδικασία τους δεν δουλεύει αυτοματοποιημένα/γρήγορα... Θέλει 2-3 μέρες να γίνεις registered και να μπορέσεις να το δεις...

    G.J.


    George J. Capnias: Χειροπρακτικός Υπολογιστών, Ύψιστος Γκουράρχης της Κουμπουτερολογίας
    w: capnias.org, t: @gcapnias, l: gr.linkedin.com/in/gcapnias
    dotNETZone.gr News
  •  14-01-2005, 18:55 868 σε απάντηση της 862

    Re: Dynamic SQL WHERE clauses using conditional operators

    ΠΟΛΥ ΚΑΛΗ ΙΔΕΑ! Και το ωραίο είναι ότι ο optimizer θα εξαφανίσει όλα αυτά τα case πριν τρέξει το query! Μόνο που για να δουλέψει καλά θέλει ένα SQL Optimizer τόσο καλό όσο αυτό του SQL Server. Δεν ξέρω αν θα έπαιζε π.χ. σε Oracle. Devil

    Το ίδιο κόλπο μπορείς να κάνεις και με το UPDATE για να αποθηκεύσεις μεταβλητό αριθμό πεδίων. Και πολύ το γουστάρω το CASE WHEN!

    Για όσους δεν έχουν συνδρομή στο SqlServerCentral, ιδού το stored procedure:

    -- Declare some local variables. Actually, we are creating a pair of variables
    -- for each column included in our WHERE clause.
    -- The first variable represents the value we are filtering and the second
    -- represents the "operator" for the filter.

    declare @companyName varchar(255)
    declare @companyNameOp varchar(2)
    declare @country varchar(255)
    declare @countryOp varchar(2)

    -- Let's set some sample values now. The values you see here represent the second
    -- of the two scenarios described above, i.e. all records for companies located in Germany,
    -- excluding companies starting with the letter A

    -- Operators are defined here with arbitrary, two-letter values.
    -- Of course you could define your own set of operators, with different
    -- naming conventions. For our example, here's the meaning of each possible
    -- value:

    -- ne = not equal
    -- eq = equal
    -- bg = begins with
    -- ed = ends with
    -- ct = contains

    -- For our example, we are using only varchar fields in our WHERE clause.
    -- It is very easy, though, to define operators for other data types as well.


    set @companyname = 'A%'
    set @companynameOp = 'ne'
    set @country = 'Germany'
    set @countryOp = 'eq'



    -- Ok, now let's form our query.

    select
         customerid, companyname, country
    from
         customers
    where
         case @companyNameOp
              when '' then 1 -- Operator not defined, get everything
              when 'eq' then -- Operator is "equals"
                   case when companyname like @companyName then 1 else 0 end
              when 'bg' then -- Operator is "begins with"
                   case when companyname like @companyName +'%' then 1 else 0 end
              when 'ed' then -- Operator is "ends with"
                   case when companyname like '%' + @companyName  then 1 else 0 end
              when 'ct' then -- Operator is "contains"
                   case when companyname like '%' + @companyName  +'%' then
    1 else 0 end
              when 'ne' then -- Operator is "not equal"
                   case when companyname not like @companyName then 1 else 0 end end =1

    AND

    -- Same approach for the second field

          case @countryOp
               when '' then 1
               when 'eq' then
                    case when country like @country then 1 else 0 end
               when 'bg' then
                    case when country like @country +'%' then 1 else 0 end
               when 'ed' then
                    case when country like '%' + @country  then 1 else 0 end
               when 'ct' then
                    case when country like '%' + @country  +'%' then 1 else 0 end
               when 'ne' then
                    case when country not like @country then 1 else 0 end
          end =1


    Παναγιώτης Καναβός, Freelancer
    Twitter: http://www.twitter.com/pkanavos
  •  14-01-2005, 18:59 869 σε απάντηση της 864

    Re: Dynamic SQL WHERE clauses using conditional operators

    Sorry Γιώργο, δεν το πήρα χαμπάρι οτι το δίνανε μόνο στους registered. (Ειχα auto login και το παρέλειψα). Μια και δοθηκε όμως τελικά, δεν το ξαναστέλνω.
    Σωτήρης Φιλιππίδης

    DotSee Web Services

    View Sotiris Filippidis's profile on LinkedIn

    DotNetNuke them!
Προβολή Τροφοδοσίας RSS με μορφή XML
Με χρήση του Community Server (Commercial Edition), από την Telligent Systems