Solved

How to allow NULL values in SQL query

Posted on 2014-07-28
6
220 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Viewers will learn how the fundamental information of how to create a table.

729 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