有个问题,请教!

zephyr_liyo 2003-11-10 04:13:31
请问一下,如果有两张表A和B
A表中有字段 column1,column2
B表中有字段 column2,column3 【A.column2和B.column2是对应的】
如果我想删除B中的一行数据条件是对应在A表中的column1字段值为<...>
请问这样的delete 语句怎么写;【应用于存储过程,希望不要用游标cursor功能】
高分请教!
...全文
29 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
txlicenhe 2003-11-10
  • 打赏
  • 举报
回复
A表中有字段 column1,column2
B表中有字段 column2,column3 【A.column2和B.column2是对应的】
如果我想删除B中的一行数据条件是对应在A表中的column1字段值为<...>
请问这样的delete 语句怎么写;【应用于存储过程,希望不要用游标cursor功能】
高分请教!


delete b from a join b on a.column2 = b.column2 where a.column1 = 'abc'
hdslah 2003-11-10
  • 打赏
  • 举报
回复

delete b from a where a.column2=b.column2 and a.column1=<...>
hdslah 2003-11-10
  • 打赏
  • 举报
回复
delete b from a inner join b on a.column2=b.column2 where a.column1=<...>
zjcxc 元老 2003-11-10
  • 打赏
  • 举报
回复
--创建数据测试环境
declare @a table(column1 int,column2 int)
insert into @a
select 1,11
union all select 2,22
union all select 3,33

declare @b table(column2 int,column3 int)
insert into @b
select 11,1
union all select 11,2
union all select 22,2
union all select 33,3

--删除方法1,删除与a表中对应column1=1的记录
delete from @b where column2 in(select column2 from @a where column1=1)

--显示结果:
select * from @b

--删除方法2,删除与a表中对应column1=3的记录
delete @b
from @a a inner join @b b on a.column2=b.column2
where a.column1=3

--显示结果:
select * from @b




/*--测试结果:

column2 column3
----------- -----------
22 2
33 3

(所影响的行数为 2 行)


(所影响的行数为 1 行)

column2 column3
----------- -----------
22 2

(所影响的行数为 1 行)
--*/
zjcxc 元老 2003-11-10
  • 打赏
  • 举报
回复
--或:

delete b
from a inner join b on a.column2=b.column2
where a.column1=1
zjcxc 元老 2003-11-10
  • 打赏
  • 举报
回复
delete from b where column2 in(select column2 from a where column1=<...>)
zephyr_liyo 2003-11-10
  • 打赏
  • 举报
回复
请不要说用视图,因为表名经常变,我还要做视图维护的内容,太不爽!
还有有没有人用存储过程生成触发器的???,给个例子好不好??
zephyr_liyo 2003-11-10
  • 打赏
  • 举报
回复
如果用cursor功能,我可以在A表中先检索出满足条件的column2值,再根据column2去删除B表中的数据,这样是不是会很慢,如果数据量比较大的情况下???所以我不是很喜欢用这样的东西。能不能找到直接的sql语句,比如delete B where ...这样的!

34,874

社区成员

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

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