Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot insert NULL values into column 'USERNAME', table 'tempdb.dbo.#temptable error

I have a SP. while executing I am getting error as

Cannot insert the value NULL into column 'USERNAME', table 'tempdb.dbo.#temptable__________________________________________________________________________________________________________0000000002FD'; column does not allow nulls. UPDATE fails. The statement has been terminated.

Below is my SP

ALTER PROCEDURE [dbo].[UserReportData] 
                @As_ONDATE Datetime 

                AS 
            BEGIN 
                    DECLARE @REPORTDATE datetime        
                    DECLARE @USERNAME varchar(110)      

            Select * INTO #temptable
        FROM
                    (
                        select  a.CUser_id, b.User_Id, a.U_datetime
                        as REPORTDATE, b.first_Name + ' '  + b.last_name as USERNAME
                        from inward_doc_tracking_trl a inner join user_mst b
                        on a.CUser_id = b.mkey
                        and a.U_datetime >= @As_ONDATE
                ) as x

DECLARE Cur_1 CURSOR
            FOR SELECT CUser_id, User_Id FROM #temptable

                    OPEN Cur_1
                        DECLARE @CUser_id INT
                        DECLARE @User_Id INT
                        FETCH NEXT FROM Cur_1 
                        INTO @CUser_id, @User_Id

                        WHILE (@@FETCH_STATUS = 0)
            BEGIN

                            SELECT @REPORTDATE = U_datetime FROM inward_doc_tracking_trl                        
                            where  U_datetime >= @As_ONDATE                             



                            UPDATE #temptable
                                SET REPORTDATE = @REPORTDATE,
                                    USERNAME = @USERNAME
                                WHERE CUser_id = @CUser_id
                                AND User_Id = @User_Id  


                FETCH NEXT FROM Cur_1 INTO @CUser_id, @User_Id
                    END
            CLOSE Cur_1
            DEALLOCATE Cur_1


            SELECT * FROM #temptable
                        DROP TABLE #temptable                           
    END

UPDATED PROCEDURE

ALTER PROCEDURE [dbo].[UserReportData] 
                @As_ONDATE Datetime 

                AS 
            BEGIN 
                    DECLARE @REPORTDATE datetime        
                    DECLARE @USERNAME varchar(110)      

            Select * INTO #temptable
        FROM
                    (
                        select  a.CUser_id, b.User_Id, a.U_datetime
                        as REPORTDATE, ISNULL(b.first_Name, '') + ' ' + ISNULL(b.last_name, '') AS USERNAME                         
                        from inward_doc_tracking_trl a inner join user_mst b
                        on a.CUser_id = b.mkey
                        and a.U_datetime >= @As_ONDATE
                ) as x

DECLARE Cur_1 CURSOR
            FOR SELECT CUser_id, User_Id FROM #temptable

                    OPEN Cur_1
                        DECLARE @CUser_id INT
                        DECLARE @User_Id INT
                        --DECLARE @USERNAME VARCHAR (100)
                        FETCH NEXT FROM Cur_1 
                        INTO @CUser_id, @User_Id

                        WHILE (@@FETCH_STATUS = 0)
            BEGIN

                            SELECT @REPORTDATE = U_datetime FROM inward_doc_tracking_trl                        
                            where  U_datetime >= @As_ONDATE 

                            SELECT @USERNAME = ISNULL(b.first_Name, '') + ' ' + ISNULL(b.last_name, '') 
                                FROM inward_doc_tracking_trl a 
                                INNER JOIN user_mst b on a.CUser_id = b.mkey 
                                where a.U_datetime >= @As_ONDATE                            

                            UPDATE #temptable
                                SET REPORTDATE = @REPORTDATE,
                                    USERNAME = @USERNAME
                                WHERE CUser_id = @CUser_id
                                AND User_Id = @User_Id  


                FETCH NEXT FROM Cur_1 INTO @CUser_id, @User_Id
                    END
            CLOSE Cur_1
            DEALLOCATE Cur_1


            SELECT * FROM #temptable
                        DROP TABLE #temptable                           
    END
like image 659
Nad Avatar asked Jan 22 '26 19:01

Nad


1 Answers

First comment the @USERNAME declaration in the initial BEGIN block

 -- DECLARE @USERNAME varchar(110)

then handle the NULL value for the USERNAME as in the SELECT * INTO block

 ISNULL(b.first_Name, '') + ' ' + ISNULL(b.last_name, '') AS USERNAME

Then add the @USERNAME in the CURSOR as

DECLARE Cur_1 CURSOR
        FOR SELECT CUser_id, User_Id, USERNAME FROM #temptable

            OPEN Cur_1
                    DECLARE @CUser_id INT
                    DECLARE @User_Id INT
                    DECLARE @USERNAME VARCHAR (200) -- can set your required length
                    FETCH NEXT FROM Cur_1 
                    INTO @CUser_id, @User_Id

Then the @USERNAME won't throw the NULL error


UPDATE: Based on your comments, I updated the answer:

So you want to get the @USERNAME based on the U_datetime >= @As_ONDATE condition.

So use your existing code and add one more block inside the CURSOR for getting the @USERNAME value as after the SELECT @REPORTDATE = U_datetime FROM inward_doc_tracking_trl where U_datetime >= @As_ONDATE

SELECT @USERNAME = ISNULL(b.first_Name, '') + ' ' + ISNULL(b.last_name, '') -- no need of column alias here
FROM inward_doc_tracking_trl a 
INNER JOIN user_mst b on a.CUser_id = b.mkey 
WHERE a.U_datetime >= @As_ONDATE
like image 83
Arulkumar Avatar answered Jan 24 '26 13:01

Arulkumar