Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 something like this?

like image 323
SharePoint Newbie Avatar asked Dec 01 '08 08:12

SharePoint Newbie


People also ask

How do I restrict NULL values in SQL?

SELECT column_names FROM table_name WHERE column_name IS NOT NULL; Query: SELECT * FROM Student WHERE Name IS NOT NULL AND Department IS NOT NULL AND Roll_No IS NOT NULL; To exclude the null values from all the columns we used AND operator.

Is NULL in stored procedure SQL Server?

Inside the stored procedure, the parameter value is first tested for Null using the ISNULL function and then checked whether it is Blank (Empty). If the parameter has value then only matching records will be returned, while if the parameter is Null or Blank (Empty) then all records from the table will be returned.

Can we pass NULL value in stored procedure?

You can't set impromptu to use a NULL value as the default prompt value, but you can work around this issue by setting the default value to a nonsensical value that would not be entered by a user normally, and then modfiying the stored procedure to change the parameter to NULL if it encounters that nonsensical value.


4 Answers

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 @i int 
as
begin
  if @i is null 
    raiserror('The value for @i should not be null', 15, 1) -- with log 

end
GO

Then call:

exec dbo.CheckForNull @i = 1 

or

exec dbo.CheckForNull @i = null 
like image 189
Unsliced Avatar answered Oct 10 '22 07:10

Unsliced


Your code is correct, sensible and even good practice. You just need to wait for SQL Server 2014 which supports this kind of syntax.

After all, why catch at runtime when you can at compile time?

See also this Microsoft document and search for Natively Compiled in there.

As dkrez says, nullability is not considered part of the data type definition. I still wonder why not.

like image 31
Patrick Fromberg Avatar answered Oct 10 '22 07:10

Patrick Fromberg


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 
like image 39
BigBother Avatar answered Oct 10 '22 08:10

BigBother


One reason why you may need such syntax is that, when you use sp in C# dataset GUI wizard, it creates function with nullable parameters if there is no null restriction. No null check in sp body helps it.

like image 24
Kakha Middle Or Avatar answered Oct 10 '22 07:10

Kakha Middle Or