How to restrict NULL as parameter to stored procedure SQL Server?

前端 未结 5 1530
孤独总比滥情好
孤独总比滥情好 2020-12-09 14:52

Is it possible to create a stored procedure as

CREATE PROCEDURE Dummy 
    @ID INT NOT NULL
AS
BEGIN
END

Why is it not possible to do somet

5条回答
  •  抹茶落季
    2020-12-09 15:20

    Oh well, it seems I cannot edit @Unsliced post because "This edit deviates from the original intent of the post. Even edits that must make drastic changes should strive to preserve the goals of the post's owner.".

    So (@crokusek and everyone interested) this is my porposed solution:

    You could check for its NULL-ness in the sproc and RAISERROR to report the state back to the calling location.

    CREATE proc dbo.CheckForNull 
      @name sysname = 'parameter',
      @value sql_variant
    as
    begin
      if @value is null
        raiserror('The value for %s should not be null', 16, 1, @name) -- with log
    end
    GO
    

    Then call:

    exec dbo.CheckForNull @name 'whateverParamName', @value = 1
    

    or

    exec dbo.CheckForNull @value = null 
    

提交回复
热议问题