记录误删除,请帮忙分析一下,谢谢!

caprason 2011-03-15 11:05:50
两个表字段相同,将tmp表中数据更新至data表中,然后清空tmp表
按照以下代码更新,会出现部分data表中没有的数据被误删除(即没有导入)
            Cmd.CommandText = "select * from tmp where name in (select name from data) and account in (select account from data) and subject in (select subject from data) and iounumber in (select iounumber from data)"
Dr = Cmd.ExecuteReader(CommandBehavior.Default)
If Dr.HasRows = True Then
While Dr.Read
B = CDbl(Dr.Item("贷").ToString())
TA = Dr.Item("account")
TS = Dr.Item("subject")
TC = Dr.Item("iounumber")
C = Dr.Item("name")
Cmd.Connection = Oledb2
Cmd.CommandText = "update data set 贷" & V & "=convert(money,'" & (B) & "') where name='" & (C) & "' and account='" & (TA) & "' and subject='" & (TS) & "' and iounumber='" & (TC) & "'"
Cmd.ExecuteNonQuery()
End While
End If
Dr.Close()
Oledb2.Close()
Cmd.Connection = Oledb1
Cmd.CommandText = "delete from tmp where name in (select name from data) and account in (select account from data) and subject in (select subject from data) and iounumber in (select iounumber from data)"
Cmd.ExecuteNonQuery()
Cmd.CommandText = "insert into data(type,subject,account,name,[currency],IouNumber,贷" & V & ") select type,subject,account,name,[currency],IouNumber,贷 from tmp"
Cmd.ExecuteNonQuery()

问题应该出在delete那句上,但是上面update却没有问题,很奇怪!
...全文
155 22 打赏 收藏 转发到动态 举报
写回复
用AI写文章
22 条回复
切换为时间正序
请发表友善的回复…
发表回复
caprason 2011-03-15
  • 打赏
  • 举报
回复
麻烦AcHerat老大给写句delete吧!谢谢
caprason 2011-03-15
  • 打赏
  • 举报
回复
已经确定问题出在delete那句上了
因为我把清空数据库这句话删除,运行完后查看tmp,结果是空表
caprason 2011-03-15
  • 打赏
  • 举报
回复
[Quote=引用 16 楼 acherat 的回复:]
insert into data(type,subject,account,name,[currency],IouNumber,贷" & V & ") select type,subject,account,name,[currency],IouNumber,贷 from tmp


应该是第一个 贷 后边的 " & V & " 这里,不知道具体是什么意思!如……
[/Quote]
这个V就是个变量,每次导入数据都新建一列,列名就是“贷日期”
AcHerat 元老 2011-03-15
  • 打赏
  • 举报
回复
insert into data(type,subject,account,name,[currency],IouNumber,贷" & V & ") select type,subject,account,name,[currency],IouNumber,贷 from tmp


应该是第一个 贷 后边的 " & V & " 这里,不知道具体是什么意思!如果有多个贷的列,后边有跟区别的字符,那么V应该在这些区别字符范围内,如果超过会插入不成功。
caprason 2011-03-15
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 acherat 的回复:]
你需要删除的是这四个字段都和data里的四个字段值相同的嘛?
[/Quote]
对的
大部分的iounumber都是空值
caprason 2011-03-15
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 acherat 的回复:]
那就where吧!把你需要符合更新的条件放后边。

update a
set a.a = b.b,a.c = c.c,a.d = c.d
from b,c,d
where a.bid = b.id
and a.cid = b.id
and a.did = d.id
and ......
[/Quote]
update没问题了,就是新记录有的被加入,有的没有被加入
gw6328 2011-03-15
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 caprason 的回复:]
引用 1 楼 jinfengyiye 的回复:
select * from tmp where name in (select name from data) and account in (select account from data) and subject in (select subject from data) and iounumber in (select iounumber ……
[/Quote]

应该是10楼那种条件吧。四项相符。
AcHerat 元老 2011-03-15
  • 打赏
  • 举报
回复
你需要删除的是这四个字段都和data里的四个字段值相同的嘛?
caprason 2011-03-15
  • 打赏
  • 举报
回复
比如有条记录
subject name account iounumber 贷
123456 123456 1111111 77 111111
如果data中有了一条subject,name,account相同,但iounumber不同的记录
那就有可能不导入,这个很郁闷啊!
我总觉得问题出在那句delete上,因为当数据库为空时,导入一天的数据就是对的
一旦数据库里已有一天数据的情况下,再做导入就会发生上面的情况
AcHerat 元老 2011-03-15
  • 打赏
  • 举报
回复
那就where吧!把你需要符合更新的条件放后边。

update a
set a.a = b.b,a.c = c.c,a.d = c.d
from b,c,d
where a.bid = b.id
and a.cid = b.id
and a.did = d.id
and ......
caprason 2011-03-15
  • 打赏
  • 举报
回复
额~表连接和inner join不会用
刚才查了查inner join
是不是会产生一个新的查询集?查询集怎么更新进表里?
AcHerat 元老 2011-03-15
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 caprason 的回复:]

引用 6 楼 herowang 的回复:
首先Cmd.CommandText 的sql语句写的就很纠结

既然用tmp中的数据更新data中的数据,那么直接
update data from tmp
set
where 。首先在数据库中先测试下自己的sql语句

谢谢,是很纠结,原来是打算用update data from tmp set XXXX=(select .... wh……
[/Quote]

用表连接吧! where 或者 inner join。。。
caprason 2011-03-15
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 herowang 的回复:]
首先Cmd.CommandText 的sql语句写的就很纠结

既然用tmp中的数据更新data中的数据,那么直接
update data from tmp
set
where 。首先在数据库中先测试下自己的sql语句
[/Quote]
谢谢,是很纠结,原来是打算用update data from tmp set XXXX=(select .... where 。
但是很容易出错,因为记录可能的非唯一性,字段的转换等
  • 打赏
  • 举报
回复
首先Cmd.CommandText 的sql语句写的就很纠结

既然用tmp中的数据更新data中的数据,那么直接
update data from tmp
set
where 。首先在数据库中先测试下自己的sql语句
caprason 2011-03-15
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 acherat 的回复:]
select * from tmp where name in (select name from data) and account in (select account from data) and subject in (select subject from data) and iounumber in (select iounumber from data)

看下这四个select……
[/Quote]
Dr = Cmd.ExecuteReader(CommandBehavior.Default)
超时已过期
AcHerat 元老 2011-03-15
  • 打赏
  • 举报
回复
select * from tmp where name in (select name from data) and account in (select account from data) and subject in (select subject from data) and iounumber in (select iounumber from data)

看下这四个select里有没有null的数据产生! 加 isnull(字段名,默认值)限定


select * from tmp where name in (select isnull(name,'') from data) and account in (select isnull(account,0) from data) and subject in (select isnull(subject,'') from data) and iounumber in (select isnull(iounumber,0) from data)
caprason 2011-03-15
  • 打赏
  • 举报
回复
me,account,subject,iounumber四项每个单项都有重复记录
但四项核对一定只有一条记录
caprason 2011-03-15
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 jinfengyiye 的回复:]
select * from tmp where name in (select name from data) and account in (select account from data) and subject in (select subject from data) and iounumber in (select iounumber from data)
这一句你是要这种效果吗?
[/Quote]
这个还没确认过数据,这句话想要的效果是:
name,account,subject,iounumber四项都相符才更新“贷”
但iounumber并不是每条数据都有,前面三项每条数据都有
gw6328 2011-03-15
  • 打赏
  • 举报
回复
select * from tmp where name in (select name from data) and account in (select account from data) and subject in (select subject from data) and iounumber in (select iounumber from data)
这一句你是要这种效果吗?
caprason 2011-03-15
  • 打赏
  • 举报
回复
[Quote=引用 21 楼 acherat 的回复:]
delete from tmp where name in (select name from data) and account in (select account from data) and subject in (select subject from data) and iounumber in (select iounumber from data)

改为

delete ……
[/Quote]
谢谢!现在已经通过inner join结合delete解决了
delete from tmp from tmp inner join data on tmp.name=data.name and ......
又学到了很多东西啊,谢谢LS的诸位了!

20楼的广告去死吧
加载更多回复(1)

34,593

社区成员

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

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