1、
delete from company t1
where t1.ifend=0 and 
   not exists (select 1 
               from department t2  
               where t2.departmentid like t1.companyid +'%')
删除DEPARTMENT中的记录也一样。2、
delete from department t1
where not exists (select 1 from company t2  where t1.departmentid like t2.companyid +'%')
删除Employee中的记录也一样。

解决方案 »

  1.   


    谢谢 icevi(按钮工厂),解释一下好吗?
    delete from company t1   --“t1”是什么意思呀?
    where t1.ifend=0 and 
       not exists (select 1  --“1”是什么意思?
                   from department t2  
                   where t2.departmentid like t1.companyid +'%')
    我在运行时报错“Incorrect syntax near 't1'.”
      

  2.   

    1、 delete company where ifend=0 and 
      not exists(select DepartmentID from Department where DepartmentID like Company.CompanyID+'%')
      delete Department where ifend=0 and 
      not exists(select EmployeeID from Employee where EmployeeID like Department.DepartmentID+'%')2 delete Employee 
          from Employee a,Department b 
          where a.EmployeeID not like b.DepartmentID+'%'
      delete Department
          from Department a,company b 
          where a.DepartmentID not like b.companyID+'%'
    -----------------------------------------------------------
    未经调试,大意是这样。
      

  3.   

    看来你的表的引用完整性有问题。没有定义引用关系。删除一遍不一定可以保证完整。
    从逻辑上考虑,先作2
    这个应该从上往下进行:
    delete Department d
    where not exixts (select CompanyID from  Company c
                      where  d.DepartmentID like c.CompanyID  +'%')delete Employee e
    where not exixts (select DepartmentID from  Department d
                      where e.EmployeeID like d.DepartmentID + '%')1.你的这个删除逻辑上应该是从下往上进行的。
    delete Department d
    where not exists(select DepartmentID from Employee e
                     where e.EmployeeID like d.DepartmentID + '%')
          and d.ifned = 0delete Company c
    where not exists(select CompanyID from Department d
                     where d.DepartmentID like c.CompanyID  + '%')
          and c.ifned = 0大概就是这样,没有测试。试一试吧。--“t1”是什么意思呀? 这个是起一个别名。
      

  4.   

    delete t1
      from company ti,department t2  
    where t1.ifend=0 and t2.departmentid not like t1.companyid +'%'
    --------------------------------------------------------
    我替按钮改改。
      

  5.   

    你的问题中为什么用 like CompanyID,应该是 = CompanyID吧。
      

  6.   


    谢谢各位,我在问题中是想表达的意思是like CompanyID+'%',因为CompanyID为0001,DepartmentID为000101所以DepartmentID like CompanyID+'%'.