问题
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
回答1:
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
回答2:
Maybe trying this:
ALTER PROCEDURE [dbo].[UserReportData]
@As_ONDATE Datetime
AS
BEGIN
Select * INTO #temptable
FROM
(
select
a.CUser_id,
b.User_Id,
a.U_datetime as REPORTDATE,
ISNULL( b.first_Name + ' ' + b.last_name, 'NONAME' ) as USERNAME -- normally if one of the two is null then all of it is Null
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 @REPORTDATE datetime
-- moved out of the loop
SELECT @REPORTDATE = U_datetime
FROM inward_doc_tracking_trl
where U_datetime >= @As_ONDATE -- this might bring more than one rows I think ???????
DECLARE Cur_1 CURSOR FOR SELECT CUser_id, User_Id, USERNAME FROM #temptable
DECLARE @CUser_id INT
DECLARE @User_Id INT
DECLARE @USERNAME varchar(110)
OPEN Cur_1
FETCH NEXT FROM Cur_1 INTO @CUser_id, @User_Id, @USERNAME
WHILE (@@FETCH_STATUS = 0)
BEGIN
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, @USERNAME
END
CLOSE Cur_1
DEALLOCATE Cur_1
SELECT * FROM #temptable
DROP TABLE #temptable
END
来源:https://stackoverflow.com/questions/37760849/cannot-insert-null-values-into-column-username-table-tempdb-dbo-temptable-e