--两个表结构
create table T1(gongshi varchar(250),shuzhi numeric(18, 2))
create table T2(vid varchar(50),shuzhi numeric(18, 2))--测试数据
insert into T1 values('=code(101)',0)
insert into T1 values('=code(102)',0)
insert into T1 values('=code(101)+code(102)',0)insert into T2 values('101',1000)
insert into T2 values('102',500)--要求实现(根据表2的数据update表T1的值,如下:)
gongshi shuzhi =code(101) 1000
=code(102) 500
=code(101)+code(102) 1500
create table T1(gongshi varchar(250),shuzhi numeric(18, 2))
create table T2(vid varchar(50),shuzhi numeric(18, 2))--测试数据
insert into T1 values('=code(101)',0)
insert into T1 values('=code(102)',0)
insert into T1 values('=code(101)+code(102)',0)insert into T2 values('101',1000)
insert into T2 values('102',500)--要求实现(根据表2的数据update表T1的值,如下:)
gongshi shuzhi =code(101) 1000
=code(102) 500
=code(101)+code(102) 1500
from #T1 M
LEFT JOIN #T2 N
ON CHARINDEX(N.VID,M.gongshi)>0
group by m.gongshi
order by M.gongshi
gongshi shuzhi
------------ ---------------------------------------
=code(101) 1000.00
=code(101)+code(102) 1500.00
=code(102) 500.00(3 row(s) affected)
create table T1(gongshi varchar(250),shuzhi numeric(18, 2))
create table T2(vid varchar(50),shuzhi numeric(18, 2))
--测试数据
insert into T1 values('=code(101)',0)
insert into T1 values('=code(102)',0)
insert into T1 values('=code(101)+code(102)',0)
insert into T2 values('101',1000)
insert into T2 values('102',500)
select a.gongshi,
sum(b.shuzhi)shuzhi
from t1 a left join t2 b
on a.gongshi like '%('+b.vid+')%'
group by a.gongshi
order by shuzhi
drop table t1,t2
/*
gongshi shuzhi
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ---------------------------------------
=code(102) 500.00
=code(101) 1000.00
=code(101)+code(102) 1500.00(3 行受影响)*/
set T.shuzhi=M.shuzhi
from #T1 T
LEFT JOIN
(
select M.gongshi,sum(N.shuzhi)shuzhi
from #T1 M
LEFT JOIN #T2 N
ON CHARINDEX(N.VID,M.gongshi)>0
group by m.gongshi
)M
ON T.gongshi=M.gongshi-------
=code(101) 1000.00
=code(102) 500.00
=code(101)+code(102) 1500.00
create table T1(gongshi varchar(250),shuzhi numeric(18, 2))
create table T2(vid varchar(50),shuzhi numeric(18, 2))
--测试数据
insert into T1 values('=code(101)',0)
insert into T1 values('=code(102)',0)
insert into T1 values('=code(101)+code(102)',0)
insert into T2 values('101',1000)
insert into T2 values('102',500)update t1 set shuzhi=isnull((select sum(shuzhi)
from t2
where t1.gongshi like '%('+t2.vid+')%'),shuzhi)
select * from t1
drop table t1,t2
http://blog.csdn.net/wufeng4552/archive/2009/11/25/4868138.aspx
create table T2(vid varchar(50),shuzhi numeric(18, 2))--测试数据
insert into T1 values('=code(101)',0)
insert into T1 values('=code(102)',0)
insert into T1 values('=code(101)+code(102)',0)insert into T2 values('101',1000)
insert into T2 values('102',500)
create function [dbo].[code](
@code int
)
returns numeric(18, 2)
as
begindeclare @result numeric(18, 2)select @result = (select top 1 shuzhi from t2 where vid =
cast(@code as varchar(8)))
return @result
enddeclare @sql varchar(1000)
update t1 set gongshi = replace(gongshi,'code','dbo.code')
set @sql = ''
select @sql = @sql + ' update t1 set shuzhi ' + gongshi
+ ' where gongshi = ''' + gongshi + ''''
from t1select @sqlexec(@sql)
update t1 set gongshi = replace(gongshi,'dbo.code','code')
select * from t1