sql中参数过多利用变量替换参数的方法_MsSql

工作中遇到一个需求,需要非常多的参数,例如如下sql,

复制代码 代码如下:

select
ff.fundsc||'-'||ff.fundtzfs||'-'||ff.fundcjfl||'-'||ff.fundonefl||'-'||ff.fundtowfl catagory,
sf.scode,replace(sf.fund5,'型证券投资基金','')fund5,sf.fund4,sf.fund10,
(select to_date(tradedate,'yyyy-MM-dd') from sdc_fundnetassetvalue where scode=sf.scode and tradedate in
(select max(tradedate) from sdc_fundnetassetvalue where scode=sf.scode and to_date(tradedate,'yyyyMMdd')>=(to_date(20120801,'yyyyMMdd')-10) and tradedate<=20120801) and rownum<2) trdatedate,
(select unitvalue from sdc_fundnetassetvalue where scode=sf.scode and tradedate in
(select max(tradedate) from sdc_fundnetassetvalue where scode=sf.scode and to_date(tradedate,'yyyyMMdd')>=(to_date(20120801,'yyyyMMdd')-10) and tradedate<=20120801) and rownum<2) fejz,
(select accumulatedunitvalue from sdc_fundnetassetvalue where scode=sf.scode and tradedate in
(select max(tradedate) from sdc_fundnetassetvalue where scode=sf.scode and to_date(tradedate,'yyyyMMdd')>=(to_date(20120801,'yyyyMMdd')-10) and tradedate<=20120801) and rownum<2) feljjz,
(select f30004_30002 from si_fund_30002 where f_fundcode=sf.scode and f_type=0 and f_tradingdate in
(select max(f_tradingdate) from si_fund_30002 where f_fundcode=sf.scode and to_date(f_tradingdate,'yyyyMMdd')>=(to_date(20120801,'yyyyMMdd')-10) and f_tradingdate<=20120801) and rownum<2) gqyzjzzzl,
(select f30006_30002 from si_fund_30002 where f_fundcode=sf.scode and f_type=0 and f_tradingdate in
(select max(f_tradingdate) from si_fund_30002 where f_fundcode=sf.scode and to_date(f_tradingdate,'yyyyMMdd')>=(to_date(20120801,'yyyyMMdd')-10) and f_tradingdate<=20120801) and rownum<2) gqyyjzzzl,
decode((select f30007_30002 from si_fund_30002 where f_fundcode=sf.scode and f_type=0 and f_tradingdate in
(select max(f_tradingdate) from si_fund_30002 where f_fundcode=sf.scode and to_date(f_tradingdate,'yyyyMMdd')>=(to_date(20120801,'yyyyMMdd')-10) and f_tradingdate<=20120801) and rownum<2),
null,'-',to_char((select f30007_30002 from si_fund_30002 where f_fundcode=sf.scode and f_type=0 and f_tradingdate in
(select max(f_tradingdate) from si_fund_30002 where f_fundcode=sf.scode and to_date(f_tradingdate,'yyyyMMdd')>=(to_date(20120801,'yyyyMMdd')-10) and f_tradingdate<=20120801) and rownum<2),'fm999999990.9999')) gqsgyjzzzl,
row_number() over(partition by ff.fundsc||'-'||ff.fundtzfs||'-'||ff.fundcjfl||'-'||ff.fundonefl||'-'||ff.fundtowfl
order by decode((select f30007_30002 from si_fund_30002 where f_fundcode=sf.scode and f_type=0 and f_tradingdate in
(select max(f_tradingdate) from si_fund_30002 where f_fundcode=sf.scode and to_date(f_tradingdate,'yyyyMMdd')>=(to_date(20120801,'yyyyMMdd')-10) and f_tradingdate<=20120801) and rownum<2),
null,'-',to_char((select f30007_30002 from si_fund_30002 where f_fundcode=sf.scode and f_type=0 and f_tradingdate in
(select max(f_tradingdate) from si_fund_30002 where f_fundcode=sf.scode and to_date(f_tradingdate,'yyyyMMdd')>=(to_date(20120801,'yyyyMMdd')-10) and f_tradingdate<=20120801) and rownum<2),'fm999999990.9999')) desc) sgy,
decode((select f30008_30002 from si_fund_30002 where f_fundcode=sf.scode and f_type=0 and f_tradingdate in
(select max(f_tradingdate) from si_fund_30002 where f_fundcode=sf.scode and to_date(f_tradingdate,'yyyyMMdd')>=(to_date(20120801,'yyyyMMdd')-10) and f_tradingdate<=20120801) and rownum<2),
null,'-',to_char((select f30008_30002 from si_fund_30002 where f_fundcode=sf.scode and f_type=0 and f_tradingdate in
(select max(f_tradingdate) from si_fund_30002 where f_fundcode=sf.scode and to_date(f_tradingdate,'yyyyMMdd')>=(to_date(20120801,'yyyyMMdd')-10) and f_tradingdate<=20120801) and rownum<2),'fm999999990.9999')) gqbnjzzzl,
row_number() over(partition by ff.fundsc||'-'||ff.fundtzfs||'-'||ff.fundcjfl||'-'||ff.fundonefl||'-'||ff.fundtowfl
order by decode((select f30008_30002 from si_fund_30002 where f_fundcode=sf.scode and f_type=0 and f_tradingdate in
(select max(f_tradingdate) from si_fund_30002 where f_fundcode=sf.scode and to_date(f_tradingdate,'yyyyMMdd')>=(to_date(20120801,'yyyyMMdd')-10) and f_tradingdate<=20120801) and rownum<2),
null,'-',to_char((select f30008_30002 from si_fund_30002 where f_fundcode=sf.scode and f_type=0 and f_tradingdate in
(select max(f_tradingdate) from si_fund_30002 where f_fundcode=sf.scode and to_date(f_tradingdate,'yyyyMMdd')>=(to_date(20120801,'yyyyMMdd')-10) and f_tradingdate<=20120801) and rownum<2),'fm999999990.9999')) desc) bn,
decode((select F30017_30003 from si_fund_30003 where f_fundcode=sf.scode and f_type=0 and f_tradingdate in
(select max(f_tradingdate) from si_fund_30003 where f_fundcode=sf.scode and to_date(f_tradingdate,'yyyyMMdd')>=(to_date(20120801,'yyyyMMdd')-10) and f_tradingdate<=20120801) and rownum<2),
null,'-',to_char((select F30017_30003 from si_fund_30003 where f_fundcode=sf.scode and f_type=0 and f_tradingdate in
(select max(f_tradingdate) from si_fund_30003 where f_fundcode=sf.scode and to_date(f_tradingdate,'yyyyMMdd')>=(to_date(20120801,'yyyyMMdd')-10) and f_tradingdate<=20120801) and rownum<2),'fm999999990.9999')) jinnian,
row_number() over(partition by ff.fundsc||'-'||ff.fundtzfs||'-'||ff.fundcjfl||'-'||ff.fundonefl||'-'||ff.fundtowfl
order by decode((select F30017_30003 from si_fund_30003 where f_fundcode=sf.scode and f_type=0 and f_tradingdate in
(select max(f_tradingdate) from si_fund_30003 where f_fundcode=sf.scode and to_date(f_tradingdate,'yyyyMMdd')>=(to_date(20120801,'yyyyMMdd')-10) and f_tradingdate<=20120801) and rownum<2),
null,'-',to_char((select F30017_30003 from si_fund_30003 where f_fundcode=sf.scode and f_type=0 and f_tradingdate in
(select max(f_tradingdate) from si_fund_30003 where f_fundcode=sf.scode and to_date(f_tradingdate,'yyyyMMdd')>=(to_date(20120801,'yyyyMMdd')-10) and f_tradingdate<=20120801) and rownum<2),'fm999999990.9999')) desc) jn,
decode((select f30009_30002 from si_fund_30002 where f_fundcode=sf.scode and f_type=0 and f_tradingdate in
(select max(f_tradingdate) from si_fund_30002 where f_fundcode=sf.scode and to_date(f_tradingdate,'yyyyMMdd')>=(to_date(20120801,'yyyyMMdd')-10) and f_tradingdate<=20120801) and rownum<2),
null,'-',to_char((select f30009_30002 from si_fund_30002 where f_fundcode=sf.scode and f_type=0 and f_tradingdate in
(select max(f_tradingdate) from si_fund_30002 where f_fundcode=sf.scode and to_date(f_tradingdate,'yyyyMMdd')>=(to_date(20120801,'yyyyMMdd')-10) and f_tradingdate<=20120801) and rownum<2),'fm999999990.9999')) gqynjzzzl,
row_number() over(partition by ff.fundsc||'-'||ff.fundtzfs||'-'||ff.fundcjfl||'-'||ff.fundonefl||'-'||ff.fundtowfl
order by decode((select f30009_30002 from si_fund_30002 where f_fundcode=sf.scode and f_type=0 and f_tradingdate in
(select max(f_tradingdate) from si_fund_30002 where f_fundcode=sf.scode and to_date(f_tradingdate,'yyyyMMdd')>=(to_date(20120801,'yyyyMMdd')-10) and f_tradingdate<=20120801) and rownum<2),
null,'-',to_char((select f30009_30002 from si_fund_30002 where f_fundcode=sf.scode and f_type=0 and f_tradingdate in
(select max(f_tradingdate) from si_fund_30002 where f_fundcode=sf.scode and to_date(f_tradingdate,'yyyyMMdd')>=(to_date(20120801,'yyyyMMdd')-10) and f_tradingdate<=20120801) and rownum<2),'fm999999990.9999')) desc) gqyn,
decode((select f30010_30002 from si_fund_30002 where f_fundcode=sf.scode and f_type=0 and f_tradingdate in
(select max(f_tradingdate) from si_fund_30002 where f_fundcode=sf.scode and to_date(f_tradingdate,'yyyyMMdd')>=(to_date(20120801,'yyyyMMdd')-10) and f_tradingdate<=20120801) and rownum<2),
null,'-',to_char((select f30010_30002 from si_fund_30002 where f_fundcode=sf.scode and f_type=0 and f_tradingdate in
(select max(f_tradingdate) from si_fund_30002 where f_fundcode=sf.scode and to_date(f_tradingdate,'yyyyMMdd')>=(to_date(20120801,'yyyyMMdd')-10) and f_tradingdate<=20120801) and rownum<2),'fm999999990.9999')) gqlnjzzzl,
row_number() over(partition by ff.fundsc||'-'||ff.fundtzfs||'-'||ff.fundcjfl||'-'||ff.fundonefl||'-'||ff.fundtowfl
order by decode((select f30010_30002 from si_fund_30002 where f_fundcode=sf.scode and f_type=0 and f_tradingdate in
(select max(f_tradingdate) from si_fund_30002 where f_fundcode=sf.scode and to_date(f_tradingdate,'yyyyMMdd')>=(to_date(20120801,'yyyyMMdd')-10) and f_tradingdate<=20120801) and rownum<2),
null,'-',to_char((select f30010_30002 from si_fund_30002 where f_fundcode=sf.scode and f_type=0 and f_tradingdate in
(select max(f_tradingdate) from si_fund_30002 where f_fundcode=sf.scode and to_date(f_tradingdate,'yyyyMMdd')>=(to_date(20120801,'yyyyMMdd')-10) and f_tradingdate<=20120801) and rownum<2),'fm999999990.9999')) desc) gqln,
decode((select f30011_30002 from si_fund_30002 where f_fundcode=sf.scode and f_type=0 and f_tradingdate in
(select max(f_tradingdate) from si_fund_30002 where f_fundcode=sf.scode and to_date(f_tradingdate,'yyyyMMdd')>=(to_date(20120801,'yyyyMMdd')-10) and f_tradingdate<=20120801) and rownum<2),
null,'-',to_char((select f30011_30002 from si_fund_30002 where f_fundcode=sf.scode and f_type=0 and f_tradingdate in
(select max(f_tradingdate) from si_fund_30002 where f_fundcode=sf.scode and to_date(f_tradingdate,'yyyyMMdd')>=(to_date(20120801,'yyyyMMdd')-10) and f_tradingdate<=20120801) and rownum<2),'fm999999990.9999')) gqsnjzzzl

from
(select
b.scode,b.fund5,b.fund4,b.fund10
from
sdc_fundbase b left join sdc_security a on b.scode=a.scode and b.mktcode=a.mktcode and b.fund49=a.stype
where
b.scode in (select t.fundcode from fund_fundcategoryone t where (t.fundtzfs='开放式' or t.fundtzfs='封闭式') and t.fundonefl!='货币市场基金' and t.fundonefl!='交易类' )
and (a.enddate>=to_date(20120801,'yyyy-MM-dd') or a.enddate is null or a.enddate=to_date(19000101,'yyyy-MM-dd'))
and a.stype in (6,7) order by scode
) sf
left join fund_fundcategoryone ff on sf.scode = ff.fundcode and ff.fundonefl!='交易类'

非常复杂,不仔细介绍,此时需要将20120801处全部变成参数传入,,只写参数就得累死我,下面方法可以节省我的好多工作量,在最内部函数中修改select
b.scode,b.fund5,b.fund4,b.fund10,20120801 tdate
from
将20120801用tdate变量来代替,这样传入的参数就用tdate来表示就可以了。

时间: 2024-07-29 17:00:36

sql中参数过多利用变量替换参数的方法_MsSql的相关文章

sql中参数过多利用变量替换参数的方法

工作中遇到一个需求,需要非常多的参数,例如如下sql, 复制代码 代码如下: select ff.fundsc||'-'||ff.fundtzfs||'-'||ff.fundcjfl||'-'||ff.fundonefl||'-'||ff.fundtowfl catagory, sf.scode,replace(sf.fund5,'型证券投资基金','')fund5,sf.fund4,sf.fund10, (select to_date(tradedate,'yyyy-MM-dd') from

sql中varchar和nvarchar的区别与使用方法_MsSql

问: sql server中的varchar和Nvarchar有什么区别啊,varchar好像是一个英文和一个汉字都站两个字节,而Nvarchar则是一个英文占一个字节,汉字占两个字节.可是这个对asp程序有什么影响? 答: varchar(n) 长度为 n 个字节的可变长度且非 Unicode 的字符数据.n 必须是一个介于 1 和 8,000 之间的数值.存储大小为输入数据的字节的实际长度,而不是 n 个字节. nvarchar(n) 包含 n 个字符的可变长度 Unicode 字符数据.n

SQL Server中利用正则表达式替换字符串的方法

建立正则替换函数,利用了OLE对象,以下是函数代码: --如果存在则删除原有函数 IF OBJECT_ID(N'dbo.RegexReplace') IS NOT NULL DROP FUNCTION dbo.RegexReplace GO --开始创建正则替换函数 CREATE FUNCTION dbo.RegexReplace ( @string VARCHAR(MAX), --被替换的字符串 @pattern VARCHAR(255), --替换模板 @replacestr VARCHAR

MSSQL批量替换语句 在SQL SERVER中批量替换字符串的方法_MsSql

方法一:(这种是最常用的,因为很多大段的内容都使用text ntext等数据类型,而我们通常也是替换里面的内容) varchar和nvarchar类型是支持replace,所以如果你的text不超过8000可以先转换成前面两种类型再使用replace 替换 text ntext 数据类型字段的语句 复制代码 代码如下: update 表名 set 字段名=replace(cast(与前面一样的字段名 as varchar(8000)) ,'原本内容','想要替换成什么') 方法二:(替换其他数据

按图索骥:SQL中数据倾斜问题的处理思路与方法

本文通过示例分享部分场景的处理方法 未使用绑定变量 使用绑定变量 几种特殊场景 1 测试环境说明 数据库版本:ORACLE 11.2.0.4 新建测试表tb_test: create tablescott.tb_test as select * from dba_objects; 创建索引: create indexscott.idx_tb_test_01 on scott.tb_test(object_id); 更新数据,使用数据分布不均匀: update scott.tb_testset o

SQL 双亲节点查找所有子节点的实现方法_MsSql

怎么保存树状结构的数据呢?在 SQL 中常用的是双亲节点法.创建表如下 CREATE TABLE category ( id LONG, parentId LONG, name String(20) ) INSERT INTO category VALUES ( 1, NULL, 'Root' ) INSERT INTO category VALUES ( 2, 1, 'Branch1' ) INSERT INTO category VALUES ( 3, 1, 'Branch2' ) INSE

SQL server服务显示远程过程调用失败的解决方法_MsSql

 刚刚打开SQL Server 2008,想要新建一个数据库,却发现出现了一个问题,这个问题由于之前没有遇到过,所以下面整理解决SQL server服务远程调用失败的几个方法,供大家参考,具体内容如下 先看看出现的问题: 出现上面这个错误的原因可能是由于咱们在装VS2012或者其他版本的时候,这个VS会自动装"Microsoft SQL Server 2013(2012) ExpressLocalDB"服务,所以导致SQL server2008,中SQL server服务显示远程过程调

sql server中判断表或临时表是否存在的方法_MsSql

1.判断数据表是否存在 方法一: use yourdb; go if object_id(N'tablename',N'U') is not null print '存在' else print '不存在' 例如: use fireweb; go if object_id(N'TEMP_TBL',N'U') is not null print '存在' else print '不存在' 方法二: USE [实例名] GO IF EXISTS (SELECT * FROM dbo.SysObjec

在Oracle PL/SQL中游标声明中表名动态变化的方法_Oracle应用

/*     小弟刚刚接触ORACLE存储过程,有一个问题向各位同行求教,小弟写了一个存储过程,其目的是接收一个参数作为表名,然后查询该表中的全部记录的某一个字段的内容导入到另一个表中.     (     tabname in varchar     )     is     v_servicesname tabname.服务类型%type; --这个变量就是用来存放所要取得的字段内容,但不知该如何定义     cursor curSort1 is select 服务类型 from tabna