Click here to Skip to main content
15,907,326 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
SQL
insert into revenue (
   Year,
   Newsales,
   Beginninginventory,
   Purchases,
   Goodforsales,
   Endinventory,
   Costofsales,
   GrossIncome,
   Salesandwages,
   sss,
   Rental,
   Postage,
   Lightandwater,
   Freight,
   Fuel,
   Officesupplies,
   Taxesandlicenses,
   Representation,
   Depreciation,
   Transportation,
   Repairs,
   Miscellaneous,
   Totaloperatingexpense,
   Provisions,
   Netincomefinal
)
VALUES (
   @one,
   (Select SUM(TotalPrice) FROM inventoryout where revenue.Newsales=@two),
   @three,
   @four,
   @five,
   @six,
   @seven,
   (select NewSales - Costofsales from revenue where GrossIncome=@eight),
   @nine,
   @ten,
   @eleven,
   @twelve,
   @thirteen,
   @fourteen,
   @fifteen,
   @sixteen,
   @seventeen,
   @eighteen,
   @nineteen,
   @twenty,
   @twentyone,
   @twentytwo,
   @twentythree,
   (select GrossIncome - Totaloperatingexpense from revenue where Netincomebeforetaxes = @twentyfour),
   @twentyfive,
   (Select Netincomebeforetaxes - Provisions from revenue where Netincomefinal = @twentysix)
)
Posted
Updated 8-Mar-15 6:01am
v2

Maybe you can try to compute the subqueries first, and store them in other variables, that you can then include in your main query.
Something like:
SQL
DECLARE @computedTwo money = SELECT SUM(TotalPrice) FROM inventoryout WHERE revenue.Newsales=@two
-- Then
INSERT INTO revenues
-- ...
VALUES (
-- ...
@computedTwo
-- ...
)


Do you catch the idea?
 
Share this answer
 
Comments
_bluRe_ 8-Mar-15 12:12pm    
hmm something like execute the sub query and assign to variable then put them into the main query, right?
phil.o 8-Mar-15 12:17pm    
Yes. That is not necessarily optimized, though, but that is the only thing I can say, given the fact that we don't know much of the structure of revenues and revenue tables, and how they are related. It has the advantadge to be much easier to read and debug.
_bluRe_ 8-Mar-15 12:15pm    
how can i code this in buttonevent in c#? like assign in a string variable, then..?
phil.o 8-Mar-15 12:18pm    
You can, but that does not mean you should. I do not like to put implementations in event handlers; I prefer having implementations in meaningfully-named methods, which I call from my event handlers. But that's a question of choice.
_bluRe_ 8-Mar-15 12:24pm    
cmd.commandText = "insert into ....values(@computedTwo)..";
ComputedTwo();
cmd.Parameter.AddwithValue(@computerTwo);

something like this? thats what i want to do but i dont know how to put the method in the parameter..
i use store procedure to handle them at once

create procedure testtable
(
@colum1 int,
@colum2 nvarchar(50),
@colum3 nvarchar(50),
@colum4 nvarchar(50),
@flag int,
)
as
BEGIN
	if (@flag=0)
	BEGIN
	-- select query
	END
	
	if (@flag=1)
    BEGIN
		-- insert query
    END
    
    if (@flag=2)
    BEGIN
		-- update query
    END
    
    if (@flag=3)
    BEGIN
		-- delete query
    END
END
 
Share this answer
 

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