Connecting Tech Pros Worldwide Forums | Help | Site Map

Error and output variables in stored procedures

Member
 
Join Date: Dec 2007
Posts: 45
#1: Jan 9 '08
I have found alot of help on this site, and is helping me along. I am having trouble with a stored procedure. This is my first stored procedure.

The task I was given was to create a stored procedure that will accept leave balances for employees from our web based leave system. Those 4 values are: @Annual_Forward, @Sick_Forward, @Family_Forward, @Other_Forward. All variables are not going to be passed, so allowing null/empty values is a necessity. That is no problem as I figured that part out. My problem is the Output parameters. I was told to return @SQLCode(This is the code returned by the SQL Server showing if successful or not.) and @ErrMsg(The message returned by the database when there is an error.)

This is my stored procedure I wrote so far:
Expand|Select|Wrap|Line Numbers
  1. CREATE PROCEDURE [dbo].[usp_AcceptLeaveBalance]
  2.  
  3. @Emp_SSN int,
  4. @Annual_Forward decimal(10,2),
  5. @Sick_Forward decimal(10,2),
  6. @Family_Forward decimal(10,2),
  7. @Other_Forward decimal(10,2)
  8.  
  9. AS
  10.  
  11. UPDATE OT_MAIN
  12.  
  13. SET
  14.  
  15. EmpAnnual_Forward = IsNull(@Annual_Forward, EmpAnnual_Forward),
  16. EmpSick_Forward = IsNull(@Sick_Forward, EmpSick_Forward),
  17. EmpFamily_Forward = IsNull(@Family_Forward, EmpFamily_Forward),
  18. EmpOther_Forward = IsNull(@Other_Forward, EmpOther_Forward)
  19.  
  20. WHERE
  21.  
  22. Emp_SSN=@Emp_SSN
I can execute the procedure doing this:

Expand|Select|Wrap|Line Numbers
  1. exec usp_AcceptLeaveBalance 123456789, 10, null,null, null
As you can tell it is very rudimentary. The exec statement works, but if a ssn is passed to the database that doesn't exist then obviously that should generate an error. So i think something using IF @@ROWCOUNT = 0 then show error.

The @SQLCode and @ErrMsg is the problem. I am sure those two will be used to generate an email in the application side to alert me if there are errors. I hope some can help, and also post where to find some help on the web or books the would help a true beginner.

Thanks.

ck9663's Avatar
Expert
 
Join Date: Jun 2007
Posts: 1,925
#2: Jan 9 '08

re: Error and output variables in stored procedures


Quote:

Originally Posted by barmatt80

I have found alot of help on this site, and is helping me along. I am having trouble with a stored procedure. This is my first stored procedure.

The task I was given was to create a stored procedure that will accept leave balances for employees from our web based leave system. Those 4 values are: @Annual_Forward, @Sick_Forward, @Family_Forward, @Other_Forward. All variables are not going to be passed, so allowing null/empty values is a necessity. That is no problem as I figured that part out. My problem is the Output parameters. I was told to return @SQLCode(This is the code returned by the SQL Server showing if successful or not.) and @ErrMsg(The message returned by the database when there is an error.)

This is my stored procedure I wrote so far:

Expand|Select|Wrap|Line Numbers
  1. CREATE PROCEDURE [dbo].[usp_AcceptLeaveBalance]
  2.  
  3. @Emp_SSN int,
  4. @Annual_Forward decimal(10,2),
  5. @Sick_Forward decimal(10,2),
  6. @Family_Forward decimal(10,2),
  7. @Other_Forward decimal(10,2)
  8.  
  9. AS
  10.  
  11. UPDATE OT_MAIN
  12.  
  13. SET
  14.  
  15. EmpAnnual_Forward = IsNull(@Annual_Forward, EmpAnnual_Forward),
  16. EmpSick_Forward = IsNull(@Sick_Forward, EmpSick_Forward),
  17. EmpFamily_Forward = IsNull(@Family_Forward, EmpFamily_Forward),
  18. EmpOther_Forward = IsNull(@Other_Forward, EmpOther_Forward)
  19.  
  20. WHERE
  21.  
  22. Emp_SSN=@Emp_SSN
I can execute the procedure doing this:

Expand|Select|Wrap|Line Numbers
  1. exec usp_AcceptLeaveBalance 123456789, 10, null,null, null
As you can tell it is very rudimentary. The exec statement works, but if a ssn is passed to the database that doesn't exist then obviously that should generate an error. So i think something using IF @@ROWCOUNT = 0 then show error.

The @SQLCode and @ErrMsg is the problem. I am sure those two will be used to generate an email in the application side to alert me if there are errors. I hope some can help, and also post where to find some help on the web or books the would help a true beginner.

Thanks.


did not test this but i hope you'll get the idea:

Expand|Select|Wrap|Line Numbers
  1.       CREATE PROCEDURE [dbo].[usp_AcceptLeaveBalance]
  2.       @Emp_SSN int,
  3.       @Annual_Forward decimal(10,2),
  4.       @Sick_Forward decimal(10,2),
  5.       @Family_Forward decimal(10,2),
  6.       @Other_Forward decimal(10,2),
  7.       @result varchar(50) OUTPUT
  8.       AS
  9.          declare @errornum int,  @rowupdated int
  10.  
  11.          UPDATE OT_MAIN
  12.          SET
  13.          EmpAnnual_Forward = IsNull(@Annual_Forward, EmpAnnual_Forward),
  14.          EmpSick_Forward = IsNull(@Sick_Forward, EmpSick_Forward),
  15.          EmpFamily_Forward = IsNull(@Family_Forward, EmpFamily_Forward),
  16.          EmpOther_Forward = IsNull(@Other_Forward, EmpOther_Forward)
  17.          WHERE
  18.          Emp_SSN=@Emp_SSN
  19.  
  20.          select @errornum = @@error, @rowupdated = @@rowcount
  21.  
  22.          if @errornum <> 0 
  23.             set result = 'Error updating table (' + cast(@errornum as varchar) + ')'
  24.          else
  25.             if @rowupdated > 0
  26.                set @result = 'Total records updated: ' + cast(@rowupdated as varchar)
  27.             else
  28.                set @result = 'No records updated'
  29.  
  30.       go
  31.  
  32.  
  33.       declare @resultofproc varchar
  34.  
  35.       exec usp_AcceptLeaveBalance 123456789, 10, null,null, null, @resultofproc OUTPUT
here's a good reference

you can also use 2 output parameters to pass both the errornumber and the message. you can get it from the sysmessages system table....

also, am not sure if you pass an existing ssn and all the other values are the same, meaning what your passing and the existing value on the table are the same, am not sure if it will effect in the @@rowcount...

happy coding...

-- CK
Reply