1

I'm trying to write a function that adds values that are selected from two different tables. I need the SUM of one column for a specific id in table 1, added to the SUM of a column for a specific id in table 2.

CREATE FUNCTION dbo.getSum(@id varchar(9)) --Our IDs are strings of length 9
RETURNS integer --I've also tried decimal(x,x) and int
AS
BEGIN
DECLARE @total as integer; -- same here with decimal and int
SELECT @total = 
(
    (select SUM(Amount)
    from table1
    where id = @id)
    +
    (select SUM(amount)
    from table2
    where id = @id)
);
RETURN @total;
END;

I get several types of errors when I try to create this function, like incorrect syntax near 'integer', Must declare the scalar variable "@total"., and Incorrect syntax near 'END'.

I've tried several variations and looked at several SO questions, but haven't found one that's fixed this issue for me yet. This is on SQL Server.

2
  • Have you tried declaring two different variables, storing the sum of each table into each variable and then performing an addition on them? Commented May 19, 2016 at 14:46
  • Yes, and that also didn't work, with similar errors to what I posted above. Commented May 19, 2016 at 15:15

4 Answers 4

2

As previously mentioned the cause of your error is you did not define a datatype for your inbound parameter.

I would suggest a bit of a departure from the current structure. Instead of a scalar function I would use an inline table valued function. Something along these lines.

CREATE FUNCTION dbo.getSum(@id int)
RETURNS table
AS RETURN

SELECT SUM(MyAmount)
from
(
    select SUM(Amount) as MyAmount
    from table1
    where id = @id
    UNION ALL
    select SUM(amount)
    from table2
    where id = @id

) x;
Sign up to request clarification or add additional context in comments.

3 Comments

There were multiple errors, even after declaring the parameter type, but your suggestion with the UNION ALL is perfect, and goes right along with what I needed. Thanks!
So, I did have to make a minor edit from what you returned. The UNION ALL part of the function works fine, but I just had to return the whole thing as a table, without the outer select SUM(MyAmount) from part. It returns a table with one column, MyAmount, and then I can add those together.
Cool. Glad you were able to at least get a solid solution from it. :)
0

Looks like you are missing the parameter type in the function definiton

Try with the following

CREATE FUNCTION dbo.getSum(@id int)
RETURNS integer --I've also tried decimal(x,x)
AS
BEGIN
DECLARE @total as integer; -- same here with decimal
SELECT @total = 
(
    (select SUM(Amount)
    from table1
    where id = @id)
    +
    (select SUM(amount)
    from table2
    where id = @id)
);
RETURN @total;
END;

1 Comment

I've updated my original question. I did forget the parameter type originally, but have added it and still getting the same errors.
0

I think you need to declare the type of the parameter

CREATE FUNCTION dbo.getSum(@id int)

4 Comments

I noticed I hadn't done that after, but it still is causing issues. I'm not sure why, I've done the same thing on other functions and not getting that error.
I can create the function in my db if I add the datatype
what SQL Server version are you using?
I think 2014. I'd have to check, but I'm pretty sure it's 2014.
0

Your data types should be INT and there are general syntax errors... see below.

CREATE FUNCTION dbo.getSum(@id AS INT) -- Declare your paramater AS (datatype)
RETURNS INT  -- No need for AS here, just start your block
BEGIN
DECLARE @total AS INT; 
SELECT @total = 
(
    (select SUM(Amount)
    from TableOne
    where id = @id)
    +
    (select SUM(amount)
    from TableTwo
    where id = @id)
);
RETURN @total;
END;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.