Click here to Skip to main content
15,908,020 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
SQL
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


SQL
------------------------------------------------------------
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)



SQL
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 --


SQL
-- <% Preparing Invoice Coding Stuff %>

declare @maximum int, @members int, @memberinmt int, @cidcocharge int, @total float, @charge float, @Areasqure float, @monthlycharge float

-- select starting/minimum number of MemberID
select @members = min(MemberID) from tblmembers where SocietyID = 1

-- select last/maximum number of MemberID
select @maximum = max(MemberID) from tblmembers where SocietyID = 1

while(@members<=@maximum)
Begin
    -- calculating society charges and area charges for maintainence

        select @charge = SocietyCharges from tblsociety where SocietyID = 1
        select @Areasqure = Area from tblmembers where SocietyID = 1 and MemberID = @members
        set @monthlycharge = @charge * @Areasqure
        -- print @monthlycharge

        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.
Posted

1 solution

From the details, I think you just need the calculated results for society id 1? There is no need of using a while loop here, you can do the calculation and do the insert operation using a sql query like this:

SQL
insert into tblInvoice(SocietyID,MemberID,MonthlyCharges,CidocCharges,TotalCharges)
select mem.SocietyID,mem.MemberID,(sc.SocietyCharges*mem.AreaofFlat) as MonthlyCharge,sc.AdditionCharges,((SocietyCharges*mem.AreaofFlat)+AdditionCharges) as TotalCharges  from tblsociety sc
    inner join tblmembers mem on sc.SocietyID=mem.SocietyID where sc.SocietyID=1
 
Share this answer
 
Comments
Tarun Jaiswal 28-Apr-14 11:30am    
thx buddie
altaf008bd 30-Apr-14 6:20am    
welcome :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900