《SQL中缀算术转后缀算术表达式并计算的函数》由会员分享,可在线阅读,更多相关《SQL中缀算术转后缀算术表达式并计算的函数(3页珍藏版)》请在金锄头文库上搜索。
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(1,1),s varchar(100)declare sv table(id int
2、 identity(1,1),v float)select i = 0,j = len(bds),c2 = ,c = while i 0 or (c2 = - and c1 in(,*,-,+,/,()begin select c = c + c2 continue endif c 0begininsert t(s) select s from s where id isnull(select max(id) from s where s in(),0) order by id descdelete s where id = isnull(select max(id) from s where
3、 s in(),0)continueendif charindex(c2,+-)0begininsert t(s) select s from s where id isnull(select max(id) from s where s in(),0) order by id descdelete s where id isnull(select max(id) from s where s in(),0)if c2 0begininsert t(s) select s from s where id isnull(select max(id) from s where s in(,+,-)
4、,0) order by id descdelete s where id isnull(select max(id) from s where s in(,+,-),0)insert s select c2continueendif 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 jbeginselect i = i + 1select c = s f
5、rom t where id = iif c = ( continueif c not in(*,-,+,/) begin insert sv(v) select convert(float,c) continue endselect v2 = v from sv delete sv where id = (select max(id) fromsv)select v1 = v from sv delete sv where id = (select max(id) fromsv)select v = case c when + then v1 + v2 when - then v1 - v2when * then v1 * v2 when / then v1 / v2 endinsert sv(v) select vendselect v = v from svreturn vend-测试示例declare bds varchar(100)select bds = 1+(2+3)*4-9select dbo.js(bds) as func-运行结果/*func-12*/