关于可更新游标、不导入重复记录的问题(急)

am2004 2005-03-25 08:36:03
一个关于可更新游标、不导入重复记录的问题

请各位高人指点:
有两个结构相同的表table1、table2,有字段c1、c2、c3。
我在存储过程中定义了一个游标:
declare cur_t cursor for select a.c1,a.c2 from table1 a,table b
where a.c1<>b.c2 and a.c2<>b.c2
for update of a.c3
这个游标的select部分实现将表table1中有而table2中没有的记录(只要字段c1和c2都相同则认为是相同的记录)检索出来,
我在存储过程的另一部分通过游标对表table1进行更新:
update table1 set c3='s' where current of cur_t
操作目的是将游标中的记录导入table2成功后将相应table1中记录的c3字段写入字符‘s’

我想问:
1、定义的游标从多个表中读数据时是否该游标自动变为readonly,因为我在运行上面的存储过程时出错,提示为我的游标被定义为readonly,不可update table。游标从单个数据表中读数据时顺利通过。
2、忙了一天头晕脑涨,都糊涂了,请教从一个表导入数据时不导入重复记录的语句实现(多个字段都相同才算是相同记录)——糊涂了呀~~~~~~~~~~
...全文
120 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
pbsql 2005-03-25
  • 打赏
  • 举报
回复
可能是表未建立主键
am2004 2005-03-25
  • 打赏
  • 举报
回复
我单个表时可以不出现在select 列表里呀?
pbsql 2005-03-25
  • 打赏
  • 举报
回复
import_status应出现在前面的select里的列表里:
declare cur_IFDB10_car_series cursor
for select
a.factory_code,
a.base_car_series, a.comments,
a.import_status
...
yizhixiaozhu 2005-03-25
  • 打赏
  • 举报
回复
帮你up
am2004 2005-03-25
  • 打赏
  • 举报
回复
一楼的高人,我按你写的改了游标,可还是:

服务器: 消息 16957,级别 16,状态 4,过程 sp_IF2012,行 26
在 READ ONLY 游标上不能指定 FOR UPDATE。

我的游标定义如下:
--定义游标变量
declare cur_IFDB10_car_series cursor
for select
a.factory_code,
a.base_car_series,
a.comments
from IFDB10_car_series a--,IFDB20_car_series b
where
a.import_date in --取导入日期最大
(select max(import_date) from IFDB10_car_series)
and not exists
(select * from IFDB20_car_series b where
a.factory_code=b.factory_code
and a.base_car_series=b.base_car_series ) --如果这两个字段都相等则不选取
for update of a.import_status
pbsql 2005-03-25
  • 打赏
  • 举报
回复
1.定义的游标从多个表中读数据时该游标自动变为readonly,改SQL语句:
declare cur_t cursor for
select c1,c2,c3 from table1 a
where not exists(select * from table2 b where a.c1=b.c2 and a.c2=b.c2)
for update of a.c3
2.其实可以不要游标的:
update table1 set c3='s'
where not exists(select * from table2 b where table1.c1=b.c2 and table1.c2=b.c2)

34,588

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧