兩個結構,資料一樣的表,執行一樣的sql,為甚麼效果不一樣
yone 2003-11-21 06:39:53 drop table tmp_profit1
go
drop table tmp2
go
create table tmp_profit1(
id int not null,
subject varchar(30) not null ,
cname varchar(50) null,
ename varchar(50) null,
leaf tinyint null,
primary key clustered(subject)
)
go
INSERT INTO tmp_profit1 VALUES(1,'4','[營業收入類]','[Operating revenue]',0)
INSERT INTO tmp_profit1 VALUES(2,'41','營業收入','Operating revenue',0)
INSERT INTO tmp_profit1 VALUES(3,'4101','銷貨收入','Sales Revenue',1)
INSERT INTO tmp_profit1 VALUES(4,'4102','減:銷貨退回','減:Sales Returns',1)
INSERT INTO tmp_profit1 VALUES(5,'4104','減:銷貨折讓','減:Sales Allowances',1)
INSERT INTO tmp_profit1 VALUES(6,'4105','test of 4105','test of 4105',1)
INSERT INTO tmp_profit1 VALUES(7,'42','營業收入','Operating revenue',0)
go
select * into tmp2 from tmp_profit1
declare @space char(3)
select @space =' '
update a
set cname = @space + a.cname ,ename = @space + a.ename
from tmp2 a, tmp2 b
where substring(a.subject,1,len(b.subject)) = b.subject and len(b.subject) < len(a.subject)
and b.leaf =0
go
declare @space char(3)
select @space =' '
update a
set cname = @space + a.cname ,ename = @space + a.ename
from tmp_profit1 a, tmp_profit1 b
where substring(a.subject,1,len(b.subject)) = b.subject and len(b.subject) < len(a.subject)
and b.leaf =0
go
select * from tmp_profit1
select * from tmp2
go
上面會計科目的一部分,每一級子科目前面加3空格,多級多加.
tmp2是tmp_profit1直接導出的表.執行的sql也一樣,為甚麼結果不一樣.
tmp2所有級別子科目都只加了一次,3個空個.
而tmp_profit1是正確的.