游标将值存储在变量中但未在另一个 table Sql 服务器中更新
Cursor is storing values in a variable but not updating in another table Sql Server
我写了一个游标,我在其中使用游标从 table 获取值并在另一个 table 中更新这些值。
在测试时,我分析了值存储在 table 中并且更新也在工作,因为 sql 服务器消息显示 1 行已更新,但游标中的值未在 table
我很困惑,无法得到任何答案
DECLARE
@R_AGE$PATIENT_SEQ_NUM numeric,
@R_AGE$PATIENT_TOTAL_BAL DECIMAL(18,2),
@R_AGE$INS_TOTAL_BAL DECIMAL(18,2),
@R_AGE$PAT_CURRENT_AMOUNT DECIMAL(18,2),
@R_AGE$PAT_ABOVE_30 DECIMAL(18,2),
@R_AGE$PAT_ABOVE_60 DECIMAL(18,2),
@R_AGE$PAT_ABOVE_90 DECIMAL(18,2),
@R_AGE$PAT_ABOVE_120 DECIMAL(18,2),
@R_AGE$INS_CURRENT_AMOUNT DECIMAL(18,2),
@R_AGE$INS_ABOVE_30 DECIMAL(18,2),
@R_AGE$INS_ABOVE_60 DECIMAL(18,2),
@R_AGE$INS_ABOVE_90 DECIMAL(18,2),
@R_AGE$INS_ABOVE_120 DECIMAL(18,2)
DECLARE
GET_AGE CURSOR LOCAL FOR
SELECT
T_PATIENT_AMOUNT_AGE.PATIENT_SEQ_NUM,
T_PATIENT_AMOUNT_AGE.PATIENT_TOTAL_BAL,
T_PATIENT_AMOUNT_AGE.INS_TOTAL_BAL,
T_PATIENT_AMOUNT_AGE.PAT_CURRENT_AMOUNT,
T_PATIENT_AMOUNT_AGE.PAT_ABOVE_30,
T_PATIENT_AMOUNT_AGE.PAT_ABOVE_60,
T_PATIENT_AMOUNT_AGE.PAT_ABOVE_90,
T_PATIENT_AMOUNT_AGE.PAT_ABOVE_120,
T_PATIENT_AMOUNT_AGE.INS_CURRENT_AMOUNT,
T_PATIENT_AMOUNT_AGE.INS_ABOVE_30,
T_PATIENT_AMOUNT_AGE.INS_ABOVE_60,
T_PATIENT_AMOUNT_AGE.INS_ABOVE_90,
T_PATIENT_AMOUNT_AGE.INS_ABOVE_120
FROM T_PATIENT_AMOUNT_AGE
where patient_seq_num=4366999
OPEN GET_AGE
FETCH next from GET_AGE
INTO
@R_AGE$PATIENT_SEQ_NUM,
@R_AGE$PATIENT_TOTAL_BAL,
@R_AGE$INS_TOTAL_BAL,
@R_AGE$PAT_CURRENT_AMOUNT,
@R_AGE$PAT_ABOVE_30,
@R_AGE$PAT_ABOVE_60,
@R_AGE$PAT_ABOVE_90,
@R_AGE$PAT_ABOVE_120,
@R_AGE$INS_CURRENT_AMOUNT,
@R_AGE$INS_ABOVE_30,
@R_AGE$INS_ABOVE_60,
@R_AGE$INS_ABOVE_90,
@R_AGE$INS_ABOVE_120
while @@FETCH_STATUS = 0
BEGIN
UPDATE PATIENT_PROF
SET
PATIENT_TOTAL_BAL = @R_AGE$PATIENT_TOTAL_BAL,
INS_TOTAL_BAL = @R_AGE$INS_TOTAL_BAL,
PATIENT_CURRENT_AMT_DUE = @R_AGE$PAT_CURRENT_AMOUNT,
ABOVE_30 = @R_AGE$PAT_ABOVE_30,
ABOVE_60 = @R_AGE$PAT_ABOVE_60,
ABOVE_90 = @R_AGE$PAT_ABOVE_90,
ABOVE_120 = 5000,
INS_CURRENT_AMT_DUE = @R_AGE$INS_CURRENT_AMOUNT,
INS_ABOVE_30 = @R_AGE$INS_ABOVE_30,
INS_ABOVE_60 = @R_AGE$INS_ABOVE_60,
INS_ABOVE_90 = @R_AGE$INS_ABOVE_90,
INS_ABOVE_120 = @R_AGE$INS_ABOVE_120
WHERE PATIENT_PROF.SEQ_NUM = 4366999
FETCH next from GET_AGE
INTO
@R_AGE$PATIENT_SEQ_NUM,
@R_AGE$PATIENT_TOTAL_BAL,
@R_AGE$INS_TOTAL_BAL,
@R_AGE$PAT_CURRENT_AMOUNT,
@R_AGE$PAT_ABOVE_30,
@R_AGE$PAT_ABOVE_60,
@R_AGE$PAT_ABOVE_90,
@R_AGE$PAT_ABOVE_120,
@R_AGE$INS_CURRENT_AMOUNT,
@R_AGE$INS_ABOVE_30,
@R_AGE$INS_ABOVE_60,
@R_AGE$INS_ABOVE_90,
@R_AGE$INS_ABOVE_120
END
CLOSE GET_AGE
DEALLOCATE GET_AGE
我不清楚您遇到问题的原因,但这确实不需要游标(几乎什么都不需要,除了一些非常小众的场景,它们速度慢且难以编码)。
你可以直接加入更新
UPDATE pp
SET
PATIENT_TOTAL_BAL = pae.PATIENT_TOTAL_BAL,
INS_TOTAL_BAL = pae.INS_TOTAL_BAL,
PATIENT_CURRENT_AMT_DUE = pae.PAT_CURRENT_AMOUNT,
ABOVE_30 = pae.PAT_ABOVE_30,
ABOVE_60 = pae.PAT_ABOVE_60,
ABOVE_90 = pae.PAT_ABOVE_90,
ABOVE_120 = 5000,
INS_CURRENT_AMT_DUE = pae.INS_CURRENT_AMOUNT,
INS_ABOVE_30 = pae.INS_ABOVE_30,
INS_ABOVE_60 = pae.INS_ABOVE_60,
INS_ABOVE_90 = pae.INS_ABOVE_90,
INS_ABOVE_120 = pae.INS_ABOVE_120
FROM PATIENT_PROF pp
JOIN T_PATIENT_AMOUNT_AGE pae ON pae.patient_seq_num = pp.SEQ_NUM
WHERE pp.SEQ_NUM = 4366999;
你可以去掉WHERE
到运行这个整体table。
我写了一个游标,我在其中使用游标从 table 获取值并在另一个 table 中更新这些值。 在测试时,我分析了值存储在 table 中并且更新也在工作,因为 sql 服务器消息显示 1 行已更新,但游标中的值未在 table 我很困惑,无法得到任何答案
DECLARE
@R_AGE$PATIENT_SEQ_NUM numeric,
@R_AGE$PATIENT_TOTAL_BAL DECIMAL(18,2),
@R_AGE$INS_TOTAL_BAL DECIMAL(18,2),
@R_AGE$PAT_CURRENT_AMOUNT DECIMAL(18,2),
@R_AGE$PAT_ABOVE_30 DECIMAL(18,2),
@R_AGE$PAT_ABOVE_60 DECIMAL(18,2),
@R_AGE$PAT_ABOVE_90 DECIMAL(18,2),
@R_AGE$PAT_ABOVE_120 DECIMAL(18,2),
@R_AGE$INS_CURRENT_AMOUNT DECIMAL(18,2),
@R_AGE$INS_ABOVE_30 DECIMAL(18,2),
@R_AGE$INS_ABOVE_60 DECIMAL(18,2),
@R_AGE$INS_ABOVE_90 DECIMAL(18,2),
@R_AGE$INS_ABOVE_120 DECIMAL(18,2)
DECLARE
GET_AGE CURSOR LOCAL FOR
SELECT
T_PATIENT_AMOUNT_AGE.PATIENT_SEQ_NUM,
T_PATIENT_AMOUNT_AGE.PATIENT_TOTAL_BAL,
T_PATIENT_AMOUNT_AGE.INS_TOTAL_BAL,
T_PATIENT_AMOUNT_AGE.PAT_CURRENT_AMOUNT,
T_PATIENT_AMOUNT_AGE.PAT_ABOVE_30,
T_PATIENT_AMOUNT_AGE.PAT_ABOVE_60,
T_PATIENT_AMOUNT_AGE.PAT_ABOVE_90,
T_PATIENT_AMOUNT_AGE.PAT_ABOVE_120,
T_PATIENT_AMOUNT_AGE.INS_CURRENT_AMOUNT,
T_PATIENT_AMOUNT_AGE.INS_ABOVE_30,
T_PATIENT_AMOUNT_AGE.INS_ABOVE_60,
T_PATIENT_AMOUNT_AGE.INS_ABOVE_90,
T_PATIENT_AMOUNT_AGE.INS_ABOVE_120
FROM T_PATIENT_AMOUNT_AGE
where patient_seq_num=4366999
OPEN GET_AGE
FETCH next from GET_AGE
INTO
@R_AGE$PATIENT_SEQ_NUM,
@R_AGE$PATIENT_TOTAL_BAL,
@R_AGE$INS_TOTAL_BAL,
@R_AGE$PAT_CURRENT_AMOUNT,
@R_AGE$PAT_ABOVE_30,
@R_AGE$PAT_ABOVE_60,
@R_AGE$PAT_ABOVE_90,
@R_AGE$PAT_ABOVE_120,
@R_AGE$INS_CURRENT_AMOUNT,
@R_AGE$INS_ABOVE_30,
@R_AGE$INS_ABOVE_60,
@R_AGE$INS_ABOVE_90,
@R_AGE$INS_ABOVE_120
while @@FETCH_STATUS = 0
BEGIN
UPDATE PATIENT_PROF
SET
PATIENT_TOTAL_BAL = @R_AGE$PATIENT_TOTAL_BAL,
INS_TOTAL_BAL = @R_AGE$INS_TOTAL_BAL,
PATIENT_CURRENT_AMT_DUE = @R_AGE$PAT_CURRENT_AMOUNT,
ABOVE_30 = @R_AGE$PAT_ABOVE_30,
ABOVE_60 = @R_AGE$PAT_ABOVE_60,
ABOVE_90 = @R_AGE$PAT_ABOVE_90,
ABOVE_120 = 5000,
INS_CURRENT_AMT_DUE = @R_AGE$INS_CURRENT_AMOUNT,
INS_ABOVE_30 = @R_AGE$INS_ABOVE_30,
INS_ABOVE_60 = @R_AGE$INS_ABOVE_60,
INS_ABOVE_90 = @R_AGE$INS_ABOVE_90,
INS_ABOVE_120 = @R_AGE$INS_ABOVE_120
WHERE PATIENT_PROF.SEQ_NUM = 4366999
FETCH next from GET_AGE
INTO
@R_AGE$PATIENT_SEQ_NUM,
@R_AGE$PATIENT_TOTAL_BAL,
@R_AGE$INS_TOTAL_BAL,
@R_AGE$PAT_CURRENT_AMOUNT,
@R_AGE$PAT_ABOVE_30,
@R_AGE$PAT_ABOVE_60,
@R_AGE$PAT_ABOVE_90,
@R_AGE$PAT_ABOVE_120,
@R_AGE$INS_CURRENT_AMOUNT,
@R_AGE$INS_ABOVE_30,
@R_AGE$INS_ABOVE_60,
@R_AGE$INS_ABOVE_90,
@R_AGE$INS_ABOVE_120
END
CLOSE GET_AGE
DEALLOCATE GET_AGE
我不清楚您遇到问题的原因,但这确实不需要游标(几乎什么都不需要,除了一些非常小众的场景,它们速度慢且难以编码)。
你可以直接加入更新
UPDATE pp
SET
PATIENT_TOTAL_BAL = pae.PATIENT_TOTAL_BAL,
INS_TOTAL_BAL = pae.INS_TOTAL_BAL,
PATIENT_CURRENT_AMT_DUE = pae.PAT_CURRENT_AMOUNT,
ABOVE_30 = pae.PAT_ABOVE_30,
ABOVE_60 = pae.PAT_ABOVE_60,
ABOVE_90 = pae.PAT_ABOVE_90,
ABOVE_120 = 5000,
INS_CURRENT_AMT_DUE = pae.INS_CURRENT_AMOUNT,
INS_ABOVE_30 = pae.INS_ABOVE_30,
INS_ABOVE_60 = pae.INS_ABOVE_60,
INS_ABOVE_90 = pae.INS_ABOVE_90,
INS_ABOVE_120 = pae.INS_ABOVE_120
FROM PATIENT_PROF pp
JOIN T_PATIENT_AMOUNT_AGE pae ON pae.patient_seq_num = pp.SEQ_NUM
WHERE pp.SEQ_NUM = 4366999;
你可以去掉WHERE
到运行这个整体table。