I am trying to add two variables
DECLARE @RentsSum MONEY
SET @RentsSum = (SELECT SUM(Price)
FROM Rents
WHERE StartDate IS NOT NULL)
GO
DECLARE @SalesSum MONEY
SET @PriceSum = (SELECT SUM(Price)
FROM Purchases
WHERE DateBought IS NOT NULL)
GO
DECLARE @SalesAndRentsSum MONEY
SET @SalesAndRentsSum = @RentsSum + @PriceSum;
SELECT @SalesAndRentsSum
into one but I get some strange errors here...
Must declare the scalar variable "@PriceSum".
Must declare the scalar variable "@RentsSum".
Remove all the GO words. You have three separate batches here and the variable must be declared within the scope of that batch.
DECLARE @RentsSum MONEY, @SalesSum MONEY, @SalesAndRentsSum MONEY
SET @RentsSum = (SELECT SUM(Price)
FROM Rents
WHERE StartDate IS NOT NULL)
SET @SalesSum = (SELECT SUM(Price)
FROM Purchases
WHERE DateBought IS NOT NULL)
SET @SalesAndRentsSum = @SalesSum + @PriceSum
SELECT @SalesAndRentsSum
DECLARE @RentsSum MONEY
DECLARE @PriceSum MONEY
DECLARE @SalesAndRentsSum MONEY
SET @RentsSum = (SELECT SUM(Price)
FROM Rents
WHERE StartDate IS NOT NULL)
SET @PriceSum = (SELECT SUM(Price)
FROM Purchases
WHERE DateBought IS NOT NULL)
SET @SalesAndRentsSum=@RentsSum+@PriceSum;
SELECT @SalesAndRentsSum
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With