i want to prepare a invoice bill in sql-server.where all info is stored in two different table tblsociety and tblmembers.
using these tables i have to insert values in third table tblinvoice.
This is table tblsociety
SocietyID | SocietyName | SocietyCharges | AdditionCharges
1 | Ashtam building | 1.5 | 50
2 | Mayur Buildin | 1.6 | 50
tblsociety stores a values which is one time input from the user.Here SocietyId is primary key ,Identity(1,1).
This table is tblmembers is
SocietyID | DateofReg | MemberID | MemberName | AreaofFlat |
------------------------------------------------------------
1 2014-12-19 1 taurn 500
1 2014-12-19 2 jaiswal 450
1 2014-12-19 3 ramu 650
1 2014-12-19 4 shaun 850
1 2014-12-19 5 ravi 600
1 2014-12-19 6 samir 560
1 2014-12-19 7 rohan 350
1 2014-01-15 8 tarun 455
1 2014-10-15 9 tarunjakz 456
1 2014-10-15 10 me 155
2 2014-10-15 11 tarunjakz 456
2 2014-10-15 12 me 155
1 2014-01-15 13 tarun 455
1 2014-10-15 14 tarunjakz 456
1 2014-10-15 15 me 155
SocietyID is foreign key of primary key tblSociety(SocietyID) and MemberID is primary key, Identity(1,1)
based on these data i have to insert data in table tblinvoice and i have succeeded doing but in tblmembers if SocietyID is present in mixed way like above eg. [1,1,1,2,2,1,1,1].
then my code gets confused and insert some data into tblinvoice which is not correct
my database sql-server code is
declare @maximum int, @members int, @memberinmt int, @cidcocharge int, @total float, @charge float, @Areasqure float, @monthlycharge float
select @members = min(MemberID) from tblmembers where SocietyID = 1
select @maximum = max(MemberID) from tblmembers where SocietyID = 1
while(@members<=@maximum)
Begin
select @charge = SocietyCharges from tblsociety where SocietyID = 1
select @Areasqure = Area from tblmembers where SocietyID = 1 and MemberID = @members
set @monthlycharge = @charge * @Areasqure
select @cidcocharge = CidcoCharges from tblsociety where SocietyID = 1
set @total = @monthlycharge + @cidcocharge
Begin
insert into tblinvoice(SocietyID,MemberID,MonthlyCharges,CidcoCharges,TotalCharges) values (1,@members,@monthlycharge,@cidcocharge,@total)
set @members = @members + 1
End
End
select * from tblinvoice
this code give me output as
InvoiceID | SocietyID | Date | MemberID | MonthlyCharges | CidocCharges | TotalCharges
1 1 NULL 1 650 50 700
2 1 NULL 2 585 50 635
3 1 NULL 3 845 50 895
4 1 NULL 4 1105 50 1155
5 1 NULL 5 780 50 830
6 1 NULL 6 728 50 778
7 1 NULL 7 455 50 505
8 1 NULL 8 591.5 50 641.5
9 1 NULL 9 592.8 50 642.8
10 1 NULL 10 201.5 50 251.5
11 1 NULL 11 201.5 50 251.5
12 1 NULL 12 201.5 50 251.5
13 1 NULL 13 591.5 50 641.5
14 1 NULL 14 592.8 50 642.8
15 1 NULL 15 201.5 50 251.5
this is output is incorrect because MemberID 11 and 12 are from SocietyID = 2.
what i can do to solve this error.