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?
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.
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.
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.
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
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.
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With