Click here to Skip to main content
15,891,473 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi all!
I have a stored procedure that retrieves the sum twice with two aliases and I create a variable to carry the value of the difference of both of them.

There is an error, can any one tell me how can I make it,please???

SQL
ALTER PROC select_test
(
    @Id bigint
)
AS
DECLARE @x AS bigint
SELECT test6.Id ,test6.summation as  TotalLoan   ,(select test6.summation from test6 where Name='b' and Id =@Id ) as TotalBorrower, @x as 'Total'
FROM test6  WHERE Name='a' and Id =@Id
GROUP BY test6.AccId,test6.summation
SET @x= TotalLoan - TotalBorrower
PRINT @x


Thanks in advance!
Posted
Updated 14-Mar-11 3:05am
v2

1 solution

First of all there are no parenthesis () around the parameter list. Second problem is that calculating the difference after the SELECT statement will not set column "Total" to that value. Try it in the likes of this:

SQL
ALTER PROC select_test
    @Id bigint
AS
DECLARE @difference AS bigint, @totalLoan AS bigint, @totalBorrower AS bigint;
SET @totalLoan = (SELECT test6.summation FROM test6 WHERE Id = @Id AND Name='a');
SET @totalBorrower = (SELECT test6.summation FROM test6 WHERE Id = @Id AND Name='b');
SET @difference = @totalLoan - @totalBorrower;

SELECT @Id AS Id, @totalLoan AS TotalLoan, @totalBorrower AS TotalBorrower, @difference AS Total
--PRINT @x


Please tell me if that is what you wanted.

Cheers!
 
Share this answer
 
v2
Comments
moon2011 14-Mar-11 16:12pm    
thaaaaaaaaaaaaaaanks
Manfred Rudolf Bihy 14-Mar-11 16:30pm    
I take it then you got it to work, great! :)
Espen Harlinn 17-Mar-11 9:57am    
Good effort, my 5
Manfred Rudolf Bihy 17-Mar-11 10:31am    
Thanks for your vote(s).

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