473,651 Members | 2,670 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Whats wrong with this nested while loop?

Jim
Im getting way too many rows retured..what its trying to do is insert
a 0 for revenue for months 7 - 12 (aka July through December) for each
of these cost centers for each payor type..Im getting a lot of repeats
and the concatenation field date always comes back as January 2003
instead of the month and date its supposed to


--Fiscal Year
declare @year smallint
set @year = 2004

--Month number the Fiscal year starts and ends
declare @month smallint
set @month = 7

--Place holder for number of costcenters
declare @cccounter smallint

--loop counter for cost centers
declare @ccount smallint
set @ccount = 1

--Place holder for number of payor types
declare @ptcounter smallint

--loop counter for payor types
declare @pcount smallint
set @pcount = 1

--Temp table to store the blank values for all cost centers/payor
types for the fiscal year
declare @Recorded_Reven ue_tmp table
(
Revenue money default 0,
[Date] varchar(15),
monthn smallint,
yearn smallint,
[CostCenter] varchar(50),
[PayorType] varchar(50)
)

--Temp table to store the values of the cost centers
declare @costcenter_tmp table
(
ccid int IDENTITY (1,1),
ccname varchar(50)
)

--Inserts cost centers and code into the @costcenter_tmp temp table
insert into @costcenter_tmp (ccname) select costcenter.full name + ' '
+ costcenter.code from costcenter, agency_cost_cen ter
where costcenter.oid = agency_cost_cen ter.cost_center _moniker

--Sets the @cccounter variable to the number of cost centers
select @cccounter = count(*) from @costcenter_tmp

--Temp table to store the values of the payor types
declare @payor_type_tmp table
(
ptid int identity (1,1),
ptname varchar(50)
)

--Inserts payor types into the @payor_type_tmp temp table
Insert into @payor_type_tmp (ptname)select fullname from payor_type,
payor
where payor_type.oid = payor.payor_typ e_moniker

--Sets the @ptcounter variable to the number of payor types
select @ptcounter = count(*) from @payor_type_tmp
--Loop that gets the first part of the fiscal year
While (@month <13)
begin
--Loop that gets the value of the cost center to insert
While (@ccount <= @cccounter)
begin
--Loop that inserts values for the first part of the fiscal year into
the @Recorded_Reven ue_tmp temp table
while (@pcount <= @ptcounter)
begin
Insert into @Recorded_Reven ue_tmp(Revenue, [Date], monthn,
yearn,[CostCenter],[PayorType])
select 0, datename(month, @month)+ ' ' + cast(@year -1 as varchar(4)),
@month, @year -1, ccname, ptname
from @costcenter_tmp ct,@payor_type_ tmp pt where ct.ccid = @ccount and
pt.ptid = @pcount
set @pcount = @pcount + 1
end
set @pcount = 1
set @ccount = @ccount + 1
end
set @ccount = 1
set @month = @month + 1
end

select * from @Recorded_Reven ue_tmp
sample return data:(returns 16008 rows!!!)

..0000 January 2003 7 2003 Genesis Assertive Community Treatment Team
250 SAGA
..0000 January 2003 7 2003 Genesis Assertive Community Treatment Team
250 Self Pay
..0000 January 2003 7 2003 Genesis Assertive Community Treatment Team
250 ABH
..0000 January 2003 7 2003 Genesis Assertive Community Treatment Team
250 Managed Medicaid
..0000 January 2003 7 2003 Genesis Assertive Community Treatment Team
250 Managed Medicaid
..0000 January 2003 7 2003 Genesis Assertive Community Treatment Team
250 Managed Medicaid
..0000 January 2003 7 2003 Genesis Assertive Community Treatment Team
250 Managed Medicaid
..0000 January 2003 7 2003 Genesis Assertive Community Treatment Team
250 Commercial
..0000 January 2003 7 2003 Genesis Assertive Community Treatment Team
250 Commercial
..0000 January 2003 7 2003 Genesis Assertive Community Treatment Team
250 Commercial
..0000 January 2003 7 2003 Genesis Assertive Community Treatment Team
250 Commercial

thanks -Jim
Jul 20 '05 #1
2 7804
On 27 May 2004 06:43:30 -0700, Jim wrote:
Im getting way too many rows retured..what its trying to do is insert
a 0 for revenue for months 7 - 12 (aka July through December) for each
of these cost centers for each payor type..Im getting a lot of repeats
and the concatenation field date always comes back as January 2003
instead of the month and date its supposed to


Hi Jim,

I can't see what's going wrong, but I think you'd be better off with a
set-based way to do things. The loops for costcenter and payor type are
definitely not needded. The month loop can be eliminated as well, if you
create a Months table:

CREATE Months (MonthNo tinyint not null primary key
check (MonthNo between 1 and 12),
FiscalYearOffse t tinyint not null
check (FiscalYearOffs et in (-1, 0)))
go
INSERT Months (MonthNo, FiscalYearOffse t)
SELECT 1, 0 UNION ALL
SELECT 2, 0 UNION ALL
SELECT 3, 0 UNION ALL
SELECT 4, 0 UNION ALL
SELECT 5, 0 UNION ALL
SELECT 6, 0 UNION ALL
SELECT 7, -1 UNION ALL
SELECT 8, -1 UNION ALL
SELECT 9, -1 UNION ALL
SELECT 10, -1 UNION ALL
SELECT 11, -1 UNION ALL
SELECT 12, -1
go

This is a one time operation. Grant everyone select permission on this
table and revoke all other permissions for everyone.
Now, you can replace your nested loop code with one set-based insert
statement:

--Fiscal Year
declare @year smallint
set @year = 2004

declare @Recorded_Reven ue_tmp table
(
Revenue money default 0,
[Date] varchar(15),
monthn tinyint,
yearn smallint,
[CostCenter] varchar(50),
[PayorType] varchar(50)
)

Insert into @Recorded_Reven ue_tmp(Revenue, [Date], monthn,
yearn,[CostCenter],[PayorType])
select 0, datename(month, Months.MonthNo) + ' ' +
cast(@year + Months.FiscalYe arOffset as varchar(4)),
Months.MonthNo, @year + Months.FiscalYe arOffset,
costcenter.full name + ' ' + costcenter.code ,
payor_type.full name
from ( costcenter
inner join agency_cost_cen ter
on costcenter.oid = agency_cost_cen ter.cost_center _moniker )
cross join
( payor_type,
inner join payor
on payor_type.oid = payor.payor_typ e_moniker )
cross join Months

where Monts.MonthNo between 7 and 12
/* Unless you need the other half of the fiscal year as well */

(untested)

Best, Hugo
--

(Remove _NO_ and _SPAM_ to get my e-mail address)
Jul 20 '05 #2
A numbers table and CROSS JOIN is a good ways to generate this kind of
stuff.
http://www.bizdatasolutions.com/tsql/tblnumbers.asp

Something like this is easier than nested loops:

INSERT INTO @Recorded_Reven ue_tmp (date, costcenter, payortype)
SELECT ccname, ptname, DATEADD(MONTH,N .number,'200312 01')
FROM
(SELECT costcenter.full name + ' '+ costcenter.code AS ccname
FROM costcenter
JOIN agency_cost_cen ter
ON costcenter.oid = agency_cost_cen ter.cost_center _moniker) AS C
CROSS JOIN
(SELECT fullname AS ptname
FROM payor_type
JOIN payor
ON payor_type.oid = payor.payor_typ e_moniker) AS P
JOIN
Numbers AS N
ON N.number BETWEEN 1 AND 12

--
David Portas
SQL Server MVP
--
Jul 20 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
3465
by: Colin Steadman | last post by:
I have created a function to kill all session variables that aren't in a safe list. This is the function - Sub PurgeSessionVariables For Each Item In Session.Contents Select Case Trim(Item) Case "Authenticated" Case "CI_CODE" Case "organisation_description" Case "location_description"
7
1776
by: Alfonso Morra | last post by:
I have a class that contains a nested class. The outer class is called outer, and the nested class is called inner. When I try to compile the following code, I get a number of errors. It is not obvious to me, where I'm going wrong (the compiler messages do not seem to make much sense). here is the code: outer class declared as ff in "outer.h":
46
9911
by: Neptune | last post by:
Hello. I am working my way through Zhang's "Teach yourself C in 24 hrs (2e)" (Sam's series), and for nested loops, he writes (p116) "It's often necessary to create a loop even when you are already in a loop." Then he goes on to portray a contrived example that doesn't tell me under what conditions a nested loop might be favoured as a solution? i.e. what are nested loops useful for? What kinds of algorithms are served by nested loops?...
2
1132
by: Alfonso Morra | last post by:
I have a class that contains a nested class. The outer class is called outer, and the nested class is called inner. When I try to compile the following code, I get a number of errors. It is not obvious to me, where I'm going wrong (the compiler messages do not seem to make much sense). here is the code: outer class declared as ff in "outer.h":
28
72455
by: mooreit | last post by:
The purpose for my questions is accessing these technologies from applications. I develop both applications and databases. Working with Microsoft C#.NET and Microsoft SQL Server 2000 Production and 2005 Test Environments. What is the purpose of a view if I can just copy the vode from a view and put it into a stored procedure? Should I be accessing views from stored procedures?
9
2838
by: Gregory Petrosyan | last post by:
I often make helper functions nested, like this: def f(): def helper(): ... ... is it a good practice or not? What about performance of such constructs?
12
3537
by: upernikaw | last post by:
Hello, I am attempting to create a nested loop (in Access 2003/VB) that will print a report for a set of user defined months inputed on a form and that will print out for every Client. So the first loop would increment the Client by 1, then the second loop would increment the Month by 1. So the first time around Client A for Month 1, then Client A for month 2 then Client B for Month 1 and so on. Below is what I have already, I can get the...
5
1704
by: hiqu | last post by:
This issue is driving me nuts and not able to figure out whats wrong. I've this code in my firefox extension. Firefox always hangs and reports the script is busy. if I introduce a break statement in the for(;;) loop below, then no issue. Any help would be appreciated! function getStuff()
0
2771
by: LanaR | last post by:
Hello, one sql statement is causing severe performance issue. The problem occurs only in UDB environment, the same statemnt on the mainframe is running fine. I have an explain output from the sql. The statement itself is not that complicated, it is 3 selects and union all. Explain output is pretty big, but I could not find anything unusual. I'm new to db2 and I could be missing stuff. I am posting the explain output below and I really...
0
8275
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8802
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8697
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8465
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8579
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6158
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5612
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4283
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2699
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.