SQL Server Performance Forum – Threads Archive
inserting nullsHi, I have a stored prcedure for an insert, how can I set it up so that if a certain form field returns no value that a NULL is inserted into the database?
Make NULL default for those parameters that could be NULL.
CREATE PROCEDURE get_sales_for_title
@title varchar(80) = NULL, — NULL default value
@ytd_sales int OUTPUT
AS — Validate the @title parameter.
IF @title IS NULL
PRINT ‘ERROR: You must specify a title value.’
END — Get the sales for the specified title and
— assign it to the output parameter.
SELECT @ytd_sales = ytd_sales
WHERE title = @title RETURN
Taken from BOL.
If nothing is passed, the default value will be entered, if something is passed this value will be entered Frank
Don’t forget to allow null in database field.
Luis Martin …Thus mathematics may be defined as the subject in which we never know what we are talking about, nor whether what we are saying is true.
Don’t know if this is applicable in his perticular case but you can also use the IsNull-function: INSERT INTO table (SomeNumber) VALUES (IsNull(@Number, 0)) —
"Real programmers don’t document, if it was hard to write it should be hard to understand"
Thanks guys, so am I right in thinking if I used @title varchar(80) = NULL and a value was passed it would use the value and if no value was passed then it would enter NULL? if so this is exactly what I was looking for. Thanks again
Yes, you’re right on that. As for Frettmaestro:
0 is not NULL Frank