• sql server 返回计算表达式的函数


    下面为您介绍一个SQL中返回计算表达式的函数,该函数可以计算加、减、乘、除,但是不能算幂,供您参考,希望对您学习SQL中的函数能有有所帮助。

    CREATE FUNCTION [dbo].[GetExp](@pstrExpress AS VARCHAR(8000))
    RETURNS DECIMAL(18,6) AS
    BEGIN
    DECLARE @i INT,@j INT 
    DECLARE @c1 CHAR(1),@c2 CHAR(1),@c VARCHAR(100)
    DECLARE @v1 DECIMAL(18,6),@v2 DECIMAL(18,6),@v DECIMAL(18,6)
    DECLARE @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 DECIMAL(18,6))
    
    SET @pstrExpress = REPLACE(@pstrExpress,' ','')
    SELECT @i = 0,@j = LEN(@pstrExpress),@c2 = '',@c = ''
    WHILE @i<@j
    BEGIN
     SELECT @c1 = @c2,@i = @i+1
     SELECT @c2 = SUBSTRING(@pstrExpress,@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 @c2
    END
    IF @c <> '' INSERT @t(s) SELECT @c
    INSERT @t(s)  SELECT s FROM @s ORDER BY ID DESC
    SELECT @i = 0,@j = MAX(ID) FROM @t 
    WHILE @i < @j
    BEGIN 
     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 @v
    END
    SELECT @v = v FROM @sv
    RETURN @v
    END
    
  • 相关阅读:
    JAVA SE 基础复习-虚拟机
    不错的博客
    如何分析问题
    由Contains开始的
    相关有用链接
    C++语法一二
    关于论文的些许想法
    C++中一些容易迷惑的语法点总结
    android中各种组件的生命周期问题
    android开源框架thinkinandroid相关研究
  • 原文地址:https://www.cnblogs.com/robinli/p/1915521.html
Copyright © 2020-2023  润新知