表A
code igrade
50101 3
50102 3
5010101 4
501010101 5
401010101 5表B
id code cformula
0001 50101 QM(50101,月,借)
0001 50101 QM(50101,月,借)
0001 5010101 QM(5010101,月,借)
0001 501010101 QM(501010101,月,借)
0002 401010101 QM(401010101,月,借) 我需要将表A为501开头的科目改成5开头 去掉二三位的01 相应的igrade字段减1
表B的code字段跟表A一样 后面的cformula字段里的相应code字段也改变 只修改id=0001的记录
修改后的表如下
表A
code igrade
501 2
502 2
50101 3
5010101 4
401010101 5表B
id code cformula
0001 501 QM(501,月,借)
0001 502 QM(501,月,借)
0001 50101 QM(50101,月,借)
0001 5010101 QM(5010101,月,借)
0002 401010101 QM(401010101,月,借)
code igrade
50101 3
50102 3
5010101 4
501010101 5
401010101 5表B
id code cformula
0001 50101 QM(50101,月,借)
0001 50101 QM(50101,月,借)
0001 5010101 QM(5010101,月,借)
0001 501010101 QM(501010101,月,借)
0002 401010101 QM(401010101,月,借) 我需要将表A为501开头的科目改成5开头 去掉二三位的01 相应的igrade字段减1
表B的code字段跟表A一样 后面的cformula字段里的相应code字段也改变 只修改id=0001的记录
修改后的表如下
表A
code igrade
501 2
502 2
50101 3
5010101 4
401010101 5表B
id code cformula
0001 501 QM(501,月,借)
0001 502 QM(501,月,借)
0001 50101 QM(50101,月,借)
0001 5010101 QM(5010101,月,借)
0002 401010101 QM(401010101,月,借)
select (left(code,1) + right(code,len(code)-3)) as code ,(igrade-1) as igrade from 表A where code like '501%'
表B那个,说说思路吧, 通过表A得到原CODE与更改后的CODE值,然后根据表B的CODE再与表A联接,再更新cformula 值..(用replace)
go
create table [A]([code] varchar(10),[igrade] int)
insert [A]
select 50101,3 union all
select 50102,3 union all
select 5010101,4 union all
select 501010101,5 union all
select 401010101,5
goupdate a
set code=stuff(code,1,3,'5'),igrade=igrade-1
goselect * from a
/**
code igrade
---------- -----------
501 2
502 2
50101 3
5010101 4
5010101 4(所影响的行数为 5 行)
**/
update a set code= case when left(code,3)='501' then (left(code,1) + right(code,len(code)-3)) else code end, igrade= igrade-1
go
create table [A]([code] varchar(10),[igrade] int)
insert [A]
select 50101,3 union all
select 50102,3 union all
select 5010101,4 union all
select 501010101,5 union all
select 401010101,5
goupdate a set [code]=case when left([code],3)=501 then stuff(code,1,3,'5') else [code] end,igrade=igrade-1
select * from a
/*
code igrade
---------- -----------
501 2
502 2
50101 3
5010101 4
401010101 4(5 行受影响)*/
go
create table [B]([id] varchar(4),[code] int,[cformula] varchar(19))
insert [B]
select '0001',50101,'QM(50101,月,借)' union all
select '0001',50101,'QM(50101,月,借)' union all
select '0001',5010101,'QM(5010101,月,借)' union all
select '0001',501010101,'QM(501010101,月,借)' union all
select '0002',401010101,'QM(401010101,月,借)'
goupdate b
set
code=stuff(code,1,3,'5'),
cformula=stuff(cformula,4,3,'5')
where id='0001'
goselect * from b
/**
id code cformula
---- ----------- -------------------
0001 501 QM(501,月,借)
0001 501 QM(501,月,借)
0001 50101 QM(50101,月,借)
0001 5010101 QM(5010101,月,借)
0002 401010101 QM(401010101,月,借)(所影响的行数为 5 行)
**/
set code=stuff(code,1,3,'5'),igrade=igrade-1
where left(code,1)='5'
CASE WHEN LEFT(code,3)='501' THEN igrade=igrade-1 ELSE igrade END AS igrade
FROM tbA