I have a stored procedure like this,
USE MyDataBaseName
GO
CREATE PROCEDURE h_sp_find_exist
@in_name varchar,@in_counterparty varchar,
@out_recordsNum int OUTPUT
AS
BEGIN
SELECT @out_recordsNum = COUNT(*)
FROM TMyTableName
WHERE Name=@in_name AND Counterparty=@in_counterparty
RETURN @out_recordsNum
END
I always get 0 from the return value.
What’s wrong with it?
The execute code:
use MyDataBaseName
go
declare @retRecordNum int
execute h_sp_find_exist 'myName', '50139', @retRecordNum OUTPUT
select @retRecordNum as 'RecordNumber'
if I just run the sql code,
select count (*)
where name=’myName’ and counterparty=’50139′, it works, and result is 1
Your input parameters are being truncated, because you’ve not supplied a length for varchar, and the default is 1. (You also as others have said, don’t need the return statement):
Return sets the exit code for the stored procedure. This can only ever be an integer, and would generally be used to indicate success or failure. You can obtain the value from a return by assigning it as part of your exec statement:
Here,
@returnCodewould be assigned whatever value was passed to thereturnstatement