Solved

How to allow NULL values in SQL query

Posted on 2014-07-28
6
216 Views
Last Modified: 2014-08-07
Hi,

I am using ISNULL function to allow NULL values in Insert statement but still I am getting below error. Please suggest how to fix below error or please tell me how to allow null values in SQL query.

Error:
Cannot insert the value NULL into column 'O_ID', table 'dbo.P_Tbl'; column does not allow nulls. INSERT fails.

Query
IF object_id('tempdb..#temp') IS NOT NULL
BEGIN
	DROP TABLE #temp
END
create table #temp(ID INT IDENTITY(1, 1) ,ActID bigint)

DECLARE @v_ActID bigint;
DECLARE @v_Flag int;
DECLARE @v_UsrID bigint;
DECLARE @v_userid bigint;

SET @v_Flag=1;
SET @v_UsrID = 0;
SET @v_userid = 0;

SELECT @v_ActID=ActID  from #temp WITH(NOLOCK) where id=@v_Flag

SET @v_UsrID = 0;

SELECT @v_UsrID=O_ID FROM P_Tbl WITH(NOLOCK) WHERE O_ID=@v_ActID AND UsrID=@v_userid
SET @v_UsrID=isnull(@v_UsrID,0);

IF (@v_UsrID=0)

BEGIN		
																
INSERT INTO P_Tbl (O_ID,UsrID,C_By,M_By,C_Date,ISR)
VALUES (@v_ActID,@v_userid,-1,-1,getdate(),1)

END

Open in new window

0
Comment
Question by:sqldba2013
6 Comments
 
LVL 11

Assisted Solution

by:David Kroll
David Kroll earned 167 total points
ID: 40224479
It's complaining about O_ID, which you're inserting @v_ActID. Try adding the line:

SET @v_ActID=isnull(@v_ActID,0);
0
 
LVL 21

Expert Comment

by:Randy Poole
ID: 40224625
I see you are trying to get a value from #temp without inserting anything into #temp so that column will return a null..
0
 
LVL 32

Assisted Solution

by:awking00
awking00 earned 166 total points
ID: 40224633
INSERT INTO P_Tbl (O_ID,UsrID,C_By,M_By,C_Date,ISR)
VALUES (COALESCE(@v_ActID,0),@v_userid,-1,-1,getdate(),1)
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:sqldba2013
ID: 40225958
Thanks for your suggestion.

I have modified query with above changes and still I am getting same error.
IF object_id('tempdb..#temp') IS NOT NULL
BEGIN
	DROP TABLE #temp
END
create table #temp(ID INT IDENTITY(1, 1) ,ActID bigint)

DECLARE @v_ActID bigint;
DECLARE @v_Flag int;
DECLARE @v_UsrID bigint;
DECLARE @v_userid bigint;

SET @v_Flag=1;
SET @v_UsrID = 0;
SET @v_userid = 0;
SET @v_ActID=isnull(@v_ActID,0);

SELECT @v_ActID=ActID  from #temp WITH(NOLOCK) where id=@v_Flag

SET @v_UsrID = 0;

SELECT @v_UsrID=O_ID FROM P_Tbl WITH(NOLOCK) WHERE O_ID=@v_ActID AND UsrID=@v_userid
SET @v_UsrID=isnull(@v_UsrID,0);

IF (@v_UsrID=0)

BEGIN		
																
INSERT INTO P_Tbl (O_ID,UsrID,C_By,M_By,C_Date,ISR)
VALUES (COALESCE(@v_ActID,0),@v_userid,-1,-1,getdate(),1)

END

Open in new window

0
 
LVL 21

Accepted Solution

by:
Randy Poole earned 167 total points
ID: 40225962
Still you are trying to pull information from #temp and you never place any data into this table...
SELECT @v_ActID=ActID  from #temp WITH(NOLOCK) where id=@v_Flag

Open in new window

@v_ActID is always going to be null..
0
 

Author Closing Comment

by:sqldba2013
ID: 40245966
Thanks to all for your inputs and my issue has been resolved.
0

Featured Post

Backup Solution for AWS

Read about how CloudBerry Backup fully integrates your backups with Amazon S3 and Amazon Glacier to provide military-grade encryption and dramatically cut storage costs on any platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
SQL 2012 AOG and SQL2014 AOG 76 60
Syntax issue with my Where Clause SQL 2012 20 38
Many to one in one row 2 39
Split string into 3 separate fields 5 20
In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question