删除表中的单条数据

zhouyulv 2008-05-12 03:27:20
表A是单头:
id,店名, 日期 , 应收金额, 实收金额
01 001 2004-12-26 10:54:37.000 10 10
02 001 2004-12-26 11:54:37.000 11 10
03 002 2004-12-26 12:54:37.000 12 12

----------------------------------------------------

表B是明细表:
id 商品编码 销售数量 销售价 实收金额
01 1001 1 10 10
02 1002 1 5.5 5.5
02 1003 1 5.5 5.5
03 1004 1 12 12
--------------------------------------------- ----------------
我现在想把商品编码为:1003的删除..然后把表A中的应收金额和实收金额更新成删除后的金额.还要加一个日期条件..日期> dateadd(day,-1,getdate())
...全文
211 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhouyulv 2008-05-14
  • 打赏
  • 举报
回复
DELETE t_sale_d
FROM t_sale_d Left Outer Join t_sale_h ON t_sale_d.saleno=t_sale_h.saleno
Left Outer Join t_goods on t_sale_d.itemno=t_goods.itemno
where t_goods.vendorno='20441'and t_sale_h.enddate> dateadd(day,-2,getdate()) and depot='0730002'


zhouyulv 2008-05-13
  • 打赏
  • 举报
回复
我看了下..日期条件.怎么写才好?
zhouyulv 2008-05-13
  • 打赏
  • 举报
回复
搞定了.自己测试了下..可以.
chenjunsheep 2008-05-12
  • 打赏
  • 举报
回复
...偶不想害你啊,帮了你一时,帮不了一世啊,关键是自己掌握~~要明白我的辛苦用心,你懂我的对么。。
muzhenxing013 2008-05-12
  • 打赏
  • 举报
回复
delete b where 商品编码 in ( select 商品编码 from c where 供货商=001)
zhouyulv 2008-05-12
  • 打赏
  • 举报
回复
好的...我是只菜鸟...
chenjunsheep 2008-05-12
  • 打赏
  • 举报
回复
这个表C的做法跟表B的做法其实一个道理的,只要能看懂上面表B的操作,这个问题就很简单了
自己动手,丰衣足食啊,别太懒了
zhouyulv 2008-05-12
  • 打赏
  • 举报
回复
[Quote=引用楼主 zhouyulv 的帖子:]
表A是单头:
id,店名, 日期 , 应收金额, 实收金额
01 001 2004-12-26 10:54:37.000 10 10
02 001 2004-12-26 11:54:37.000 11 10
03 002 2004-12-26 12:54:37.000 12 12

----------------------------------------------------

表B是明细表:
id 商品编码 销售数量 销售价 实收金额
01 1001 1 10 …
[/Quote]
我现在还想加一个条件.表C 记录的是商品信息明细表.
商品编码 商品名 规格 供应商
10001 式 X 001
10002 小 M 006
10003 大 I 001
10004 中 W 002

把供应商编码为:001的销售记录全部删除
chenjunsheep 2008-05-12
  • 打赏
  • 举报
回复
if exists(select * from sysobjects where name = 'A' and xtype = 'U')
drop table A
if exists(select * from sysobjects where name = 'B' and xtype = 'U')
drop table B

create table A(
id nvarchar(2),
店名 nvarchar(4),
日期 datetime,
応収金額 money,
実収金額 money
)
insert into A values('01','001','2004-12-26 10:54:37.000',10,10)
insert into A values('02','001','2004-12-26 11:54:37.000',11,10)
insert into A values('03','002','2004-12-26 12:54:37.000',12,12)

create table B(
id nvarchar(2),
商品编码 nvarchar(4),
销售数量 int,
销售价 money,
实收金额 money
)
insert into B values('01','1001',1,10,10)
insert into B values('02','1002',1,5.5,5.5)
insert into B values('02','1003',1,5.5,5.5)
insert into B values('03','1004',1,12,12)

if exists(select * from sysobjects where name = 'dd' and xtype = 'tr')
drop trigger dd
go
create trigger dd on B for delete
as
declare @id nvarchar(2),@money1 money,@money2 money
select @id = id from deleted
select @money1 = 销售价 from deleted
select @money2 = 实收金额 from deleted

print @id
print @money1
print @money2

update A
set 応収金額 = 応収金額 - @money1,実収金額 = 実収金額 - @money2
where A.id = @id
go
--test date
select * from A
delete from B where 商品编码 = '1003'
select * from A
zhouyulv 2008-05-12
  • 打赏
  • 举报
回复
可以给我一个实例吗?
  • 打赏
  • 举报
回复
顶楼上的
使用触发器来实现
对B表进行Insert和Update动作时,修改A表记录.
chenjunsheep 2008-05-12
  • 打赏
  • 举报
回复
如果我没理解错的话,这个例子用触发器做会方便很多

22,210

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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