Stored Proc sending error Response back to the caller

Rock Hitman 46 Reputation points
2020-10-15T06:19:53.157+00:00

Hi, below is my Stored proc.
If I receive @Pointer input value as Alphabets or NULL or ' ' then I need to send response as "error occured: invalid input data provided."
How can I modify my stored proc. Do I need to apply if else logic ...... Please advise

ALTER PROCEDURE [dbo].[usp_TestData_Select]
     (

           @Pointer int,
           @count int
     )
AS

             Select  [ID]
            ,[ModelID]
            ,[Model]
            ,[InsuranceID]
            ,[VehicleType]

            case when @Pointer is not null then (select count(*) from [dbo].[testData]) end TotalRec 
            from [dbo].[TestData]
            Where [ID] between ((@Pointer*@count) + 1) and (@Pointer+1)*@count   



GO
Developer technologies | Transact-SQL
SQL Server | Other
{count} votes

3 answers

Sort by: Most helpful
  1. EchoLiu-MSFT 14,621 Reputation points
    2020-10-15T06:25:46.093+00:00

    Hi @Rock Hitman

    The data type of your parameter @Pointer is int, Alphabets or NULL or '' are not of type int, so an error will be returned. For the variable @Pointer, you can only enter parameters of type int.
    Or you can set the data type of @Pointer to char(25).

    Hope this can help you.

    If you have any question, please feel free to let me know.
    If the response is helpful, please click "Accept Answer" and upvote it.

    Regards
    Echo


    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


  2. Olaf Helper 47,441 Reputation points
    2020-10-15T06:42:59.937+00:00

    I need to send response as "error occured: invalid input data provided."

    For this you can use the RAISERROR command


  3. Erland Sommarskog 121.9K Reputation points MVP Volunteer Moderator
    2020-10-15T21:51:41.923+00:00
    IF @pointer IS NULL
    BEGIN
       RAISERROR('You cannot pass NULL for the @pointer parameter', 16, 1)
       RETURN
    END
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.