[posted and mailed, please reply in news]
Nachi (nachikethm@yahoo.com) writes:
[quoted text, click to view] > I am getting above error,
>
> could pls anyone help me........?
First, before you actually fix the error - keep it! What you need is
to improve your error handling, so that you can get useful diagnosis
from SQL Server. You need to have an On Error Goto, and in that part
you pick what is in the .Errors collection on the connection object,
and then print this out. (One message is also in the plain Err.Description.)
In this case you probaly would have gotten the error "Cannot insert the
value NULL into column 'Discount', table 'Current_SalesRefund_Discount';
column does not allow nulls. INSERT fails."
Why? Hang on. But first another issue:
[quoted text, click to view] > CREATE proc sp_insert_CatAmount @TransID bigint, @CatID int
Don't use the sp_ prefix for stored procedure. This prefix is reserved
for system procedures, and SQL Server first looks for these in the master
database.
[quoted text, click to view] > if @Amount = null
>
> set @Amount = 0
But this will not have any effect, because @Amout is never equal to NULL,
because nothing is ever equal to NULL. NULL is an unknown value, so you
can't tell whether two NULLs are the same unknown value or not.
When testing for NULL, you should use IS NULL or IS NOT NULL instead.
In this particular case, it would have been more convenient to write:
[quoted text, click to view] > insert into
> Current_SalesRefund_Discount (
> TransactionNo,
> CategoryID,
> Discount)
> values (
> @TransID,
> @CatID, coalesce(@Amount, 0) )
coalesce is a function that accepts a list of values and returns the
first non-NULL value in the list.
--
Erland Sommarskog, SQL Server MVP, sommar@algonet.se
Books Online for SQL Server SP3 at