PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
[RESOLVED] Query and Array-VBForums
Results 1 to 4 of 4

Thread: [RESOLVED] Query and Array

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Nov 2004
    Posts
    1,308

    Resolved [RESOLVED] Query and Array

    Hi to all,

    I had write this little query, that work, but i don't know if this is the best way to do.
    The query call a Variable that is an array, with dimension with 11 numbers.

    Any other way to do this?:

    Code:
     SQL = "select * from orcamento2 " & _
                    " where registo = " & registos(0) & " " & _
                    " or registo = " & registos(1) & " " & _
                    " or registo = " & registos(2) & " " & _
                    " or registo = " & registos(3) & " " & _
                    " or registo = " & registos(4) & " " & _
                    " or registo = " & registos(5) & " " & _
                    " or registo = " & registos(6) & " " & _
                    " or registo = " & registos(7) & " " & _
                    " or registo = " & registos(8) & " " & _
                    " or registo = " & registos(9) & " " & _
                    " or registo = " & registos(10) & " " & _
                    " and encomenda = 'Sim' " & _
                    " order by sala, sub_titulo, linha "

    Thanks

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,028

    Re: Query and Array

    The standard SQL alternative to the collection of Or's on the same field is to use In(), eg:
    Code:
    " where registo IN (" & registos(0) & ", " & registos(1) & ", " ... & registos(10) & ")" & _

  3. #3
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    102,598

    Re: Query and Array

    Based on your previous threads, you appear to be using VB.NET. In that case, you can use String.Join to make your code a bit more succinct:
    vb.net Code:
    1. sql = String.Format("SELECT * FROM orcamento2 WHERE registo IN ({0})", String.Join(", ", registos))
    That's just about OK in this case because the values appear to be numbers, so there's no real danger of SQL injection. It is generally preferable to use parameters though. To learn how to construct a query with parameters and an IN clause, follow the CodeBank link in my signature below and check out my thread on the subject.

  4. #4

    Thread Starter
    Frenzied Member
    Join Date
    Nov 2004
    Posts
    1,308

    Re: Query and Array

    Thanks Jmcilhinney

    In fact is a good idea... In that way all the values are in

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width