473,385 Members | 1,531 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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_Revenue_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.fullname + ' '
+ costcenter.code from costcenter, agency_cost_center
where costcenter.oid = agency_cost_center.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_type_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_Revenue_tmp temp table
while (@pcount <= @ptcounter)
begin
Insert into @Recorded_Revenue_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_Revenue_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 7786
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),
FiscalYearOffset tinyint not null
check (FiscalYearOffset in (-1, 0)))
go
INSERT Months (MonthNo, FiscalYearOffset)
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_Revenue_tmp table
(
Revenue money default 0,
[Date] varchar(15),
monthn tinyint,
yearn smallint,
[CostCenter] varchar(50),
[PayorType] varchar(50)
)

Insert into @Recorded_Revenue_tmp(Revenue, [Date], monthn,
yearn,[CostCenter],[PayorType])
select 0, datename(month, Months.MonthNo) + ' ' +
cast(@year + Months.FiscalYearOffset as varchar(4)),
Months.MonthNo, @year + Months.FiscalYearOffset,
costcenter.fullname + ' ' + costcenter.code,
payor_type.fullname
from ( costcenter
inner join agency_cost_center
on costcenter.oid = agency_cost_center.cost_center_moniker )
cross join
( payor_type,
inner join payor
on payor_type.oid = payor.payor_type_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_Revenue_tmp (date, costcenter, payortype)
SELECT ccname, ptname, DATEADD(MONTH,N.number,'20031201')
FROM
(SELECT costcenter.fullname + ' '+ costcenter.code AS ccname
FROM costcenter
JOIN agency_cost_center
ON costcenter.oid = agency_cost_center.cost_center_moniker) AS C
CROSS JOIN
(SELECT fullname AS ptname
FROM payor_type
JOIN payor
ON payor_type.oid = payor.payor_type_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
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)...
7
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...
46
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...
2
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...
28
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...
9
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
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...
5
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...
0
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....
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.