【问题标题】:how to get last processed rows in cursor如何获取游标中最后处理的行
【发布时间】:2018-10-04 02:32:39
【问题描述】:

我在下面有这样的表格数据

+---------------+----------+-----------+----------+--------+-------+
| 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 |        |       |
+---------------+----------+-----------+----------+--------+-------+

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

条件一:

如果 1st BC 和 2nd BC 不存在,我需要将此值插入到一个表中(BilliedAmountpaidamnt AccAmount addAmunt)

条件二:

如果有 1st BC 和 2nd Bc,我需要将 1st BC 和 2nd Bc 插入到其他表中

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

哪位大神能帮忙解决一下,万分感谢。 非常感谢提前..

【问题讨论】:

  • 公元前 1 年和公元前 2 年是什么意思?它们是列还是行?
  • 你能告诉我们你的光标查询吗?
  • 我不清楚你在问什么以及你的数据是如何挂在一起的。我观察到 4 行中有重复值,但我无法猜测 1stbc 和 2ndbc 应该表示什么或我应该如何处理重复值。

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


【解决方案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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多