Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 233
  • Last Modified:

How to allow NULL values in SQL query

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
sqldba2013
Asked:
sqldba2013
3 Solutions
 
David KrollCommented:
It's complaining about O_ID, which you're inserting @v_ActID. Try adding the line:

SET @v_ActID=isnull(@v_ActID,0);
0
 
Randy PooleCommented:
I see you are trying to get a value from #temp without inserting anything into #temp so that column will return a null..
0
 
awking00Commented:
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
NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

 
sqldba2013Author Commented:
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
 
Randy PooleCommented:
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
 
sqldba2013Author Commented:
Thanks to all for your inputs and my issue has been resolved.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now