Professional Documents
Culture Documents
Questions
Tags
Users
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no
registration required.
Badges
search
Unanswered
Ask Question
1 month ago
16
Then i want to FOR EACH through the numbers and run the SQL Query.
Something like (pseudocode):List = 1/1/2010,2/1/2010,3/1/2010,4/1/2010,5/1/2010
For each x in List
do
DECLARE @MyVar datetime = x
Linked
2 transfer data from one table
SELECT @MyVar
Related
497 How to perform an IF
edit
I have a large query that accepts a 'to date' parameter, I need to run it 24 times, each time with a
specific to date which I need to be able to supply (these dates are going to be dynamic) I want to avoid
repeating my query 24 times with union alls joining them as if I need to come back and add additional
columns it would be very time consuming.
sql
sql-server
tsql
Server
36
77
Server
in SQL Server
first row
converted by W eb2PDFConvert.com
5 Can you explain why you need to do this? 95% of the time when you need a looping structure in tSQL you
are probably doing it wrong. JohnFx Apr 24 '12 at 14:53
2 Why not create a table where you can populate the dates you want to run it against that. There is almost
certainly a better way to do this than looking through hardcoded constant values. JohnFx Apr 24 '12 at
14:57
The dates in your example are sequential by month. Is that a rule, or do you need to be able to run for an
arbitrary set of dates? Is there a reason that you can't edit the large query to take a date range or set of dates
rather than a single date? If you absolutely need to step through iterations (against the good advice given
above), then you might want to consider using a cursor. Jason Quinones Apr 24 '12 at 22:36
6 Answers
active
oldest
votes
SQL is primarily a set-orientated language - it's generally a bad idea to use a loop in it.
23
with cte as
(select 1 i union all
select i+1 i from cte where i < 5)
select dateadd(d, i-1, '2010-01-01') from cte
15
42
converted by W eb2PDFConvert.com
19
You should tell us what is your main goal, as was said by @JohnFx, this could probably be done
another (more efficient) way.
share improve this answer
38
59
I need to loop dates not integers, see edited question. Would a WHILE loop work with dates ?
SelectDistinct Apr 24 '12 at 15:12
@SelectDistinct - Yes, it should work just fine. I changed my answer to returns dates instead of ints
Lamak Apr 24 '12 at 15:22
1 Mark Bannister's answer is less code and more efficient. There's no reason to use a while loop for something
like this. Sorpigal Apr 24 '12 at 16:41
This kind of depends on what you want to do with the results. If you're just after the numbers, a setbased option would be a numbers table - which comes in handy for all sorts of things.
For MSSQL 2005+, you can use a recursive CTE to generate a numbers table inline:
;WITH Numbers (N) AS (
SELECT 1 UNION ALL
SELECT 1 + N FROM Numbers WHERE N < 500
)
SELECT N FROM Numbers
OPTION (MAXRECURSION 500)
69
116
converted by W eb2PDFConvert.com
Interesting that you consider this to be a "set-based option" (the word RECURSION is a bit of a give away!)
onedaywhen Apr 25 '12 at 8:17
@onedaywhen - there's no contradiction there. A recursive CTE is set-based. The anchor member (SELECT 1)
is set S[0], which is then UNION ALL'ed with n more sets (SELECT 1 + N FROM S[n - 1]) until an empty set
is encountered. The result is the UNION of sets S[0] to S[n]. That being said - I personally prefer a physical
table as it's more efficient. Mark Brackett Apr 25 '12 at 13:52
Joe Celko's thoughts on the matter: "it makes the semi-procedural programmer feel good to use [a recursive]
CTE... But recursion is actually a procedural technique. It is also expensive since it is really a cursor under
the covers" -- I'm not saying he is correct but demonstrates the position isn't as clear cut as you would have it.
Not a criticism of your comments either. As I said, I find it interesting without holding strong views on the
subject myself :) onedaywhen Apr 26 '12 at 7:26
@onedaywhen - And here, Celko makes a distinction between "procedural code...a procedure or a cursor" and
a recursive CTE, which "[is] still procedural under the covers". I don't doubt that recursive CTEs are
procedurally executed...but so are JOINs. At least we can agree that a numbers table is a better option.
Mark Brackett Apr 26 '12 at 14:43
13
29
13
19
33
converted by W eb2PDFConvert.com
AS
BEGIN
-- variable for storing start date
Declare @StartYear as int
-- Variable for the End date
Declare @EndYear as int
-- Setting the value in strat Date
select @StartYear = Value from rat.Configuration where Name = 'REPORT_START_YEAR';
-- Setting the End date
select @EndYear = Value from
18
88
29
Your Answer
Sign up or log in
Sign up using Google
Sign up using Facebook
Sign up using Stack Exchange
Post as a guest
Name
Email
required, but never shown
converted by W eb2PDFConvert.com
Not the answer you're looking for? Browse other questions tagged sql
question.
sql-server
tsql
question feed
tour help blog chat data legal privacy policy work here advertising info mobile contact us feedback
TECHNOLOGY
Stack Overflow
Programmers
Server Fault
Super User
Web Applications
Ask Ubuntu
WordPress
Development
Webmasters
Geographic
Information Systems
Game Development
Electrical Engineering
TeX - LaTeX
Android Enthusiasts
Information Security
Database
Administrators
LIFE / ARTS
CULTURE /
RECREATION
SCIENCE
OTHER
Photography
Mathematics
Stack Apps
Drupal Answers
SharePoint
Graphic Design
Mi Yodeya (Judaism)
Theoretical Computer
Science
User Experience
Seasoned Advice
(cooking)
Travel
Physics
Christianity
MathOverflow
Arqade (gaming)
more (7)
Mathematica
Salesforce
more (14)
Home Improvement
Personal Finance &
Money
Bicycles
Academia
Role-playing Games
more (10)
more (21)
site design / logo 2015 stack exchange inc; user contributions licensed under cc by-sa 3.0 with attribution required
rev 2015.4.22.2509
Skeptics