If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > ASP > Where In () values generated dynamically

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 12-28-09, 01:31
probelaw probelaw is offline
Registered User
 
Join Date: Jul 2004
Posts: 44
Where In () values generated dynamically

I need to design a query using where in () to select from a list of multiple values...

SELECT EmployeeID, EmployeeName from EMPLOYEES WHERE OFFICE_ID in (1,5,7)

simple enough.

However, the list of OFFICE_IDs in the where clause needs to be generated dynamically in my asp script.

Can't seem to get this to work using Createparameter. The OFFICE_ID values are int. How can I create a parameter of multiple int values?

Any help would be appreciated.
Reply With Quote
  #2 (permalink)  
Old 12-28-09, 13:11
scooby_at_work scooby_at_work is offline
Registered User
 
Join Date: Sep 2009
Posts: 44
Code:
The OFFICE_ID values are int. How can I create a parameter of multiple int values?
I'm guessing you have an array of values.

I'd strongly recommend scanning it to be sure they're actually all integers. This code isn't VBScript but regular VB as I'm not an ASP guy. If you're using ASP.NET (it does help to mention what language you're using) just make sure you type your arrays.

Code:
    Dim a(1 To 3)
    a(1) = 5
    a(2) = 89
    a(3) = "error"
    For Each i In a
        x = CInt(i) ' You don't have to do anything with x
    Next
    If UBound(a) - LBound(a) < 1 Then
        inexpr = "1 = 1"    ' Handle empty arrays correctly
    Else
        inexpr = "IN (" + Join(a, ", ") + ")"
    End If
That code will fail when it tries to CInt the string. I'd recommend always checking this stuff before sending it to the DBMS. If the check is taking up too much time, you'll see that when you profile and you can easily remove it, but this is no different than the checks CreateParameter has to do.

To use it, you insert the value of inexpr in your SQL. It is, theoretically, possible to use parameters, but that's just another means of validating your inputs.
Reply With Quote
  #3 (permalink)  
Old 12-28-09, 14:08
probelaw probelaw is offline
Registered User
 
Join Date: Jul 2004
Posts: 44
The values are coming from an array and the values are int, that is verified earlier in the code.

I know I could dynamically generate the SQL statement, just wondering if there was a way to do it through createparameter.
Reply With Quote
  #4 (permalink)  
Old 12-30-09, 06:20
gvee gvee is offline
www.gvee.co.uk
 
Join Date: Jan 2007
Location: UK
Posts: 10,156
What database are you using?
__________________
George
Twitter | Blog
Reply With Quote
  #5 (permalink)  
Old 12-30-09, 09:42
probelaw probelaw is offline
Registered User
 
Join Date: Jul 2004
Posts: 44
SQL Server 2008
Reply With Quote
  #6 (permalink)  
Old 12-30-09, 10:03
gvee gvee is offline
www.gvee.co.uk
 
Join Date: Jan 2007
Location: UK
Posts: 10,156
Fab-u-lous!!

Take a look at table valued parameters: SQL Server 2008: Table Valued Parameters - SQLTeam.com
__________________
George
Twitter | Blog
Reply With Quote
  #7 (permalink)  
Old 01-09-10, 19:45
SimonMT SimonMT is offline
Registered User
 
Join Date: Sep 2006
Posts: 265
You can use array by constructing Request.Querystrings

WHERE OriginalsQuery.[Orig Old Stock] in ("& Request.Querystring("Stock")&")

String and Numeric values are only comma separated (no quotes)

Simon
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On