views:

40

answers:

1

Hello,

I have list of DateTime values, and for each value I need to fetch something from the database. I would like to do this with one query. I know it's possible to pass a table (list) to the stored procedure, but Im not sure how to write the query itself.

Let's say I have the following table:

CREATE TABLE Shows(
    ShowId [int] NOT NULL, 
    StartTime DateTime NOT NULL, 
    EndTime DateTime NOT NULL 
)

and an array of dates

DECLARE @myDateArray MyCustomDateArrayType

Now, if I were fetching a single item, I would write a query like this:

SELECT * FROM Shows
WHERE StartTime > @ArrayItem and @ArrayItem < EndTime

where @ArrayItem is an item from @myDateArray .

But how do I formulate the query that would fetch the information for all array items?

Thanks!

+2  A: 

This should do it:

SELECT s.* 
FROM Shows s
    JOIN @MyDateArray t ON s.StartTime > t.TableVarDateField 
        AND t.TableVarDateField < s.EndTime
AdaTheDev
How do you know he is using SQL Server?
Oded
@Oded - I may be wrong, but it sounds very much like OP is talking about Table Valued Parameter suport in SQL Server 2008 - I don't know of mySQL or Oracle supporting the same in the manner they've described (but I'm by no means an expert on those).
AdaTheDev
Yes, I'm using SQL server.Anyway, answer suggested by AdaTheDev works great! I didn't realize that was SQL Server related syntax...Thanks guys!