如何在游标中获取最后处理的行

时间:2018-04-23 23:36:41

标签: sql sql-server tsql sql-server-2012 sql-server-2014

我有以下表格数据

+---------------+----------+-----------+----------+--------+-------+
| BilliedAmount | paidamnt | AccAmount | addAmunt | 1stBC  | 2ndBC |
+---------------+----------+-----------+----------+--------+-------+
|      10358.00 |  1523.55 |   8725.41 |      460 |        |       |
+---------------+----------+-----------+----------+--------+-------+
|        222.00 |   103.84 |    118.16 |    73.76 | 222.00 | 99203 |
+---------------+----------+-----------+----------+--------+-------+
|      10358.00 |  1523.55 |   8725.41 |      460 |        |       |
+---------------+----------+-----------+----------+--------+-------+
|        222.00 |   103.84 |    118.16 |    73.76 |        |       |
+---------------+----------+-----------+----------+--------+-------+

我正在使用游标在这种数据中逐行处理

条件1:

如果BC省的第一个和第二个BC不存在,我需要将这些值插入一个表(BilliedAmount payamnt AccAmount addAmunt)

条件2:

如果第一BC和第二Bc在那里我需要将第一BC和第二Bc插入其他表

我正在使用游标,因此当光标正在处理当前行并访问这些值并将其存储在表中时,我需要检查以前的值(检查第1 BC和第2 Bc值)

请有人帮忙解决这个问题,非常感谢我。 非常感谢提前..

1 个答案:

答案 0 :(得分:1)

不需要使用游标只需使用带有INSERT语句的SELECT语句

insert into newtable1(BilliedAmount, paidamnt, AccAmount, addAmunt)
select BilliedAmount, paidamnt, AccAmount, addAmunt
from table
where 1stBC is null and 2ndBC is null -- 1STBC and 2ndBC are not there

insert into newtable2(1stBC, 2ndBC)
select 1stBC , 2ndBC
from table
where 1stBC is not null and 2ndBC is not null -- 1STBC and 2ndBC are there