资源描述:
《sql中缀算术转后缀算术表达式并计算的函数》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、--创建函数(原创:cson_cson)create function dbo.js(@bds varchar(1000))returns floatasbegindeclare @i int,@j intdeclare @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(
2、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('','*','-','+','/','('
3、)) 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 >
4、= 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
5、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 wh
6、ere 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 @twhile @i < @jbegin select @i = @i + 1 select @
7、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(i
8、d) from @sv) select @v = case @c when '+' then @v1 + @v2 when '-' then @v1 - @v2