Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

How to make input parameters mandatory in Stored Procedure ? Expand / Collapse
Author
Message
Posted Thursday, August 28, 2008 7:24 AM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Friday, September 16, 2011 3:37 AM
Points: 54, Visits: 148
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 ?

Post #560409
Posted Thursday, August 28, 2008 7:39 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: 2 days ago @ 9:09 AM
Points: 10,342, Visits: 13,352
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.



Jack Corbett

Applications Developer

Don't let the good be the enemy of the best. -- Paul Fleming

Check out these links on how to get faster and more accurate answers:
Forum Etiquette: How to post data/code on a forum to get the best help
Need an Answer? Actually, No ... You Need a Question
How to Post Performance Problems
Crosstabs and Pivots or How to turn rows into columns Part 1
Crosstabs and Pivots or How to turn rows into columns Part 2
Post #560433
Posted Thursday, August 28, 2008 7:43 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Monday, November 17, 2014 12:50 PM
Points: 13,872, Visits: 9,598
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
Post #560442
Posted Friday, August 29, 2008 4:12 AM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Friday, September 16, 2011 3:37 AM
Points: 54, Visits: 148
Thanks to All.
Post #561084
Posted Friday, August 29, 2008 4:16 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Thursday, February 21, 2013 11:46 AM
Points: 19, Visits: 100
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.


Post #561557
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse