`
xys_777
  • 浏览: 206282 次
  • 性别: Icon_minigender_1
  • 来自: 大连
社区版块
存档分类
最新评论

[sql server] 一个有意思的公式计算

 
阅读更多

实际贴:http://topic.csdn.net/u/20100720/15/21f9044a-4457-4a95-bf60-018e3b4413c8.html?seed=1454200561&r=67106612#r_67106612

--建立测试环境
IF OBJECT_ID('tb1') IS NOT NULL DROP TABLE tb1
GO
CREATE TABLE tb1
(
FLAG varchar(10),
VALUE int
)
GO
INSERT TB1
SELECT 'A' , 2 union all
SELECT 'B' , 3 union all
SELECT 'D' , 4
go
IF OBJECT_ID('tb2') IS NOT NULL DROP TABLE tb2
GO
CREATE TABLE tb2
(
FLAG varchar(10),
COMP varchar(20)
)
GO
INSERT TB2
SELECT 'Loa' , 'A*B' union all
SELECT 'BWC' , 'Loa*D'
go
--查询
IF OBJECT_ID('f_test') IS NOT NULL DROP function f_test
GO
create function f_test(@s varchar(20))
returns varchar(20)
as
begin
--declare @rt varchar(20)
select @s=replace(@s,FLAG,COMP) from tb2 where charindex(FLAG,@s)>0
select @s=replace(@s,FLAG,VALUE) from tb1 where charindex(FLAG,@s)>0
return @s
end
go


declare @s varchar(100)
select @s=isnull(@s+' union all ','')+'select '''+FLAG+''' as [flag],'+dbo.f_test(COMP)+' as comp' from tb2
exec(@s)
--结果
/*
flag comp
---- -----------
Loa 6
BWC 24
*/

资料

/*-------------建立环境-----------*/
create table a
(unit_code
varchar(10),item_id varchar(10),tdatetime datetime,actdata decimal(10,1))

insert a values ( '0001 ', '10 ', '2002-01-01 ',1230.0)
insert a values ( '0001 ', '1001 ', '2002-01-01 ',3341.0)
insert a values ( '0001 ', '1002 ', '2002-01-01 ',6341.0)
insert a values ( '0001 ', '100101 ', '2002-01-01 ',619168.0)
insert a values ( '0001 ', '100102 ', '2002-01-01 ',619.0)
insert a values ( '0001 ', '100110 ', '2002-01-01 ',2230.0)
insert a values ( '0001 ', '100111 ', '2002-01-01 ',34311.0)
insert a values ( '0001 ', '2001 ', '2003-01-01 ',206705280.0)
insert a values ( '0001 ', '200101 ', '2003-01-01 ',3434.2)
insert a values ( '0001 ', '20010201 ', '2003-01-01 ',842.0)
insert a values ( '0001 ', '2002 ', '2003-01-01 ',384.0)


create table b
(expressions_code
varchar(10),expressions_describe varchar(10),expressions varchar(30),
gradation
int)

insert b values ( '0101 ', '比率1 ', '1001/200101 ',1)
insert b values ( '0102 ', '比率2 ', '(1001-100111-100110)/200101 ',2)
insert b values ( '0103 ', '比率3 ', '(100101+100102)/200101 ',3)
insert b values ( '0104 ', '资本1 ', '1001-200101 ',4)
insert b values ( '0105 ', '资本2 ', '2002+20010201-1002 ',5)
insert b values ( '0201 ', '负债1 ', '2001/10 ',6)

create table c
(expressions_code
varchar(10),expressions_end decimal(10,2))
go
/*---------建立函数来分割字符串-------------*/
create function f_split(@s varchar(8000),@split varchar(10))
returns @re table(col1 int identity(1,1),col2 varchar(100))
as
begin
declare @i int
set @i=len(@split+ 'a ')-2
while charindex(@split,@s)> 0
begin
insert into @re values (left(@s,charindex(@split,@s)-1)+ ', ')
set @s=stuff(@s,1,charindex(@split,@s)+@i, ' ')
end
insert into @re values (@s+ ', ')
return
end
go


/*--------------用游标来处理字符串中的公式计算----------------*/
declare t_cursor cursor for
select expressions_code,expressions from b
declare @sql varchar(100),@s1 varchar(800),@s2 varchar(800),@s3 decimal(10,2)
open t_cursor
fetch next from t_cursor into @s1,@s2
select @sql=@s2

select @sql=replace(@sql,a,b)
from
(
select a= '( ',b= ' ' union all
select a= ') ',b= ' ' union all
select a= '- ',b= ', ' union all
select a= '+ ',b= ', ' union all
select a= '/ ',b= ', ' union all
select a= '* ',b= ', '
) a

select @s2=replace(@s2,a,b)+ ', '
from
(
select a= '( ',b= '(, ' union all
select a= ') ',b= ',) ' union all
select a= '- ',b= ',-, ' union all
select a= '+ ',b= ',+, ' union all
select a= '/ ',b= ',/, ' union all
select a= '* ',b= ',*, '
) a
print @s2
select @s2=case when charindex(col2,@s2)> 0 then replace(@s2,col2, '
(select actdata from a where item_id=
' ' '+col2+ ' ' ') ') end from dbo.f_split(@sql, ', ')
select @s2= 'insert c select ' ' '+@s1+ ' ' ',(select ( '+replace(@s2, ', ', ' ')+ ')) '
exec(@s2)

while @@FETCH_STATUS=0
begin
fetch next from t_cursor into @s1,@s2
select @sql=@s2

select @sql=replace(@sql,a,b)
from
(
select a= '( ',b= ' ' union all
select a= ') ',b= ' ' union all
select a= '- ',b= ', ' union all
select a= '+ ',b= ', ' union all
select a= '/ ',b= ', ' union all
select a= '* ',b= ', '
) a

select @s2=replace(@s2,a,b)+ ', '
from
(
select a= '( ',b= '(, ' union all
select a= ') ',b= ',) ' union all
select a= '- ',b= ',-, ' union all
select a= '+ ',b= ',+, ' union all
select a= '/ ',b= ',/, ' union all
select a= '* ',b= ',*, '
) a
print @s2
select @s2=case when charindex(col2,@s2)> 0 then replace(@s2,col2, '
(select actdata from a where item_id=
' ' '+col2+ ' ' ') ') end from dbo.f_split(@sql, ', ')
select @s2= 'insert c select ' ' '+@s1+ ' ' ',(select ( '+replace(@s2, ', ', ' ')+ ')) '
exec(@s2)
end
close t_cursor
deallocate t_cursor


/*---------显示结果------------------*/
select * from c


/*----------删除环境-----------------*/
drop table c
drop function f_split
drop table b
drop table a


/*------------结果-------------------*/
expressions_code expressions_end
---------------- ---------------
0101 .97
0102 -9.67
0103 180.47
0104 -93.20
0105 -5115.00
0201 168053.07

(所影响的行数为
6 行)

分享到:
评论

相关推荐

    SQL Server Native Client 10,以便于SQLserver高版本可以链接SQLserver2000

    压缩包内附带链接服务器创建脚本方式,此SQL Server Native Client 10.0无病毒,有64位和32位可供选择。...安装完SQL Server Native Client 10.0后再创建个链接服务器,可以实现高版本SQLserver远程链接SQLserver2000。

    SQLServer+ 免安装版

    SQLServer+ 免安装版 SQLServer+是在原有SQLServer2000的基础上改善了数据库安装的繁锁性,让软件企业在发布基于SQLServer2000数据库软件的时候,只要把软件打包进入安装包而不需要再单独安装数据库,也不需要另外...

    Microsoft SQL Server 2008 R2 SP1 Native Client

    适用 SQL Server Native Client 的这个可转散发安装程序会安装运行时间期间需要的客户端组件,以利用 SQL Server 2008 新功能,并选择性地安装开发使用 SQL Server Native Client API 之应用程序时所需要的头文件。

    sqlserver根据经纬度计算两点间距离

    sqlserver根据经纬度计算两点间距离sqlserver根据经纬度计算两点间距离sqlserver根据经纬度计算两点间距离sqlserver根据经纬度计算两点间距离

    [SQL Server] Microsoft SQL Server 2012 技术内幕 (英文版)

    [Microsoft Press] Microsoft SQL Server 2012 技术内幕 (英文版) [Microsoft Press] Microsoft SQL Server 2012 Internals (E-Book) ☆ 图书概要:☆ Dive deep inside the architecture of SQL Server 2012 ...

    SQLSERVER计算年龄(岁月天).txt

    SQLSERVER计算年龄(岁月天),可以精确到岁,月,几月几天。

    完美SQL Server绿色版

    SQLServer文件, 否则后果自负。作者不为您承担任何方面的任何责任。 SQL Server 2000绿色版注意事项 --------------------------- 1 本地连接服务器请使用界面中“服务器名”文本框中的文本作为服务器名连接...

    SQLServer 根据生日计算年龄

    SQLServer 根据生日计算年龄

    SQL Server 2014基础入门视频教程 (40集,含课件)

    SQL Server 2014基础入门视频教程 (40集,含课件) 1.SQL Server 2014简介.mp4 2.SQL Server 2014硬件和软件要求.mp4 3.SQL Server 2014数据库安装.mp4 4.SQL Server 2014数据库创建.mp4 5.SQL Server 2014...

    SQL Server 2008 Native Client(32&64)

    Microsoft sql server 2008 Native Client (SQL Server Native Client) 是一个同时包含 SQL OLE DB 访问接口和 SQL ODBC 驱动程序的动态链接库 (DLL)。它对使用本机代码 API(ODBC、OLE DB 和 ADO)连接 Microsoft ...

    SQLServer中如何将一个字段的多个记录值合在一行显示

    SQLServer 中将一个字段的多个记录值合并到一行显示的实现方法 SQL Server 是一种关系型数据库管理系统,具有强大的数据处理能力和存储能力。在实际应用中,我们经常需要将一个字段的多个记录值合并到一行显示,以...

    SqlServer连接工具

    SqlServer连接工具SqlServer连接工具SqlServer连接工具

    sqlserver自动生成sql语句工具sqlserver转oracle

    sqlserver自动生成sql语句工具sqlserver转oracle

    利用MysqlODBC把Sqlserver数据库导入到Mysql中

    将mysql数据库转换为sql server的数据库,或者将sql server数据库转换...这里介绍一个使用sql的mmc的方法 ,将sql server的数据转化为mysql的数据库,将源和目的反之,就可以将mysql的数据库转化为sql server的数据库。

    支持navicate 连接 sqlserver 2019 的 驱动sqlserver native client 11.0

    支持navicate 连接 sqlserver 2019 的 驱动sqlserver native client 11.0 ,亲测可用,ssms自带的不能连接,会报远程关闭错误。

    sqlserver4.2 jar包

    支持的 Java 版本: Java Runtime Environments (JRE) 的...• Microsoft SQL Server 2005 - 仅受适用于 SQL Server 的 Microsoft JDBC Driver 4.0 支持 • Azure SQL 数据库 • Azure SQL 数据仓库或并行数据仓库

    Microsoft SQL Server Native Client (SQL Native Client)

    Microsoft SQL Server Native Client (SQL Native Client) 是一个同时包含 SQL OLE DB 访问接口和 SQL ODBC 驱动程序的动态链接库 (DLL)。它对使用本机代码 API(ODBC、OLE DB 和 ADO)连接到 Microsoft SQL Server ...

    利用KEPSERVER6 实现写入SQLSERVER.docx

    KEPSERVER6 实现写入 SQLSERVER 的详细步骤 KEPSERVER6 是一款工业自动化数据采集和监控软件,广泛应用于工业自动化、机器人、物联网等领域。随着工业自动化和物联网的发展,KEPSERVER6 的应用变得越来越广泛。今天...

    适用于 SQL Server 2014、2012、2008R2的JDBC Drivers 4.1

    适用于Microsoft SQL Server 2014、SQL Server 2012、SQL Server 2008 R2、SQL Server 2008、SQL Server 2005 和 SQL Azure。对于适用于 SQL Server 的 Microsoft JDBC Driver 4.1,将从 SQL Server 2008 开始支持。...

    sql server ce server tools

    这个组件用于把移动设备中的SQL Server Mobile连接到SQL Server 2005、SQL Server 2000 SP3a、及 SQL Server 2000 SP4数据库。 2. Microsoft SQL Server 2000 Service Pack 4复制组件(sql2kxxsp4.msi)在IIS机器中...

Global site tag (gtag.js) - Google Analytics