How to make input parameters mandatory in Stored Procedure ?

  • When we create a Stored Procedure , how can we define input parameters mandatory ? For eg.

    create procedure ProcTest1

    @policy_id int,

    @policy_name varchar(100),

    begin

    end

    When i execute this SP, if I dont pass any value for these 2 parameters, it gives error that Procedure expects these 2 parameters.

    But when i execute this procedure and select checkbox for 'Pass Null Value' against these 2 parameters, it executes well without showing message.

    How can I make it mandatory so that NULL is not allowed for these 2 parameters ?

  • There is no way in SQL Server to enforce that a non-null value is passed to parameter. You would need to verify the values within the stored procedure or the client application.

  • You can't actually make them mandatory, but you can add something like:

    create proc MyProc

    (@MyParam1_in datatype)

    as

    if @MyParam1_in is null

    raiserror('Null values not allowed for MyParam1_in', 16, 1)

    ...the rest of the proc...

    - Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
    Property of The Thread

    "Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon

  • Thanks to All.

  • I often use parameter validation in the proc -- ALONG WITH default parameter values on the proc --e.g.,

    CREATE PROC spMyProc

    @State CHAR(2) = 'CA',

    @Country VARCHAR(32) = NULL

    AS

    BEGIN

    -- Disallow NULL Country!

    IF (@Country IS NULL)

    SET @Country = 'USA'

    . . .

    END

    GO

    Here the user can pass either 0, 1, or 2 parameters, and things should still work.

Viewing 5 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic. Login to reply