行转列之后的datagrid的保存
行专列之后,查询的存储过程为
1 USE [APS_Future_FT]
2 GO
3 /****** Object: StoredProcedure [dbo].[P_APS_H_InternalStandardCrudePrice] Script Date: 2013/11/7 21:27:23 ******/
4 SET ANSI_NULLS ON
5 GO
6 SET QUOTED_IDENTIFIER ON
7 GO
8 -- =============================================
9 -- Author: <Author,,Name>
10 -- Create date: <Create Date,,>
11 -- Description: <Description,,>
12 -- =============================================
13 ALTER PROCEDURE [dbo].[P_APS_H_InternalStandardCrudePrice]
14 (
15 @PageSize INT ,
16 @PageIndex INT ,
17 @BeginDate DATETIME ,
18 @EndDate DATETIME ,
19 @TotalCount INT OUTPUT
20 )
21 AS
22 BEGIN
23
24 SET NOCOUNT ON;
25 ---------------------------------------------------------
26 SET @TotalCount=0
27
28 SELECT thi.CrudeStandardPriceID ,
29 vab.Name ,
30 vab.Code ,
31 thi.CrudeStandardPrice ,
32 thi.CreateDate ,
33 CONVERT(VARCHAR(10), PriceDate, 120) AS PriceDate
34 INTO #A
35 FROM T_H_InternalStandardCrudePrice thi ,
36 dbo.V_APS_BaseDataUnion vab
37 WHERE thi.CrudeStandard = vab.Code
38 AND thi.PriceDate BETWEEN @BeginDate AND @EndDate
39 ORDER BY thi.PriceDate ASC
40 ----------------------------------------------------------
41 DECLARE @i INT
42 SET @i = 0
43
44 SELECT @i = ( SELECT COUNT(*)
45 FROM #A
46 )
47 IF ( @i > 0 )
48 BEGIN
49 --读取动态列
50 DECLARE @STR NVARCHAR(MAX);
51 SELECT @STR = ( SELECT DISTINCT
52 '[' + Code + '],'
53 FROM #A
54 FOR
55 XML PATH('')
56 )
57 SELECT @STR = SUBSTRING(@STR, 1, LEN(@STR) - 1)
58
59 DECLARE @ESTR NVARCHAR(MAX)
60
61 -------------------------------------------------------------
62 --行转列
63 SET @ESTR = 'select rn=row_number()over (order by PriceDate desc),PriceDate,'
64 + @STR + '
65 FROM ( SELECT
66 Code ,
67 CrudeStandardPrice ,
68 PriceDate
69 FROM #A
70 ) AS A PIVOT ( SUM(A.CrudeStandardPrice) FOR A.Code IN ('
71 + @STR + ' ) ) AS B '
72
73 -- SET @ESTR = 'select *
74 -- FROM ( SELECT
75 -- Code ,
76 -- CrudeStandardPrice ,
77 --PriceDate
78 -- FROM #A
79 -- ) AS A PIVOT ( SUM(A.CrudeStandardPrice) FOR A.Code IN ('
80 -- + @STR + ' ) ) AS B'
81 ---------------------------------------------------------------
82 --查询总记录数
83
84 DECLARE @sql NVARCHAR(MAX)
85 SET @sql = 'select * into ##C from (' + @ESTR + ') as C'
86
87 EXEC(@sql)
88
89 SELECT @TotalCount = ( SELECT COUNT(*)
90 FROM ##C
91 )
92 DROP TABLE ##C
93 ----------------------------------------------------------------
94 --增加分页
95 SET @ESTR = 'select * from (' + @ESTR
96 + ') as C where rn BETWEEN '
97 + CAST(( ( @PageIndex ) * @PageSize + 1 ) AS NVARCHAR(20))
98 + ' and '
99 + CAST(( ( @PageIndex + 1 ) * @PageSize ) AS NVARCHAR(20))
100 + ' '
101
102 EXEC (@ESTR)
103 -------------------------------------------------------------
104 END
105 DROP TABLE #A
106
107 END
xml文件为
<data><row id="14" code="93_06" value="100" state="added" rowid="1" /><row id="14" code="name" value="New Row" state="added" rowid="2" /><row id="14" code="_uid" value="14" state="added" rowid="3" /><row id="14" code="93_11" value="100" state="added" rowid="4" /><row id="14" code="93_10" value="100" state="added" rowid="5" /><row id="14" code="93_09" value="100" state="added" rowid="6" /><row id="14" code="93_08" value="100" state="added" rowid="7" /><row id="14" code="93_12" value="100" state="added" rowid="8" /><row id="14" code="93_03" value="100" state="added" rowid="9" /><row id="14" code="93_02" value="210" state="added" rowid="10" /><row id="14" code="93_01" value="120" state="added" rowid="11" /><row id="14" code="93_07" value="100" state="added" rowid="12" /><row id="14" code="PriceDate" value="2013/11/1 0:00:00" state="added" rowid="13" /><row id="14" code="_id" value="14" state="added" rowid="14" /><row id="14" code="93_04" value="100" state="added" rowid="15" /><row id="14" code="93_05" value="100" state="added" rowid="16" /><row id="14" code="_state" value="added" state="added" rowid="17" /><row id="11" code="rn" value="1" state="modified" rowid="1" /><row id="11" code="_uid" value="11" state="modified" rowid="2" /><row id="11" code="93_12" value="200" state="modified" rowid="3" /><row id="11" code="93_11" value="200" state="modified" rowid="4" /><row id="11" code="93_10" value="200" state="modified" rowid="5" /><row id="11" code="93_06" value="200" state="modified" rowid="6" /><row id="11" code="93_09" value="200" state="modified" rowid="7" /><row id="11" code="93_08" value="200" state="modified" rowid="8" /><row id="11" code="93_03" value="200" state="modified" rowid="9" /><row id="11" code="93_02" value="200" state="modified" rowid="10" /><row id="11" code="93_01" value="200" state="modified" rowid="11" /><row id="11" code="93_07" value="200" state="modified" rowid="12" /><row id="11" code="PriceDate" value="2013-10-28" state="modified" rowid="13" /><row id="11" code="93_05" value="200" state="modified" rowid="14" /><row id="11" code="93_04" value="200" state="modified" rowid="15" /><row id="11" code="_id" value="11" state="modified" rowid="16" /><row id="11" code="_state" value="modified" state="modified" rowid="17" /></data>
存储过程为
1 USE [APS_Future_FT]
2 GO
3 /****** Object: StoredProcedure [dbo].[A_001] Script Date: 2013/11/7 19:23:03 ******/
4 SET ANSI_NULLS ON
5 GO
6 SET QUOTED_IDENTIFIER ON
7 GO
8 -- =============================================
9 -- Author: <Author,,Name>
10 -- Create date: <Create Date,,>
11 -- Description: <Description,,>
12 -- =============================================
13 ALTER PROCEDURE [dbo].[A_001] ( @str XML )
14 AS
15 BEGIN
16 -- SET NOCOUNT ON added to prevent extra result sets from
17 -- interfering with SELECT statements.
18 SET NOCOUNT ON;
19
20 DECLARE @T TABLE
21 (
22 T_id NVARCHAR(50) ,
23 T_code NVARCHAR(50) ,
24 T_value NVARCHAR(50) ,
25 T_state NVARCHAR(50)
26 )
27
28 INSERT INTO @T
29 ( T_id ,
30 T_code ,
31 T_value ,
32 T_state
33 )
34 SELECT T.r.value('@id', 'nvarchar(50)') ,
35 T.r.value('@code', 'nvarchar(50)'),
36 T.r.value('@value','nvarchar(50)'),
37 T.r.value('@state','nvarchar(50)')
38 FROM @str.nodes('/data/row') T ( r )
39
40
41
42 SELECT DISTINCT *
43 INTO #Date
44 FROM @T
45 WHERE T_code = 'PriceDate'
46
47 DELETE FROM @T
48 WHERE T_code LIKE '"_%' ESCAPE '"'
49 OR T_code = 'rn'
50 OR T_code = 'PriceDate'
51
52 SELECT T.*,
53 d.T_value T_date
54 INTO #ALL
55 FROM @T AS T
56 INNER JOIN #Date d ON T.T_id = d.T_id
57
58 --------------------------删除
59 DELETE iscp
60 FROM dbo.T_H_InternalStandardCrudePrice iscp
61 INNER JOIN #ALL a ON a.T_date = iscp.PriceDate
62 AND a.T_code = iscp.CrudeStandard
63 AND a.T_state = 'removed'
64
65
66 ---------------------------修改
67 SELECT iscp.CrudeStandardPriceID,
68 a.*
69 INTO #UPDATE
70 FROM #ALL a
71 INNER JOIN dbo.T_H_InternalStandardCrudePrice iscp
72 ON a.T_date = iscp.PriceDate
73 AND a.T_code = iscp.CrudeStandard
74
75 --SELECT * FROM #UPDATE
76
77 UPDATE T_H_InternalStandardCrudePrice
78 SET CrudeStandardPrice = up.T_value
79 FROM dbo.T_H_InternalStandardCrudePrice iscp
80 INNER JOIN #UPDATE up ON iscp.CrudeStandardPriceID = up.CrudeStandardPriceID
81
82 --------------------------------增加
83
84 INSERT INTO dbo.T_H_InternalStandardCrudePrice
85 ( CrudeStandard ,
86 PriceDate ,
87 CrudeStandardPrice
88 )
89 SELECT A.T_code,
90 CASE WHEN A.T_date IS NOT NULL
91 THEN CAST(A.T_date AS DATETIME)
92 ELSE NULL
93 END
94 ,
95 CASE WHEN A.T_value IS NOT NULL
96 THEN CAST(A.T_value AS DECIMAL)
97 ELSE NULL
98 END
99
100 FROM (
101 SELECT a.T_id,
102 a.T_code,
103 a.T_value,
104 a.T_state,
105 a.T_date
106 FROM #ALL a
107 EXCEPT
108 SELECT up.T_id,
109 up.T_code,
110 up.T_value,
111 up.T_state,
112 up.T_date
113 FROM #UPDATE up
114
115 )AS A
116
117 END
行转列之后的datagrid的保存的更多相关文章
- Easyui Datagrid 修改显示行号列宽度
EasyUI中Datagrid的第一列显示行号,可是如果数据量大的的时候,显示行号的那一列数据会显示不完全的. 可以通过修改Datagrid的样式来解决这个问题,在样式中加入下面这个样式,就可以自己修 ...
- DataGrid 滚动特定的行或者列
DataGrid 滚动特定的行或者列. DataGrid.ScrollIntoView Method (Object, DataGridColumn) .NET Framework 4.5 Silve ...
- oracle行转列与列转行
一.行转列 在有些应用场景中,如学生成绩.学生所有科目的成绩都保存到一张表里面,当我们需要以列表的形式显示出学生所对应的每一科目的成绩的时候,需要使用到行转列. 示例 -- 学生成绩表 create ...
- PIVOT就是行转列,UNPIVOT就是列传行
PIVOT通过将表达式某一列中的唯一值转换为输出中的多个列来旋转表值表达式,并在必要时对最终输出中所需的任何其余列值执行聚合.UNPIVOT与PIVOT执行相反的操作,将表值表达式的列转换为列值. 通 ...
- Java 操作Word表格——创建嵌套表格、添加/复制表格行或列、设置表格是否禁止跨页断行
本文将对如何在Java程序中操作Word表格作进一步介绍.操作要点包括 如何在Word中创建嵌套表格. 对已有表格添加行或者列 复制已有表格中的指定行或者列 对跨页的表格可设置是否禁止跨页断行 创建表 ...
- Oracle 行转列 动态出转换的列
本文链接:https://blog.csdn.net/Huay_Li/article/details/82924443 10月的第二天,前天写了个Oracle中行转列的pivot的基本使用方法,然后, ...
- SqlServer:SqlServer(sql,游标,定时作业,行转列,列转行,公用表达式递归,merge合并)
1.加载驱动: Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); DriverManager.getCo ...
- 基于OpenCV的循环行、列移动函数circShift()
///*12 在Matlab中有个circShift()函数,可以实现行.列的循环移动 /// 在返卷积运算中,会用到这个函数.所以,在Opencv中我也定义同样 /// 功能的函数 /// 该函数有 ...
- SQL Server 动态行转列(参数化表名、分组列、行转列字段、字段值)
一.本文所涉及的内容(Contents) 本文所涉及的内容(Contents) 背景(Contexts) 实现代码(SQL Codes) 方法一:使用拼接SQL,静态列字段: 方法二:使用拼接SQL, ...
随机推荐
- Codeforces Round #191 (Div. 2) A. Flipping Game(简单)
A. Flipping Game time limit per test 1 second memory limit per test 256 megabytes input standard inp ...
- c# 重载运算符(ovveride operator)踩坑记,关于null比对
场景描述: 需要比对两个版本的对应对象是否完全一致(每个属性值一致),不一致的导出报表颜色标识,以便提醒后续使用报表人员. 实现思路: 对象重载ToString方法,另实现一比对基类(为了通用)重载= ...
- [php]require&require_once&include&include_once的用法与区别
1.require和include是php引入php文件的两种方式,使用格式如下: require(include) 文件名; require(include) 变量(此变量存储的是文件名); 2.区 ...
- 如何阻止自动更新‘updated_at’和'created_at'
可以在模版中添加一条代码: public $timestamps = false;
- 【leetcode 简单】第三题 回文数
判断一个整数是否是回文数.回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数. 示例 1: 输入: 121 输出: true 示例 2: 输入: -121 输出: false 解释: 从左向 ...
- 爬虫--Scrapy之Downloader Middleware
下载器中间件(Downloader Middleware) 下载器中间件是介于Scrapy的request/response处理的钩子框架. 是用于全局修改Scrapy request和respons ...
- 海洋CMS v6.53 v6.54命令执行
测试下载地址:https://pan.baidu.com/s/1jHQBKFk 至于分析实在是看的一脸懵逼就不累赘了.直接上exp POST /haiyang/upload/search.php HT ...
- python操作adb代码
adb命令的练习 #!/usr/bin/env python # encoding: utf-8 import os import re nameplt=re.compile("packag ...
- Codeforces Round #453 (Div. 1)
Codeforces Round #453 (Div. 1) A. Hashing Trees 题目描述:给出一棵树的高度和每一层的节点数,问是否有两棵树都满足这个条件,若有,则输出这两棵树,否则输出 ...
- loadrunner 测试问题汇总
1.关于Error -27791: Error -27790:Error -27740: 错误如下: Action.c(198): Error -27791: Server ...