• 工作总结
  • 工作计划
  • 心得体会
  • 述职报告
  • 思想汇报
  • 发言讲话稿
  • 演讲稿
  • 申请书
  • 读后感
  • 报告材料
  • 策划方案
  • 当前位置: 写作资料库 > 其他范文 > 正文

    [查询实现删除]sql语句实现级联删除

    时间:2018-08-15 16:32:01 来源:写作资料库 本文已影响 写作资料库手机站

    if object_id("t0807") is not null

    drop table t0807

    go

    create table t0807(AutoID int identity(1,1) primary key,[id] int,username varchar(10))

    go

    insert into t0807

    select 1,"1" union all

    select 4,"1" union all

    select 4,"2" union all

    select 6,"2" union all

    select 2,"3" union all

    select 7,"3"

    go

    --主键AutoID

    delete from t0807

    where AutoID not in

    (

    --查找出最小值

    select AutoID from t0807 a

    where [id]<=

    (select min([id]) from t0807 b where a.username=b.username)

    )

    go

    select * from t0807

    (所影响的行数为 6 行)

    (所影响的行数为 3 行)

    AutoID id username

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

    1 1 1

    3 4 2

    5 2 3

    (所影响的行数为 3 行)