Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Return bigint from SQL Server stored procedure

I have written a stored procedure in SQL Server which will return a Bigint value

alter procedure [dbo].adding 
    @one bigint,
    @two bigint,
    @startid bigint output 
as
begin
    set @startid = @one + @two

    return @startid     
end

But while returning value I am getting an exception

Arithmetic overflow error converting expression to data type int

Can any one please help me to solve this issue?

Thanks in advance

Note : Above query is not the exactly the same procedure which i am using

UPDATED :

I have a code like below

_lookupId = cmdInsert.Parameters.Add("RetVal", SqlDbType.BigInt);
                        _lookupId.Direction = ParameterDirection.ReturnValue;

                        _procIn01 = cmdInsert.Parameters.Add("@idCount", SqlDbType.VarChar, 500);
                        cmdInsert.Parameters["@idCount"].Value = idCount;
                        _procIn01.Direction = ParameterDirection.Input;

                        _procIn02 = cmdInsert.Parameters.Add("@requestFrom", SqlDbType.VarChar, 100);
                        cmdInsert.Parameters["@requestFrom"].Value = clientId;
                        _procIn02.Direction = ParameterDirection.Input;

                        _pramOut = cmdInsert.Parameters.Add("@startID", SqlDbType.BigInt);
                        _pramOut.Direction = ParameterDirection.Output;
                        cmdInsert.ExecuteScalar();

With out returning the value how can i assign the value to "RetVal" my _lookup variable.

like image 665
backtrack Avatar asked Mar 19 '23 12:03

backtrack


1 Answers

Procedures return a status value, which is always an integer. You can return the value from the stored procedure just by setting it:

alter procedure [dbo].adding 
    -- add the parameters for the stored procedure here
    @one bigint,
    @two bigint,
    @startid bigint output 
as
begin
    -- set nocount on added to prevent extra result sets from
    -- interfering with select statements.

    set @startid = @one + @two     
end;

Use return for whether or not the stored procedure succeeds.

You could call this with something like:

declare @startid bigint;

exec dbo.adding(1, 2, @startid output);

select @startid;
like image 180
Gordon Linoff Avatar answered Mar 21 '23 20:03

Gordon Linoff