SQL2000数据库,如何计算guige乘以数量后的结果,以select id,guige,shuliang,(guige*shuliang) as heji 的方式选出?
产品信息规格是这样的方式,怎么已上面的方式选出呢?
ID guige shuliang
1 1*5 5.2
2 1.0/5 3
3 1/5 5
4 2*5+3 8
5 (8-5)*3 -5.213
------解决方案--------------------
- SQL code
--> 测试数据:[tbl]goif object_id('[tbl]') is not null drop table [tbl]gocreate table [tbl]([ID] int,[guige] varchar(11),[shuliang] numeric(5,3))goinsert [tbl]select 1,'1*5',5.2 union allselect 2,'1.0/5',3 union allselect 3,'1/5',5 union allselect 4,'2*5+3',8 union allselect 5,'(8-5)*3',-5.213declare @minid intdeclare @maxid intselect @minid=MIN(id) from tblselect @maxid=max(id) from tblgodeclare @sql varchar(max)set @sql=''declare @minid intselect @minid=MIN(ID) from tbldeclare @maxid intselect @maxid=max(ID) from tbldeclare @str varchar(1000)set @str=''while @minid<[email protected]beginselect @str='('+[guige]+')' from tbl where [email protected]select @str='select id,guige,shuliang,'[email protected]+'*[shuliang] as hejifrom tbl where ID='+cast(@minid as varchar)print @strset @[email protected]+' union all [email protected]set @[email protected]+1endprint @sqlset @sql=RIGHT(@sql,LEN(@sql)-10)exec(@sql)/*id guige shuliang heji1 1*5 5.200 26.0000000002 1.0/5 3.000 0.6000000003 1/5 5.000 0.0000000004 2*5+3 8.000 104.0000000005 (8-5)*3 -5.213 -46.917000000*/
------解决方案--------------------
- SQL code
declare @T table (ID int,guige char(7),shuliang decimal(18,3))insert into @Tselect 1,'1*5',5.2 union allselect 2,'1.0/5',3 union allselect 3,'1/5',5 union allselect 4,'2*5+3',8 union allselect 5,'(8-5)*3',-5.213select *,dbo.m_charcompute(guige+'*('+ltrim(shuliang)+')') as newcolfrom @T/*ID guige shuliang newcol----------- ------- --------------------------------------- ----------------------1 1*5 5.200 262 1.0/5 3.000 0.63 1/5 5.000 14 2*5+3 8.000 345 (8-5)*3 -5.213 -46.917*/create function [dbo].[m_charcompute](@bds varchar(1000))returns floatasBEGINset @bds = replace(@bds,' ','')--去空格,免得麻烦。declare @i int,@j int declare @c1 char(1),@c2 char(1),@c varchar(100)declare @v1 float,@v2 float,@v floatdeclare @t table(id int identity(1,1),s varchar(100))declare @s table(id int identity(1,1),s varchar(100))declare @sv table(id int identity(1,1),v float)select @i = 0,@j = len(@bds),@c2 = '',@c = ''while @i<@jbegin select @c1 = @c2,@i = @i+1 select @c2 = substring(@bds,@i,1)if charindex(@c2,'.0123456789') > 0 or (@c2 = '-' and @c1 in('','*','-','+','/','(')) begin select @c = @c + @c2 continue end if @c <> '' begin insert @t(s) select @c select @c = '' end if charindex(@c2,')')>0 begin insert @t(s) select s from @s where id > isnull((select max(id) from @s where s in('(')),0) order by id desc delete @s where id >= isnull((select max(id) from @s where s in('(')),0) continue end if charindex(@c2,'+-)')>0 begin insert @t(s) select s from @s where id > isnull((select max(id) from @s where s in('(')),0) order by id desc delete @s where id > isnull((select max(id) from @s where s in('(')),0) if @c2 <> ')' insert @s(s) select @c2 continue end if charindex(@c2,'*/')>0 begin insert @t(s) select s from @s where id > isnull((select max(id) from @s where s in('(','+','-')),0) order by id desc delete @s where id > isnull((select max(id) from @s where s in('(','+','-')),0) insert @s select @c2 continue end if charindex(@c2,'(')>0 insert @s select @c2endif @c <> '' insert @t(s) select @cinsert @t(s) select s from @s order by id descselect @i = 0,@j = max(id) from @t while @i < @jbegin select @i = @i + 1 select @c = s from @t where id = @i if @c = '(' continue if @c not in('*','-','+','/') begin insert @sv(v) select convert(float,@c) continue end select @v2 = v from @sv delete @sv where id = (select max(id) from @sv) select @v1 = v from @sv delete @sv where id = (select max(id) from @sv) select @v = case @c when '+' then @v1 + @v2 when '-' then @v1 - @v2 when '*' then @v1 * @v2 when '/' then @v1 / @v2 end insert @sv(v) select @vendselect @v = v from @svreturn @vend