更新时间:2023-11-30 20:18:34
首先注释初始 BEGIN
块中的@USERNAME 声明
First comment the @USERNAME declaration in the initial BEGIN
block
-- DECLARE @USERNAME varchar(110)
然后像在 SELECT * INTO
块中一样处理 USERNAME
的 NULL
值
then handle the NULL
value for the USERNAME
as in the SELECT * INTO
block
ISNULL(b.first_Name, '') + ' ' + ISNULL(b.last_name, '') AS USERNAME
然后在 CURSOR 中添加 @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
那么 @USERNAME
就不会抛出 NULL
错误
Then the @USERNAME
won't throw the NULL
error
更新:根据您的评论,我更新了答案:
UPDATE: Based on your comments, I updated the answer:
所以您想根据 U_datetime >= @As_ONDATE
条件获取 @USERNAME
.
So you want to get the @USERNAME
based on the U_datetime >= @As_ONDATE
condition.
因此,使用您现有的代码并在 CURSOR 中再添加一个块以获取 @USERNAME
值,就像 SELECT @REPORTDATE = U_datetime FROM inward_doc_tracking_trl where U_datetime >= @As_ONDATE代码>
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