Solved

How to allow NULL values in SQL query

Posted on 2014-07-28
6
210 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
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 

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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Suggested Solutions

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
I have a large data set and a SSIS package. How can I load this file in multi threading?
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Viewers will learn how the fundamental information of how to create a table.

895 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now