菜鸟求一个SQL2000关于删除数据的语句,呵呵

zuoxxx 2006-02-22 12:27:26
我是个新手
我的问题是
表一为用户表,其它若干个表为那些用户的各种记录,我想知道的是能不能用一条语句,在删除某个用户的时候,其他表也自动就把该用户的记录删除掉了.....谢谢各位了..我该怎么做呢??希望能给小弟讲详细些,,,偶是个菜菜新手,呵呵

表一: 表名userInfo,主键是number
number(int) name sex college …
20041111 小明 男 艺术学院 …
20040000 小张 男 工管学院 …
20043333 小红 女 文法学院 …
…… … … … …
表二: 表名workInfo
number(int) job time pay
20040000 售货 5小时 100元
20040000 家教 2小时 50元
20041111 兼职 2小时 35元
20040000 兼职 2小时 35元
…… … … …
表三: 表名helpInfo
number(int) content money time
20041111 贫困补助 100元 2月1日
20043333 生病补助 100元 1月20日
…… …… … …
表四:…
…………
表五:…
……
...全文
84 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
zlp321002 2006-02-22
  • 打赏
  • 举报
回复
--如:删除为主键number为:20041111 的数据,操作如下:

declare @oldstr varchar(100)
set @oldstr='20041111' --删除的字符串
declare @s varchar(8000)
declare tb cursor local for
select s='if exists(select 1 from ['+b.name+'] where ['+a.name+'] like ''%'+@oldstr+'%'')
delete ['+b.name+'] where ['+a.name+']='''+@oldstr+''' '
from syscolumns a join sysobjects b on a.id=b.id
where b.xtype='U' and a.status>=0
and a.xusertype in(175,239,231,167)
open tb
fetch next from tb into @s
while @@fetch_status=0
begin
exec(@s)
fetch next from tb into @s
end
close tb
deallocate tb
zlp321002 2006-02-22
  • 打赏
  • 举报
回复
--先用语句,找到删除的字符,然后,拼SQL,来批量处理删除.

类似如下操作:

declare @oldstr varchar(100)
set @oldstr='要删除的字符' --删除的字符串
declare @s varchar(8000)
declare tb cursor local for
select s='if exists(select 1 from ['+b.name+'] where ['+a.name+'] like ''%'+@oldstr+'%'')
delete ['+b.name+'] where ['+a.name+']='''+@oldstr+''' '
from syscolumns a join sysobjects b on a.id=b.id
where b.xtype='U' and a.status>=0
and a.xusertype in(175,239,231,167)
open tb
fetch next from tb into @s
while @@fetch_status=0
begin
exec(@s)
fetch next from tb into @s
end
close tb
deallocate tb


hongfof 2006-02-22
  • 打赏
  • 举报
回复
写一个触发器就行了
Teng_s2000 2006-02-22
  • 打赏
  • 举报
回复
在数据库中建立一个约束也可以,实现级联删除,级联更新
yesyesyes 2006-02-22
  • 打赏
  • 举报
回复
两表建立关系,级联删除
lcooc 2006-02-22
  • 打赏
  • 举报
回复
收藏

34,575

社区成员

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

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