Question

I am trying to send a return message to front end. If the parameter which is passed, if it is not in format like

'A-1213-465-798-01'

It should reply back 1. But I am missing out the validation. please help.

'A-1213-465-798-01'

IF @OptParam3 not like '[_-_-_-_-_]'
Begin
    Set @Return_Message = '1' -- Validation 'Invalid code
    Print 'Error: Invalid Code' 
    Return
End
Was it helpful?

Solution

Assuming this is a stored procedure you probably want something along the lines of this

IF OBJECT_ID('sp_demo') > 0 DROP PROC sp_demo
go
CREATE PROC sp_demo (@OptParam1 varchar(50),@OptParam2 varchar(50),@OptParam3 varchar(50))
AS 
IF @OptParam3 not like '_-____-___-___-__'
Begin
    Print 'Error: Invalid Code' 
    Return 1
End
/*
If we got here then the parameter is validated
*/
PRINT '@OptParam3 is valid:'
PRINT @OptParam3
Return 0

You can check this by doing the following

/* Driver to test the stored procedure */

DECLARE @return_Code int
EXEC @return_Code = sp_demo 'parameterA','parameterB','A-1213-465-798-01';
PRINT @return_Code

But really you should probably be raising an error in your code if your input parameters are invalid. This will come through as a trapable error in your front end

IF OBJECT_ID('sp_demo') > 0 DROP PROC sp_demo
go
CREATE PROC sp_demo (@OptParam1 varchar(50),@OptParam2 varchar(50),@OptParam3 varchar(50))
AS 
IF @OptParam3 not like '_-____-___-___-__'
Begin
    RAISERROR (N'Error: Invalid Code "%s"',16,1 ,@OptParam3)
    return @@ERROR
End
/*
If we got here then the parameter is validated
*/

PRINT '@OptParam3 is valid:'
PRINT @OptParam3
Return 0
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top