我有一个SP。执行时我收到错误
无法将值NULL插入列'USERNAME',表'tempdb.dbo。#temptable __________________________________________________________________________________________________________ 0000000002FD';列不允许空值。更新失败。 声明已经终止。
以下是我的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
更新程序
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
答案 0 :(得分:1)
首先在初始BEGIN
块
-- DECLARE @USERNAME varchar(110)
然后处理NULL
的{{1}}值,如同USERNAME
块一样
SELECT * INTO
然后将CURSOR中的 ISNULL(b.first_Name, '') + ' ' + ISNULL(b.last_name, '') AS USERNAME
添加为
@USERNAME
然后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
不会抛出@USERNAME
错误
更新:根据您的评论,我更新了答案:
因此,您希望根据NULL
条件获取@USERNAME
。
因此,请使用现有代码并在CURSOR中再添加一个块,以便在U_datetime >= @As_ONDATE
@USERNAME
值
SELECT @REPORTDATE = U_datetime FROM inward_doc_tracking_trl where U_datetime >= @As_ONDATE
答案 1 :(得分:0)
也许尝试这个:
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