sql - Stored Procedure Error Handling when parameter left out -
i have stored procedure here executes fine, custom error messages aren't working when leave out parameter. instead getting default error message.
does know doing wrong? using sql server 2012
so instance when call
exec sp_getclienttransactioninfo '2001-01-01'
should printing
'this error msg= date required. please enter date'
but isn't.
alter procedure sp_getclienttransactioninfo @fromdate datetime, @todate datetime, @active int set nocount on set transaction isolation level read uncommitted -- variables handle error msg declare @error int, @errormessage varchar(200) set @error = 0; set @errormessage = ''; if (@fromdate null) begin set @error = -1 set @errormessage = 'from date required. please enter date' goto final_process end if (@todate null) begin set @error = -1 set @errormessage = 'to date required. please enter date' goto final_process end if (@active null) begin set @error = -1 set @errormessage = 'active status required. please enter active status' goto final_process end select restaurantid, clientname client, mem_name member, mem_address address, sum(amountspent) 'amount spent', --((membersamt)/totclientamt)*100 convert(varchar,floor(round((sum(mv.amountspent)/ ( select sum(amountspent) udc_membervisits umv umv.restaurantid=mv.restaurantid )) * 100,0)))+'%' peramountspent dbo.udclub_client c join dbo.udc_membervisits mv on mv.restaurantid = c.clientid join dbo.udc_member m on m.mem_id = mv.memberid mv.datevisited between @fromdate , @todate group clientname, mem_name, mem_id, restaurantid, mem_address order clientname --error handling declare @errorvar int set @errorvar = @@error if @@error <> 0 final_process: print 'this error msg=' + @errormessage print 'error code =' + cast(@error nvarchar(8));
if call procedure this
exec sp_getclienttransactioninfo '2001-01-01'
then going error saying procedure expects missing parameter @todate
. missing parameter not set null, not there sql server throws error saying expecting parameter before executing procedure.
you can around this, setting parameters null in declaration of procedure, so:
alter procedure sp_getclienttransactioninfo @fromdate datetime = null, @todate datetime = null, @active int
so if not provide parameter, default null , error message show.
you may want raiseerror
rather print
Comments
Post a Comment