日期:2014-05-18  浏览次数:20489 次

求一SQL语句update
有一会计科目表
科目编号 科目名称 科目级别 是否末级科目
101 现金 1 0
12201 个人 2 0
1220101 张三 3 0
1220102 李四 3 0
12202 单位 2 0
1220201 AAA公司 3 0
....

科目编号按3,2,2,3共10位数字分4级
要求把是末级科目的【是否末级科目】设为1
没有下一级编号的即位末级科目。

怎么写这个update


------解决方案--------------------
SQL code
create table 表名 (科目编号 varchar(10), 科目名称 varchar(10),科目级别 int,是否末级科目   int)

insert into 表名 select '101'     ,'现金',     1,       0  
insert into 表名 select '12201'   ,'个人',     2,       0  
insert into 表名 select '1220101' ,'张三',     3,       0  
insert into 表名 select '1220102' ,'李四',     3,       0  
insert into 表名 select '12202'   ,'单位',     2,       0  
insert into 表名 select '1220201' ,'AAA公司',  3,       0  

update 表名 set 是否末级科目=0 

--你要求的SQL语句
update 表名 set 是否末级科目=1 
from 表名 as t 
where not exists (select * from 表名 where 科目编号 like t.科目编号 +  '%' and 科目编号 <>t.科目编号)

select * from 表名

drop table 表名