troubleshooting Question

SQL - Stored Procedure Loop

Avatar of Endelm
Endelm asked on
Microsoft SQL ServerMicrosoft SQL Server 2005SQL
4 Comments3 Solutions2265 ViewsLast Modified:
Hi,

I'm using Miscrosoft SQL 2005.

I have a StoredProcedure. Each time I run this StoredProcedure  it takes a DateTime input:
exec MySP '2007-07-23'

...and creates a View
select ViewField from MyView

I have a query that returns me a list of DateTime fields
select MyDTField
from MyTable
order my MyDTField desc

2007-07-20 08:53:46.120
2007-07-21 00:00:00.000
2007-07-22 00:00:00.000

How can I loop throught all the datetime fields in MyTable query, and for each datetime value run the StoredProcedure MySP and insert the datetime value and the result from MyView in a table?

Code:
Foreach (datetime in MyTableQuery)
{
exec MySP 'datetime'      

select ViewField from MyView

insert into TmpTable(datetimeField, ViewField)
values('datetime', ViewField)
}

How is this possable in SQL?

Thank you.
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 3 Answers and 4 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 3 Answers and 4 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros