Search EggHeadCafe's Job Board
EggHeadCafe Silverlight WPF ASP.NET VB.NET C# Excel SQL Server SharePoint
search
SQL Server GroupsView
SQL Server Ce
SQL Server Clients
SQL Server Connect
SQL Server Datamining
SQL Server Datawarehouse
SQL Server Dts
SQL Server Fulltext
SQL Server Msde
SQL Server Olap
SQL Server Replication
SQL Server Reportingsvcs
SQL Server Security
SQL Server Server
SQL Server Setup
SQL Server Tools
SQL Server Clustering
SQL Server Programming
SQL Server Xml
SQL Server Newusers
SQL Server Integrationsvcs

Group SummariesView
.NET Framework
Access
BizTalk
Certifications
CRM
DDK
Exchange Server
FoxPro
French
French .NET
Games
German
German .NET
Graphic Design
IIS
Internet
ISA Server
Italian
Italian .NET
Maps
MCIS
Miscellaneous
Mobile Apps
Money
MSN
Networking
Office
Ops Mgr
Publisher
Security
SharePoint
Small Business
Spanish
Spanish .NET
SQL Server
Systems Management Server
Transaction Server
Virtual PC / Virtual Server
Visual Studio
Win32
Windows 2000
Windows 2003 Server
Windows 7
Windows Live
Windows Media
Windows Update
Windows Vista
Windows XP
 

View All Microsoft SQL Server Programming Posts  Ask A New Question 

Nice example Eric!

TheSQLGuru posted on Tuesday, August 19, 2008 2:42 PM

Nice example Eric!  ;-))

--
Kevin G. Boles
Indicium Resources, Inc.
SQL Server MVP
kgboles a earthlink dt net



Yes there are Loops in SQL Server, but Celko doesn't like them.
Here's a quick example...

WHILE @CelkoIsBeingDifficult
BEGIN --LOOP
TRY
SET @Him = @Straight
CATCH
@Him = @DoingItAgainLater
END TRY
END --Less LOOP


Here's one way to do it...

SET @EndMonth = MONTH(@EndDate)
SET @Month = MONTH(@StartDate)

WHILE @Month <= @EndMonth
BEGIN
--Process data for the month

SET @Month = @Month + 1
END

-Eric Isaacs
reply

 

The example I was referring to had nothing to do with cursors.

TheSQLGuru posted on Tuesday, August 19, 2008 3:47 PM

The example I was referring to had nothing to do with cursors.  It dealt
with your recurrent cantankerousness in a rather funny way.

--
Kevin G. Boles
Indicium Resources, Inc.
SQL Server MVP
kgboles a earthlink dt net
reply

How to loop a month at a time between two dates

Robin9876 posted on Thursday, August 21, 2008 11:08 PM

In a SQL 2005 SP that has two parameters a start and end date.

How can you loop through these dates a month at a time to process data
for that month period?
reply

We don't have loops in SQL; this is a declarative language, so we doit all in

--CELKO-- posted on Thursday, August 21, 2008 11:08 PM

We don't have loops in SQL; this is a declarative language, so we do
it all in one statement on the entire set.  If you can explain what

SELECT ..
FROM ..
WHERE something_date BETWEEN @vague_start_date AND@vague_end_date
AND..;
reply

Yes there are Loops in SQL Server, but Celko doesn't like them.

Eric Isaacs posted on Thursday, August 21, 2008 11:08 PM

Yes there are Loops in SQL Server, but Celko doesn't like them.
Here's a quick example...

WHILE @CelkoIsBeingDifficult
BEGIN --LOOP
TRY
SET @Him =3D @Straight
CATCH
@Him =3D @DoingItAgainLater
END TRY
END --Less LOOP


Here's one way to do it...

SET @EndMonth =3D MONTH(@EndDate)
SET @Month =3D MONTH(@StartDate)

WHILE @Month <=3D @EndMonth
BEGIN
--Process data for the month

SET @Month =3D @Month + 1
END

-Eric Isaacs
reply

Seriously, Celko is right from the standpoint that you should beavoiding loops

Eric Isaacs posted on Thursday, August 21, 2008 11:08 PM

Seriously, Celko is right from the standpoint that you should be
avoiding loops in your SQL.  Just because they can be done, doesn't
mean they should be done.  You should always try to process the data
in a single bulk SQL statement like he suggested.  If that isn't
possible, a loop is possible.

-Eric Isaacs
reply

After 30 years with SQL, I think it is always possible to avoidloops.

--CELKO-- posted on Thursday, August 21, 2008 11:08 PM

After 30 years with SQL, I think it is always possible to avoid
loops.

Actually, I knew it was always possible because of some theorem that
showed a push-down stack automata (procedural language) was
computationally equivalent to primitive recursive functions
(declarative language).  But it was not worth it.  Hey, my first
Masters degree was Math and my second was Comp Sci!

After we got CASE expressions, the OLAP functions and windows clause
on aggregates, the only problem I can think of for doing in loops is a
return the first answer you find that is acceptable (i.e. cost < k)
without looking further.
reply

Do you really think that will run faster and port easier than a singelSELECT

--CELKO-- posted on Thursday, August 21, 2008 11:08 PM

Do you really think that will run faster and port easier than a singel
SELECT or UPDATE statement?  Of course not!

Let's see if Robin9876 will actually post his problem so we can code
it...
reply

How to loop a month at a time between two dates

Robin9876 posted on Thursday, August 21, 2008 11:08 PM

t

Ok, I have changed the scenario to a sales ordering system the data
requested may seem strange but is the type of information required.

For each Sales Person, in user defined date range, by month I need the
number of orders placed, number of orders invoiced and at the end of
each month period how many customers last order was yet to be
despatched.
reply

Robin,Can you include more details about the tables involved?

Eric Isaacs posted on Thursday, August 21, 2008 11:09 PM

Robin,

Can you include more details about the tables involved?  Maybe some
CREATE scripts for those tables and definitions of the relationships
between the tables?  Otherwise we'll just be guessing what your schema
looks like.

Thanks,

-Eric Isaacs
reply

Please post DDL, so that people do not have to guess what the

--CELKO-- posted on Thursday, August 21, 2008 11:09 PM

Please post DDL, so that people do not have to guess what the keys,
constraints, Declarative Referential Integrity, data types, etc. in
your schema are. If you know how, follow ISO-11179 data element naming
conventions and formatting rules.  Sample data is also a good idea,
along with clear specifications.  It is very hard to debug code when
you do not let us see it.  If you want to learn how to ask a question
on a Newsgroup, look at:  http://www.catb.org/~esr/faqs/smart-questions.html
reply