output parameter in stored procedure not working - sql

I am new with stored procedures.
I have simple stored procedure for addition of two numbers as follows:
alter proc simpleProc
(
#Tax int ,
#TotalAmount int,
#sum int output
)
as
BEGIN
set #sum=(#Tax+#TotalAmount)
print #sum
END
As we can see in this #sum is output parameter.
But when I execute it as follows:
exec simpleProc 908,82
It gives me the following error:
Msg 201, Level 16, State 4, Procedure simpleProc, Line 0
Procedure or Function 'simpleProc' expects parameter '#sum', which was not supplied.
I have mentioned #sum as output parameter, but then also its demanding me to input #sum parameter.
What can be the mistake?

Yes it you haven't provided output parameter.
Try this
Declare #op int
exec simpleProc 908,82,#op output
//use op variable

You should give the procedure a variable where the output can be stored
declare #sum int
exec simpleProc 908, 82, #sum output

Related

The formal parameter “#mode” was not declared as an OUTPUT parameter, but the actual parameter passed in requested output

I have this stored procedure:
ALTER PROCEDURE spCertificationType
#result nvarchar(15) output,
#mode int
AS
BEGIN
if #mode = 1
begin
exec spGeneratedID 2, #result output
print #result
end
END
but when I tried to execute it,it has this error
The formal parameter “#mode” was not declared as an OUTPUT parameter, but the actual parameter passed in requested output.
I tried to set #mode as output like this:
ALTER PROCEDURE spCertificationType
#result nvarchar(15) output,
#mode int output
AS
BEGIN
if #mode = 1
begin
exec spGeneratedID 2, #result output
print #result
end
END
but the it returns a null value.
Any fix for this? Thanks in advance.
the sequence of parameter in store procedure is that first use input parameter then use output parameter:-
you can see this link for more knowledge of store procedure:-
http://www.codeproject.com/Articles/126898/Sql-Server-How-To-Write-a-Stored-Procedure-in-SQL
ALTER PROCEDURE spCertificationType
#mode int,
#result nvarchar(15) output
AS
BEGIN
if #mode = 1
begin
exec spGeneratedID 2, #result output
print #result
end
END
I fixed this error a different way.
I had removed the OUTPUT statement after a parameter in my SP.
I then got the error.
I then went back to the SQLDataSource Configure Data Source wizard and went through the steps again. I discovered that changing the SP made the wizard delete the settings associated with the parameter that used to have OUTPUT after it.
In my case, I had declared the OUTPUT parameter last but was also getting a similar exception.
I moved the default parameters (like #var FLOAT = null) to the end after the output parameter then it started working.

How to change data type of output of stored procedure? I want to output money data type but procedure always returns int

This is my procedure:
ALTER PROCEDURE spMaxOfInvoiceTotal
AS
BEGIN
DECLARE #max MONEY
SET #max = (SELECT MAX(InvoiceTotal) FROM Invoices)
PRINT #MAX
RETURN #MAX
END
GO
But when I execute, it returns int not money type.
DECLARE #return_value int
EXEC #return_value = [dbo].[spMaxOfInvoiceTotal]
SELECT 'Return Value' = #return_value
GO
As a result, a value is incorrect. It has to be 37966.19. But procedure returns 37966.
Even if I change #return_value money, I still get int. How to change procedure so return value would be money?
Stored procedure return value is used to return exit code, it is integer.
You should define output parameter
CREATE PROCEDURE mysp
#Maxval MONEY OUTPUT
http://msdn.microsoft.com/en-us/library/ms188655.aspx
What RDBMS is this for? SQL Server?
The value returned from a stored procedure in SQL Server is always INT and you can't change that - it's typically used to convey back a success/failure flag, or a "number of rows affected" information.
If you to "return" a MONEY (or better yet: DECIMAL) value - you can either use an OUTPUT parameter (which works fine for a single value), or you need to return a result set with that value.
So in your case, you could try something like:
CREATE PROCEDURE GetMaxOfInvoiceTotal
#MaxValue DECIMAL(20,4) OUTPUT
AS
BEGIN
SET #MaxValue = (SELECT MAX(InvoiceTotal) FROM Invoices)
END
GO
and then call this stored procedure like this:
DECLARE #RC INT
DECLARE #MaxValue DECIMAL(20,4)
EXECUTE #RC = [dbo].[GetMaxOfInvoiceTotal] #MaxValue OUTPUT
GO

store the return value from a procedure in a variable

I am trying to execute a procedure(A) inside another procedure(B) and I want to store the value returned from the procedure A in a variable called #LogId.
after executing Procedure A
exec TES_usp_getnextlogid
it returns something like
NewLogId = xxxx
I tried doing the following in procedure B.
declare #LogId int
exec #LogId = TES_usp_getnextlogid
my question is, why doesn't #LogId holds the value which is returned from TES_usp_getnextlogid Procedure?
Thanks.
use output parameter for this, return value should only be used for returning Success/Faliuer status.
CREATE PROCEDURE My_Outter_Proc2
AS
BEGIN
DECLARE #NextID INT;
EXEC TES_usp_getnextlogid #NextID OUTPUT
EXEC My_Inner_Proc #NextID
END

int is incompatible with uniqueidentifier when no int usage

I am getting this error when there is absolutely no usage of int anywhere.
I have this stored procedure
ALTER procedure [dbo].[usp_GetFileGuid] #fileType varchar(25)
as
select [id] from fileTypes where dirId = #fileType
Here id is a uniqueidentifier in fileTypes table
When I execute the following
declare #fileGuid uniqueidentifier
exec #fileGuid = usp_GetFileGuid 'accounts'
print #fileGuid
I get the following error
(1 row(s) affected)
Msg 206, Level 16, State 2, Procedure usp_GetFileGuid, Line 0
Operand type clash: int is incompatible with uniqueidentifier
Is there anything wrong with the syntax of assigning output of stored procedure to the local variable? Thank you.
You are using EXEC #fileGuid = procedure syntax which is used for retrieving return values, not resultsets. Return values are restricted to INT and should only be used to return status / error codes, not data.
What you want to do is use an OUTPUT parameter:
ALTER procedure [dbo].[usp_GetFileGuid]
#fileType varchar(25),
#id UNIQUEIDENTIFIER OUTPUT
AS
BEGIN
SET NOCOUNT ON;
SELECT #id = [id] from dbo.fileTypes where dirId = #fileType;
-- if the procedure *also* needs to return this as a resultset:
SELECT [id] = #id;
END
GO
Then for usage:
declare #fileGuid uniqueidentifier;
exec dbo.usp_GetFileGuid #fileType = 'accounts', #id = #fileGuid OUTPUT;
print #fileGuid;
create procedure [dbo].[usp_GetFileGuid] #fileType varchar(25),#uuid uniqueidentifier output
as
select #uuid=[id] from fileTypes where dirId = #fileType
declare #fileGuid uniqueidentifier
exec usp_GetFileGuid 'accounts',#fileGuid output
print #fileGuid
The value returned is an int as it is the status of the execution
From CREATE PROCEDURE (Transact-SQL)
Return a status value to a calling procedure or batch to indicate
success or failure (and the reason for failure).
You are looking for an output parameter.
OUT | OUTPUT
Indicates that the parameter is an output parameter. Use
OUTPUT parameters to return values to the caller of the procedure.
text, ntext, and image parameters cannot be used as OUTPUT parameters,
unless the procedure is a CLR procedure. An output parameter can be a
cursor placeholder, unless the procedure is a CLR procedure. A
table-value data type cannot be specified as an OUTPUT parameter of a
procedure.

How Can i Let Stored Procedure Returns Varchar Value?

Here is my sample:
ALTER PROCEDURE EmpFirstName
#myParam int,
#empFName varchar(20) output
AS
BEGIN
SET NOCOUNT ON;
SELECT #empFName = empfname
FROM FE_QEMP
WHERE empno = #myParam
END
GO
myParam is the input and empFName will carry the output, so the procedure
should only take 1 parameter since empFName is the output, but in my case
i get this error:
Msg 201, Level 16, State 4, Procedure
EmpFirstName, Line 0 Procedure or
function 'EmpFirstName' expects
parameter '#empFName', which was not
supplied.
This is the way i called the procedure:
DECLARE #returnValue varchar(20)
EXEC #returnValue = EmpFirstName 10
SELECT 'Return Value ' = #returnValue
Return values and output parameters are two different things. If you want to call it with an output parameter, you'd do it like this:
EXEC EmpFirstName 10, #returnValue OUTPUT
SELECT 'Return Value ' + #returnValue
If you want to call it in the manner that you described in your example, then you need to alter the stored procedure to state RETURNS VARCHAR(20) and remove the output parameter. To return a value, you have to explicitly call return. In your example, you'd declare a variable, assign it in the select statement, then call return #varName.
Thanks. My aha moment came with this post. Did not realise that output parameters need to be qualified with the "output" identifier too when executed, not just in the procedure!
Here are my test workings for my fellow sql server noobs. I am using sqlcmd with sql server 2005.
The stored procedure:
/* :r procTest.sql */
if exists (select name from sysobjects where name="procTest" and type="P")
drop procedure procTest;
go
create procedure procTest
/* Test stored procedure elements. */
(#i_pt_varchar varchar(20),
#o_pt_varchar varchar(20) output)
as
begin
print "procTest";
set #o_pt_varchar = "string coming out";
print "#i_pt_varchar " + #i_pt_varchar;
print "#o_pt_varchar " + #o_pt_varchar;
return (0);
end
go
The test call:
/* :r procTest.test.sql */
declare #returnFlag int;
declare #i_varchar varchar(20);
declare #o_varchar varchar(20);
set #i_varchar = "string going in";
set #o_varchar = null;
execute #returnFlag = procTest #i_varchar, #o_varchar output
print "#returnFlag " + cast(#returnFlag as varchar(20));
print "after call";
print "#i_varchar " + #i_varchar;
print "#o_varchar " + #o_varchar;
go