Solved

How to allow NULL values in SQL query

Posted on 2014-07-28
6
208 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
Comment Utility
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
Comment Utility
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 31

Assisted Solution

by:awking00
awking00 earned 166 total points
Comment Utility
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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 

Author Comment

by:sqldba2013
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks to all for your inputs and my issue has been resolved.
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Viewers will learn how the fundamental information of how to create a table.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

763 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

6 Experts available now in Live!

Get 1:1 Help Now