/*
Navicat SQL Server Data Transfer

Source Server         : 127.0.0.1
Source Server Version : 130000
Source Host           : 127.0.0.1:1433
Source Database       : EIP_PRD_HQWS
Source Schema         : dbo

Target Server Type    : SQL Server
Target Server Version : 130000
File Encoding         : 65001

Date: 2026-01-30 00:37:25
*/


-- ----------------------------
-- Table structure for A_UPLOAD
-- ----------------------------
DROP TABLE [dbo].[A_UPLOAD]
GO
CREATE TABLE [dbo].[A_UPLOAD] (
[mpointcode] varchar(50) NULL ,
[parmname] varchar(50) NULL ,
[scdtype] varchar(50) NULL ,
[upload] char(10) NULL 
)


GO

-- ----------------------------
-- Records of A_UPLOAD
-- ----------------------------

-- ----------------------------
-- Table structure for Alarm
-- ----------------------------
DROP TABLE [dbo].[Alarm]
GO
CREATE TABLE [dbo].[Alarm] (
[IOServerName] varchar(50) NOT NULL ,
[MachineName] text NULL ,
[TagName] varchar(50) NOT NULL ,
[TagComment] text NULL ,
[GroupName] text NULL ,
[AlarmValue] text NULL ,
[AlarmValueDataType] int NULL ,
[LimitValue] text NULL ,
[LimitValueDataType] int NULL ,
[AlarmType] text NULL ,
[Pri] int NULL ,
[Quality] int NULL ,
[AlarmTime] datetime NULL ,
[AlarmTimeMs] int NULL ,
[EventTime] datetime NOT NULL ,
[EventTimeMs] int NULL ,
[OperatorName] text NULL ,
[OperatorDomain] text NULL ,
[ResumeValue] text NULL ,
[ResumeValueDataType] int NULL ,
[EventType] varchar(50) NOT NULL ,
[AlarmText] text NULL ,
[ExtendField1] text NULL ,
[ExtendField2] text NULL ,
[ExtendField3] text NULL ,
[ExtendField4] text NULL ,
[ExtendField5] text NULL ,
[ExtendField6] text NULL ,
[ExtendField7] text NULL ,
[ExtendField8] text NULL ,
[OperationRemark] text NULL ,
[MachineIP] text NULL 
)


GO

-- ----------------------------
-- Records of Alarm
-- ----------------------------

-- ----------------------------
-- Table structure for dtproperties
-- ----------------------------
DROP TABLE [dbo].[dtproperties]
GO
CREATE TABLE [dbo].[dtproperties] (
[id] int NOT NULL IDENTITY(1,1) ,
[objectid] int NULL ,
[property] varchar(64) NOT NULL ,
[value] varchar(255) NULL ,
[uvalue] nvarchar(255) NULL ,
[lvalue] image NULL ,
[version] int NOT NULL DEFAULT ((0)) 
)


GO

-- ----------------------------
-- Records of dtproperties
-- ----------------------------
SET IDENTITY_INSERT [dbo].[dtproperties] ON
GO
SET IDENTITY_INSERT [dbo].[dtproperties] OFF
GO

-- ----------------------------
-- Table structure for Enter
-- ----------------------------
DROP TABLE [dbo].[Enter]
GO
CREATE TABLE [dbo].[Enter] (
[MachineName] varchar(50) NOT NULL ,
[OperatorName] text NULL ,
[OperatorDomain] text NULL ,
[SuccessEnter] bit NULL ,
[EventType] text NULL ,
[EventTime] datetime NOT NULL ,
[EventTimeMs] int NOT NULL ,
[MachineIP] text NULL 
)


GO

-- ----------------------------
-- Records of Enter
-- ----------------------------

-- ----------------------------
-- Table structure for MZ_DF11_C_01_1006_02_COD02
-- ----------------------------
DROP TABLE [dbo].[MZ_DF11_C_01_1006_02_COD02]
GO
CREATE TABLE [dbo].[MZ_DF11_C_01_1006_02_COD02] (
[ItemID] bigint NOT NULL IDENTITY(1,1) ,
[ParmValue] float(53) NOT NULL DEFAULT ((0)) ,
[MeasureDT] datetime NOT NULL DEFAULT (getdate()) ,
[userid] varchar(50) NULL ,
[insdt] varchar(50) NULL DEFAULT (getdate()) ,
[memotype] varchar(100) NULL ,
[memo] varchar(500) NULL 
)


GO
DBCC CHECKIDENT(N'[dbo].[MZ_DF11_C_01_1006_02_COD02]', RESEED, 7489)
GO

-- ----------------------------
-- Records of MZ_DF11_C_01_1006_02_COD02
-- ----------------------------
SET IDENTITY_INSERT [dbo].[MZ_DF11_C_01_1006_02_COD02] ON
GO
SET IDENTITY_INSERT [dbo].[MZ_DF11_C_01_1006_02_COD02] OFF
GO

-- ----------------------------
-- Table structure for Operate
-- ----------------------------
DROP TABLE [dbo].[Operate]
GO
CREATE TABLE [dbo].[Operate] (
[IOServerName] varchar(50) NOT NULL ,
[MachineName] text NULL ,
[TagName] varchar(50) NOT NULL ,
[TagComment] text NULL ,
[OldValue] text NULL ,
[OldValueDataType] int NULL ,
[NewValue] text NULL ,
[NewValueDataType] int NULL ,
[EventTime] datetime NOT NULL ,
[EventTimeMs] int NULL ,
[SuccessOa] bit NULL ,
[EventType] varchar(50) NOT NULL ,
[FieldName] text NULL ,
[OperatorName] text NULL ,
[OperatorDomain] text NULL ,
[OperateText] text NULL ,
[MachineIP] text NULL ,
[ExtendField1] text NULL ,
[ExtendField2] text NULL ,
[ExtendField3] text NULL ,
[ExtendField4] text NULL ,
[ExtendField5] text NULL ,
[ExtendField6] text NULL ,
[ExtendField7] text NULL ,
[ExtendField8] text NULL 
)


GO

-- ----------------------------
-- Records of Operate
-- ----------------------------

-- ----------------------------
-- Table structure for Station
-- ----------------------------
DROP TABLE [dbo].[Station]
GO
CREATE TABLE [dbo].[Station] (
[MachineName] varchar(50) NOT NULL ,
[EventType] text NULL ,
[EventTime] datetime NOT NULL ,
[EventTimeMs] int NOT NULL ,
[MachineIP] text NULL 
)


GO

-- ----------------------------
-- Records of Station
-- ----------------------------

-- ----------------------------
-- Table structure for TB_Alarm_col
-- ----------------------------
DROP TABLE [dbo].[TB_Alarm_col]
GO
CREATE TABLE [dbo].[TB_Alarm_col] (
[id] int NOT NULL IDENTITY(1,1) ,
[colval] varchar(50) NULL ,
[colname] varchar(50) NULL ,
[st] varchar(50) NULL ,
[morder] int NULL 
)


GO
DBCC CHECKIDENT(N'[dbo].[TB_Alarm_col]', RESEED, 19)
GO

-- ----------------------------
-- Records of TB_Alarm_col
-- ----------------------------
SET IDENTITY_INSERT [dbo].[TB_Alarm_col] ON
GO
SET IDENTITY_INSERT [dbo].[TB_Alarm_col] OFF
GO

-- ----------------------------
-- Table structure for TB_DataRemote_LOG
-- ----------------------------
DROP TABLE [dbo].[TB_DataRemote_LOG]
GO
CREATE TABLE [dbo].[TB_DataRemote_LOG] (
[id] bigint NOT NULL IDENTITY(1,1) ,
[dt] datetime NULL ,
[content] varchar(800) NULL ,
[insdt] datetime NULL DEFAULT (getdate()) 
)


GO
DBCC CHECKIDENT(N'[dbo].[TB_DataRemote_LOG]', RESEED, 18805)
GO

-- ----------------------------
-- Records of TB_DataRemote_LOG
-- ----------------------------
SET IDENTITY_INSERT [dbo].[TB_DataRemote_LOG] ON
GO
SET IDENTITY_INSERT [dbo].[TB_DataRemote_LOG] OFF
GO

-- ----------------------------
-- Table structure for TB_DATATRANS_BADMP
-- ----------------------------
DROP TABLE [dbo].[TB_DATATRANS_BADMP]
GO
CREATE TABLE [dbo].[TB_DATATRANS_BADMP] (
[ID] int NOT NULL IDENTITY(1,1) ,
[id_main] varchar(50) NULL ,
[BadMPcode] varchar(50) NULL ,
[BadMPname] varchar(50) NULL ,
[IP] varchar(50) NULL ,
[OPCServer] varchar(50) NULL ,
[DBName] varchar(50) NULL ,
[BizType] varchar(50) NULL ,
[insdt] datetime NULL DEFAULT (getdate()) 
)


GO
DBCC CHECKIDENT(N'[dbo].[TB_DATATRANS_BADMP]', RESEED, 879)
GO

-- ----------------------------
-- Records of TB_DATATRANS_BADMP
-- ----------------------------
SET IDENTITY_INSERT [dbo].[TB_DATATRANS_BADMP] ON
GO
SET IDENTITY_INSERT [dbo].[TB_DATATRANS_BADMP] OFF
GO

-- ----------------------------
-- Table structure for TB_DATATRANS_LOG
-- ----------------------------
DROP TABLE [dbo].[TB_DATATRANS_LOG]
GO
CREATE TABLE [dbo].[TB_DATATRANS_LOG] (
[id] bigint NOT NULL IDENTITY(1,1) ,
[dt] datetime NULL ,
[content] varchar(800) NULL ,
[insdt] datetime NULL DEFAULT (getdate()) 
)


GO
DBCC CHECKIDENT(N'[dbo].[TB_DATATRANS_LOG]', RESEED, 28603)
GO

-- ----------------------------
-- Records of TB_DATATRANS_LOG
-- ----------------------------
SET IDENTITY_INSERT [dbo].[TB_DATATRANS_LOG] ON
GO
SET IDENTITY_INSERT [dbo].[TB_DATATRANS_LOG] OFF
GO

-- ----------------------------
-- Table structure for TB_DATATRANS_Record
-- ----------------------------
DROP TABLE [dbo].[TB_DATATRANS_Record]
GO
CREATE TABLE [dbo].[TB_DATATRANS_Record] (
[ID] int NOT NULL IDENTITY(1,1) ,
[id_main] varchar(50) NULL ,
[IP] varchar(50) NULL ,
[StartTime] datetime NULL ,
[OPCServer] varchar(50) NULL ,
[DBName] varchar(50) NULL ,
[BizType] varchar(50) NULL ,
[MPCount] varchar(50) NULL ,
[MPGetCount] varchar(50) NULL ,
[MPTransCount] varchar(50) NULL ,
[SuccessRate] varchar(50) NULL ,
[insdt] datetime NULL DEFAULT (getdate()) 
)


GO
DBCC CHECKIDENT(N'[dbo].[TB_DATATRANS_Record]', RESEED, 3712)
GO

-- ----------------------------
-- Records of TB_DATATRANS_Record
-- ----------------------------
SET IDENTITY_INSERT [dbo].[TB_DATATRANS_Record] ON
GO
SET IDENTITY_INSERT [dbo].[TB_DATATRANS_Record] OFF
GO

-- ----------------------------
-- Table structure for TB_DataUpError
-- ----------------------------
DROP TABLE [dbo].[TB_DataUpError]
GO
CREATE TABLE [dbo].[TB_DataUpError] (
[id] bigint NOT NULL IDENTITY(1,1) ,
[dbname] varchar(50) NULL ,
[updt] datetime NULL ,
[insdt] datetime NULL DEFAULT (getdate()) 
)


GO
DBCC CHECKIDENT(N'[dbo].[TB_DataUpError]', RESEED, 99)
GO

-- ----------------------------
-- Records of TB_DataUpError
-- ----------------------------
SET IDENTITY_INSERT [dbo].[TB_DataUpError] ON
GO
SET IDENTITY_INSERT [dbo].[TB_DataUpError] OFF
GO

-- ----------------------------
-- Table structure for TB_FANGAN
-- ----------------------------
DROP TABLE [dbo].[TB_FANGAN]
GO
CREATE TABLE [dbo].[TB_FANGAN] (
[id] varchar(50) NOT NULL ,
[name] varchar(50) NULL ,
[hismin] char(1) NULL DEFAULT ('1') ,
[hishour] char(1) NULL DEFAULT ('1') ,
[hisday] char(1) NULL DEFAULT ('1') ,
[active] varchar(50) NULL ,
[morder] int NULL ,
[owneruserid] varchar(50) NULL ,
[ownerdate] datetime NULL ,
[SYSTEMCHART] varchar(50) NULL 
)


GO

-- ----------------------------
-- Records of TB_FANGAN
-- ----------------------------

-- ----------------------------
-- Table structure for TB_FANGANDETAIL
-- ----------------------------
DROP TABLE [dbo].[TB_FANGANDETAIL]
GO
CREATE TABLE [dbo].[TB_FANGANDETAIL] (
[id] varchar(50) NULL ,
[faid] varchar(50) NULL ,
[mps] varchar(500) NULL ,
[func] varchar(50) NULL ,
[axesorder] varchar(50) NULL ,
[disname] varchar(50) NULL ,
[avgmin] money NULL ,
[avgmax] money NULL ,
[forcemin] money NULL ,
[forcemax] money NULL ,
[color] varchar(50) NULL ,
[morder] money NULL ,
[fmt] varchar(50) NULL ,
[axismax] money NULL ,
[axismin] money NULL ,
[spanrange] money NULL ,
[numtail] varchar(50) NULL 
)


GO

-- ----------------------------
-- Records of TB_FANGANDETAIL
-- ----------------------------

-- ----------------------------
-- Table structure for TB_FORMULA
-- ----------------------------
DROP TABLE [dbo].[TB_FORMULA]
GO
CREATE TABLE [dbo].[TB_FORMULA] (
[id] varchar(50) NOT NULL ,
[name] varchar(50) NULL ,
[memo] varchar(500) NULL ,
[exp] varchar(500) NULL ,
[rate] money NULL ,
[intv] varchar(50) NULL ,
[starthour] int NULL ,
[active] varchar(50) NULL ,
[morder] varchar(50) NULL ,
[owneruserid] varchar(50) NULL ,
[ownerdate] datetime NULL 
)


GO

-- ----------------------------
-- Records of TB_FORMULA
-- ----------------------------

-- ----------------------------
-- Table structure for TB_FORMULADETAIL
-- ----------------------------
DROP TABLE [dbo].[TB_FORMULADETAIL]
GO
CREATE TABLE [dbo].[TB_FORMULADETAIL] (
[id] varchar(50) NOT NULL ,
[pid] varchar(50) NULL ,
[name] varchar(50) NULL ,
[memo] varchar(500) NULL ,
[mp] varchar(50) NULL ,
[type] varchar(50) NULL 
)


GO

-- ----------------------------
-- Records of TB_FORMULADETAIL
-- ----------------------------

-- ----------------------------
-- Table structure for TB_FORMULAPARM
-- ----------------------------
DROP TABLE [dbo].[TB_FORMULAPARM]
GO
CREATE TABLE [dbo].[TB_FORMULAPARM] (
[ItemID] bigint NOT NULL IDENTITY(1,1) ,
[MpointName] varchar(50) NULL ,
[ParmName] varchar(50) NULL ,
[ParmValue] money NULL ,
[MeasureDT] datetime NULL 
)


GO
DBCC CHECKIDENT(N'[dbo].[TB_FORMULAPARM]', RESEED, 471)
GO

-- ----------------------------
-- Records of TB_FORMULAPARM
-- ----------------------------
SET IDENTITY_INSERT [dbo].[TB_FORMULAPARM] ON
GO
SET IDENTITY_INSERT [dbo].[TB_FORMULAPARM] OFF
GO

-- ----------------------------
-- Table structure for TB_INMPVALUE
-- ----------------------------
DROP TABLE [dbo].[TB_INMPVALUE]
GO
CREATE TABLE [dbo].[TB_INMPVALUE] (
[MeasureDT] datetime NULL ,
[AT1103_JSCOD] float(53) NULL ,
[AT2103_CSCOD] float(53) NULL ,
[AT1104_JSNH3N] float(53) NULL ,
[AT2104_CSNH3N] float(53) NULL ,
[AT1102_JSSS] float(53) NULL ,
[AT2102_CSSS] float(53) NULL ,
[FT1101_JSFLOW] float(53) NULL ,
[LT1102] float(53) NULL ,
[LT1103] float(53) NULL ,
[jsb3_I2] float(53) NULL ,
[jsb4_I2] float(53) NULL ,
[jsb5_I2] float(53) NULL ,
[GFJ1_DL] float(53) NULL ,
[GFJ2_DL] float(53) NULL ,
[GFJ3_DL] float(53) NULL ,
[AT105_DO] float(53) NULL ,
[AT107_DO] float(53) NULL ,
[AT106_DO] float(53) NULL ,
[AT108_DO] float(53) NULL ,
[AT109_MLSS] float(53) NULL ,
[AT110_MLSS] float(53) NULL ,
[AT101_ORP] float(53) NULL ,
[AT102_ORP] float(53) NULL ,
[AT103_ORP] float(53) NULL ,
[AT104_ORP] float(53) NULL 
)


GO

-- ----------------------------
-- Records of TB_INMPVALUE
-- ----------------------------

-- ----------------------------
-- Table structure for TB_MeasurePoint
-- ----------------------------
DROP TABLE [dbo].[TB_MeasurePoint]
GO
CREATE TABLE [dbo].[TB_MeasurePoint] (
[ID] varchar(50) NOT NULL ,
[MPointID] varchar(50) NOT NULL ,
[MPointCode] varchar(50) NOT NULL ,
[ParmName] varchar(50) NULL ,
[Unit] nvarchar(50) NULL ,
[alarmmax] money NULL ,
[alarmmin] money NULL ,
[ParmValue] money NULL ,
[MeasureDT] datetime NULL ,
[Rate] decimal(18,10) NULL DEFAULT ((1)) ,
[Freq] int NULL DEFAULT ((1)) ,
[FreqUnit] varchar(10) NULL ,
[SignalType] varchar(50) NULL DEFAULT ('AI') ,
[SignalTag] varchar(50) NULL ,
[LEDType] char(1) NULL DEFAULT ('1') ,
[LEDColor] char(1) NULL DEFAULT ('1') ,
[DirectType] char(1) NULL DEFAULT ('1') ,
[BizId] varchar(50) NULL ,
[BizType] varchar(50) NULL DEFAULT ('??') ,
[NumTail] varchar(10) NULL DEFAULT ('0.00') ,
[prochour] varchar(50) NULL ,
[procday] varchar(50) NULL ,
[procmonth] varchar(50) NULL ,
[ShowName] char(1) NULL DEFAULT ('1') ,
[exp] varchar(50) NULL ,
[forcemin] money NULL ,
[forcemax] money NULL ,
[avgmax] money NULL ,
[avgmin] money NULL ,
[remoteup] char(1) NULL DEFAULT ('0') ,
[morder] int NULL DEFAULT ((999)) ,
[TriggerAlarm] varchar(50) NULL DEFAULT ('0') ,
[ConfirmAlarm] char(1) NULL DEFAULT ('0') ,
[flowset] money NULL DEFAULT ((0)) ,
[triggerCycle] char(1) NULL DEFAULT ('0') ,
[cyclemax] money NULL ,
[cyclemin] money NULL ,
[triggerMutation] char(1) NULL DEFAULT ('0') ,
[mutationset] money NULL ,
[causeset] money NULL ,
[operateset] money NULL ,
[resultset] money NULL ,
[triggerEquOff] char(1) NULL DEFAULT ('0') ,
[mathop] char(1) NULL DEFAULT ('*') ,
[valuetype] varchar(50) NULL ,
[valueMeaning] varchar(50) NULL ,
[active] varchar(50) NULL DEFAULT ('??') ,
[SoundAlarm] char(1) NULL DEFAULT ('0') ,
[scdtype] varchar(50) NULL ,
[spanrange] money NULL ,
[modbusfigid] varchar(50) NULL ,
[register] varchar(50) NULL ,
[processSectionCode] varchar(50) NULL ,
[equipmentId] varchar(50) NULL ,
[source_type] varchar(50) NULL ,
[patrol_type] varchar(10) NULL ,
[remark] varchar(500) NULL ,
[alarm_level] int NULL ,
[disname] varchar(50) NULL ,
[halarmmax] money NULL ,
[lalarmmin] money NULL 
)


GO
IF ((SELECT COUNT(*) from fn_listextendedproperty('MS_Description', 
'SCHEMA', N'dbo', 
'TABLE', N'TB_MeasurePoint', 
'COLUMN', N'MPointID')) > 0) 
EXEC sp_updateextendedproperty @name = N'MS_Description', @value = N'测量点ID'
, @level0type = 'SCHEMA', @level0name = N'dbo'
, @level1type = 'TABLE', @level1name = N'TB_MeasurePoint'
, @level2type = 'COLUMN', @level2name = N'MPointID'
ELSE
EXEC sp_addextendedproperty @name = N'MS_Description', @value = N'测量点ID'
, @level0type = 'SCHEMA', @level0name = N'dbo'
, @level1type = 'TABLE', @level1name = N'TB_MeasurePoint'
, @level2type = 'COLUMN', @level2name = N'MPointID'
GO
IF ((SELECT COUNT(*) from fn_listextendedproperty('MS_Description', 
'SCHEMA', N'dbo', 
'TABLE', N'TB_MeasurePoint', 
'COLUMN', N'MPointCode')) > 0) 
EXEC sp_updateextendedproperty @name = N'MS_Description', @value = N'测量点名'
, @level0type = 'SCHEMA', @level0name = N'dbo'
, @level1type = 'TABLE', @level1name = N'TB_MeasurePoint'
, @level2type = 'COLUMN', @level2name = N'MPointCode'
ELSE
EXEC sp_addextendedproperty @name = N'MS_Description', @value = N'测量点名'
, @level0type = 'SCHEMA', @level0name = N'dbo'
, @level1type = 'TABLE', @level1name = N'TB_MeasurePoint'
, @level2type = 'COLUMN', @level2name = N'MPointCode'
GO
IF ((SELECT COUNT(*) from fn_listextendedproperty('MS_Description', 
'SCHEMA', N'dbo', 
'TABLE', N'TB_MeasurePoint', 
'COLUMN', N'ParmName')) > 0) 
EXEC sp_updateextendedproperty @name = N'MS_Description', @value = N'测量点中文描述'
, @level0type = 'SCHEMA', @level0name = N'dbo'
, @level1type = 'TABLE', @level1name = N'TB_MeasurePoint'
, @level2type = 'COLUMN', @level2name = N'ParmName'
ELSE
EXEC sp_addextendedproperty @name = N'MS_Description', @value = N'测量点中文描述'
, @level0type = 'SCHEMA', @level0name = N'dbo'
, @level1type = 'TABLE', @level1name = N'TB_MeasurePoint'
, @level2type = 'COLUMN', @level2name = N'ParmName'
GO
IF ((SELECT COUNT(*) from fn_listextendedproperty('MS_Description', 
'SCHEMA', N'dbo', 
'TABLE', N'TB_MeasurePoint', 
'COLUMN', N'Unit')) > 0) 
EXEC sp_updateextendedproperty @name = N'MS_Description', @value = N'单位'
, @level0type = 'SCHEMA', @level0name = N'dbo'
, @level1type = 'TABLE', @level1name = N'TB_MeasurePoint'
, @level2type = 'COLUMN', @level2name = N'Unit'
ELSE
EXEC sp_addextendedproperty @name = N'MS_Description', @value = N'单位'
, @level0type = 'SCHEMA', @level0name = N'dbo'
, @level1type = 'TABLE', @level1name = N'TB_MeasurePoint'
, @level2type = 'COLUMN', @level2name = N'Unit'
GO
IF ((SELECT COUNT(*) from fn_listextendedproperty('MS_Description', 
'SCHEMA', N'dbo', 
'TABLE', N'TB_MeasurePoint', 
'COLUMN', N'alarmmax')) > 0) 
EXEC sp_updateextendedproperty @name = N'MS_Description', @value = N'报警上限'
, @level0type = 'SCHEMA', @level0name = N'dbo'
, @level1type = 'TABLE', @level1name = N'TB_MeasurePoint'
, @level2type = 'COLUMN', @level2name = N'alarmmax'
ELSE
EXEC sp_addextendedproperty @name = N'MS_Description', @value = N'报警上限'
, @level0type = 'SCHEMA', @level0name = N'dbo'
, @level1type = 'TABLE', @level1name = N'TB_MeasurePoint'
, @level2type = 'COLUMN', @level2name = N'alarmmax'
GO
IF ((SELECT COUNT(*) from fn_listextendedproperty('MS_Description', 
'SCHEMA', N'dbo', 
'TABLE', N'TB_MeasurePoint', 
'COLUMN', N'alarmmin')) > 0) 
EXEC sp_updateextendedproperty @name = N'MS_Description', @value = N'报警下限'
, @level0type = 'SCHEMA', @level0name = N'dbo'
, @level1type = 'TABLE', @level1name = N'TB_MeasurePoint'
, @level2type = 'COLUMN', @level2name = N'alarmmin'
ELSE
EXEC sp_addextendedproperty @name = N'MS_Description', @value = N'报警下限'
, @level0type = 'SCHEMA', @level0name = N'dbo'
, @level1type = 'TABLE', @level1name = N'TB_MeasurePoint'
, @level2type = 'COLUMN', @level2name = N'alarmmin'
GO
IF ((SELECT COUNT(*) from fn_listextendedproperty('MS_Description', 
'SCHEMA', N'dbo', 
'TABLE', N'TB_MeasurePoint', 
'COLUMN', N'ParmValue')) > 0) 
EXEC sp_updateextendedproperty @name = N'MS_Description', @value = N'采集值'
, @level0type = 'SCHEMA', @level0name = N'dbo'
, @level1type = 'TABLE', @level1name = N'TB_MeasurePoint'
, @level2type = 'COLUMN', @level2name = N'ParmValue'
ELSE
EXEC sp_addextendedproperty @name = N'MS_Description', @value = N'采集值'
, @level0type = 'SCHEMA', @level0name = N'dbo'
, @level1type = 'TABLE', @level1name = N'TB_MeasurePoint'
, @level2type = 'COLUMN', @level2name = N'ParmValue'
GO
IF ((SELECT COUNT(*) from fn_listextendedproperty('MS_Description', 
'SCHEMA', N'dbo', 
'TABLE', N'TB_MeasurePoint', 
'COLUMN', N'MeasureDT')) > 0) 
EXEC sp_updateextendedproperty @name = N'MS_Description', @value = N'采集时间'
, @level0type = 'SCHEMA', @level0name = N'dbo'
, @level1type = 'TABLE', @level1name = N'TB_MeasurePoint'
, @level2type = 'COLUMN', @level2name = N'MeasureDT'
ELSE
EXEC sp_addextendedproperty @name = N'MS_Description', @value = N'采集时间'
, @level0type = 'SCHEMA', @level0name = N'dbo'
, @level1type = 'TABLE', @level1name = N'TB_MeasurePoint'
, @level2type = 'COLUMN', @level2name = N'MeasureDT'
GO
IF ((SELECT COUNT(*) from fn_listextendedproperty('MS_Description', 
'SCHEMA', N'dbo', 
'TABLE', N'TB_MeasurePoint', 
'COLUMN', N'Rate')) > 0) 
EXEC sp_updateextendedproperty @name = N'MS_Description', @value = N'系数'
, @level0type = 'SCHEMA', @level0name = N'dbo'
, @level1type = 'TABLE', @level1name = N'TB_MeasurePoint'
, @level2type = 'COLUMN', @level2name = N'Rate'
ELSE
EXEC sp_addextendedproperty @name = N'MS_Description', @value = N'系数'
, @level0type = 'SCHEMA', @level0name = N'dbo'
, @level1type = 'TABLE', @level1name = N'TB_MeasurePoint'
, @level2type = 'COLUMN', @level2name = N'Rate'
GO
IF ((SELECT COUNT(*) from fn_listextendedproperty('MS_Description', 
'SCHEMA', N'dbo', 
'TABLE', N'TB_MeasurePoint', 
'COLUMN', N'Freq')) > 0) 
EXEC sp_updateextendedproperty @name = N'MS_Description', @value = N'刷新间隔'
, @level0type = 'SCHEMA', @level0name = N'dbo'
, @level1type = 'TABLE', @level1name = N'TB_MeasurePoint'
, @level2type = 'COLUMN', @level2name = N'Freq'
ELSE
EXEC sp_addextendedproperty @name = N'MS_Description', @value = N'刷新间隔'
, @level0type = 'SCHEMA', @level0name = N'dbo'
, @level1type = 'TABLE', @level1name = N'TB_MeasurePoint'
, @level2type = 'COLUMN', @level2name = N'Freq'
GO
IF ((SELECT COUNT(*) from fn_listextendedproperty('MS_Description', 
'SCHEMA', N'dbo', 
'TABLE', N'TB_MeasurePoint', 
'COLUMN', N'SignalType')) > 0) 
EXEC sp_updateextendedproperty @name = N'MS_Description', @value = N'测量点分类(AI模拟DI数字)'
, @level0type = 'SCHEMA', @level0name = N'dbo'
, @level1type = 'TABLE', @level1name = N'TB_MeasurePoint'
, @level2type = 'COLUMN', @level2name = N'SignalType'
ELSE
EXEC sp_addextendedproperty @name = N'MS_Description', @value = N'测量点分类(AI模拟DI数字)'
, @level0type = 'SCHEMA', @level0name = N'dbo'
, @level1type = 'TABLE', @level1name = N'TB_MeasurePoint'
, @level2type = 'COLUMN', @level2name = N'SignalType'
GO
IF ((SELECT COUNT(*) from fn_listextendedproperty('MS_Description', 
'SCHEMA', N'dbo', 
'TABLE', N'TB_MeasurePoint', 
'COLUMN', N'LEDType')) > 0) 
EXEC sp_updateextendedproperty @name = N'MS_Description', @value = N'开关量分类(1运行0故障)'
, @level0type = 'SCHEMA', @level0name = N'dbo'
, @level1type = 'TABLE', @level1name = N'TB_MeasurePoint'
, @level2type = 'COLUMN', @level2name = N'LEDType'
ELSE
EXEC sp_addextendedproperty @name = N'MS_Description', @value = N'开关量分类(1运行0故障)'
, @level0type = 'SCHEMA', @level0name = N'dbo'
, @level1type = 'TABLE', @level1name = N'TB_MeasurePoint'
, @level2type = 'COLUMN', @level2name = N'LEDType'
GO
IF ((SELECT COUNT(*) from fn_listextendedproperty('MS_Description', 
'SCHEMA', N'dbo', 
'TABLE', N'TB_MeasurePoint', 
'COLUMN', N'LEDColor')) > 0) 
EXEC sp_updateextendedproperty @name = N'MS_Description', @value = N'颜色正反(1红开绿停0绿开红停)'
, @level0type = 'SCHEMA', @level0name = N'dbo'
, @level1type = 'TABLE', @level1name = N'TB_MeasurePoint'
, @level2type = 'COLUMN', @level2name = N'LEDColor'
ELSE
EXEC sp_addextendedproperty @name = N'MS_Description', @value = N'颜色正反(1红开绿停0绿开红停)'
, @level0type = 'SCHEMA', @level0name = N'dbo'
, @level1type = 'TABLE', @level1name = N'TB_MeasurePoint'
, @level2type = 'COLUMN', @level2name = N'LEDColor'
GO
IF ((SELECT COUNT(*) from fn_listextendedproperty('MS_Description', 
'SCHEMA', N'dbo', 
'TABLE', N'TB_MeasurePoint', 
'COLUMN', N'DirectType')) > 0) 
EXEC sp_updateextendedproperty @name = N'MS_Description', @value = N'信号正反(运行信号相反,故障也是)'
, @level0type = 'SCHEMA', @level0name = N'dbo'
, @level1type = 'TABLE', @level1name = N'TB_MeasurePoint'
, @level2type = 'COLUMN', @level2name = N'DirectType'
ELSE
EXEC sp_addextendedproperty @name = N'MS_Description', @value = N'信号正反(运行信号相反,故障也是)'
, @level0type = 'SCHEMA', @level0name = N'dbo'
, @level1type = 'TABLE', @level1name = N'TB_MeasurePoint'
, @level2type = 'COLUMN', @level2name = N'DirectType'
GO
IF ((SELECT COUNT(*) from fn_listextendedproperty('MS_Description', 
'SCHEMA', N'dbo', 
'TABLE', N'TB_MeasurePoint', 
'COLUMN', N'BizType')) > 0) 
EXEC sp_updateextendedproperty @name = N'MS_Description', @value = N'采集站点分类'
, @level0type = 'SCHEMA', @level0name = N'dbo'
, @level1type = 'TABLE', @level1name = N'TB_MeasurePoint'
, @level2type = 'COLUMN', @level2name = N'BizType'
ELSE
EXEC sp_addextendedproperty @name = N'MS_Description', @value = N'采集站点分类'
, @level0type = 'SCHEMA', @level0name = N'dbo'
, @level1type = 'TABLE', @level1name = N'TB_MeasurePoint'
, @level2type = 'COLUMN', @level2name = N'BizType'
GO
IF ((SELECT COUNT(*) from fn_listextendedproperty('MS_Description', 
'SCHEMA', N'dbo', 
'TABLE', N'TB_MeasurePoint', 
'COLUMN', N'NumTail')) > 0) 
EXEC sp_updateextendedproperty @name = N'MS_Description', @value = N'小数位'
, @level0type = 'SCHEMA', @level0name = N'dbo'
, @level1type = 'TABLE', @level1name = N'TB_MeasurePoint'
, @level2type = 'COLUMN', @level2name = N'NumTail'
ELSE
EXEC sp_addextendedproperty @name = N'MS_Description', @value = N'小数位'
, @level0type = 'SCHEMA', @level0name = N'dbo'
, @level1type = 'TABLE', @level1name = N'TB_MeasurePoint'
, @level2type = 'COLUMN', @level2name = N'NumTail'
GO
IF ((SELECT COUNT(*) from fn_listextendedproperty('MS_Description', 
'SCHEMA', N'dbo', 
'TABLE', N'TB_MeasurePoint', 
'COLUMN', N'prochour')) > 0) 
EXEC sp_updateextendedproperty @name = N'MS_Description', @value = N'小时采样点'
, @level0type = 'SCHEMA', @level0name = N'dbo'
, @level1type = 'TABLE', @level1name = N'TB_MeasurePoint'
, @level2type = 'COLUMN', @level2name = N'prochour'
ELSE
EXEC sp_addextendedproperty @name = N'MS_Description', @value = N'小时采样点'
, @level0type = 'SCHEMA', @level0name = N'dbo'
, @level1type = 'TABLE', @level1name = N'TB_MeasurePoint'
, @level2type = 'COLUMN', @level2name = N'prochour'
GO
IF ((SELECT COUNT(*) from fn_listextendedproperty('MS_Description', 
'SCHEMA', N'dbo', 
'TABLE', N'TB_MeasurePoint', 
'COLUMN', N'procday')) > 0) 
EXEC sp_updateextendedproperty @name = N'MS_Description', @value = N'日采样点'
, @level0type = 'SCHEMA', @level0name = N'dbo'
, @level1type = 'TABLE', @level1name = N'TB_MeasurePoint'
, @level2type = 'COLUMN', @level2name = N'procday'
ELSE
EXEC sp_addextendedproperty @name = N'MS_Description', @value = N'日采样点'
, @level0type = 'SCHEMA', @level0name = N'dbo'
, @level1type = 'TABLE', @level1name = N'TB_MeasurePoint'
, @level2type = 'COLUMN', @level2name = N'procday'
GO
IF ((SELECT COUNT(*) from fn_listextendedproperty('MS_Description', 
'SCHEMA', N'dbo', 
'TABLE', N'TB_MeasurePoint', 
'COLUMN', N'procmonth')) > 0) 
EXEC sp_updateextendedproperty @name = N'MS_Description', @value = N'月采样点'
, @level0type = 'SCHEMA', @level0name = N'dbo'
, @level1type = 'TABLE', @level1name = N'TB_MeasurePoint'
, @level2type = 'COLUMN', @level2name = N'procmonth'
ELSE
EXEC sp_addextendedproperty @name = N'MS_Description', @value = N'月采样点'
, @level0type = 'SCHEMA', @level0name = N'dbo'
, @level1type = 'TABLE', @level1name = N'TB_MeasurePoint'
, @level2type = 'COLUMN', @level2name = N'procmonth'
GO
IF ((SELECT COUNT(*) from fn_listextendedproperty('MS_Description', 
'SCHEMA', N'dbo', 
'TABLE', N'TB_MeasurePoint', 
'COLUMN', N'ShowName')) > 0) 
EXEC sp_updateextendedproperty @name = N'MS_Description', @value = N'是否显示标题'
, @level0type = 'SCHEMA', @level0name = N'dbo'
, @level1type = 'TABLE', @level1name = N'TB_MeasurePoint'
, @level2type = 'COLUMN', @level2name = N'ShowName'
ELSE
EXEC sp_addextendedproperty @name = N'MS_Description', @value = N'是否显示标题'
, @level0type = 'SCHEMA', @level0name = N'dbo'
, @level1type = 'TABLE', @level1name = N'TB_MeasurePoint'
, @level2type = 'COLUMN', @level2name = N'ShowName'
GO
IF ((SELECT COUNT(*) from fn_listextendedproperty('MS_Description', 
'SCHEMA', N'dbo', 
'TABLE', N'TB_MeasurePoint', 
'COLUMN', N'exp')) > 0) 
EXEC sp_updateextendedproperty @name = N'MS_Description', @value = N'相关参数公式'
, @level0type = 'SCHEMA', @level0name = N'dbo'
, @level1type = 'TABLE', @level1name = N'TB_MeasurePoint'
, @level2type = 'COLUMN', @level2name = N'exp'
ELSE
EXEC sp_addextendedproperty @name = N'MS_Description', @value = N'相关参数公式'
, @level0type = 'SCHEMA', @level0name = N'dbo'
, @level1type = 'TABLE', @level1name = N'TB_MeasurePoint'
, @level2type = 'COLUMN', @level2name = N'exp'
GO
IF ((SELECT COUNT(*) from fn_listextendedproperty('MS_Description', 
'SCHEMA', N'dbo', 
'TABLE', N'TB_MeasurePoint', 
'COLUMN', N'forcemin')) > 0) 
EXEC sp_updateextendedproperty @name = N'MS_Description', @value = N'纵轴下限'
, @level0type = 'SCHEMA', @level0name = N'dbo'
, @level1type = 'TABLE', @level1name = N'TB_MeasurePoint'
, @level2type = 'COLUMN', @level2name = N'forcemin'
ELSE
EXEC sp_addextendedproperty @name = N'MS_Description', @value = N'纵轴下限'
, @level0type = 'SCHEMA', @level0name = N'dbo'
, @level1type = 'TABLE', @level1name = N'TB_MeasurePoint'
, @level2type = 'COLUMN', @level2name = N'forcemin'
GO
IF ((SELECT COUNT(*) from fn_listextendedproperty('MS_Description', 
'SCHEMA', N'dbo', 
'TABLE', N'TB_MeasurePoint', 
'COLUMN', N'forcemax')) > 0) 
EXEC sp_updateextendedproperty @name = N'MS_Description', @value = N'纵轴上限'
, @level0type = 'SCHEMA', @level0name = N'dbo'
, @level1type = 'TABLE', @level1name = N'TB_MeasurePoint'
, @level2type = 'COLUMN', @level2name = N'forcemax'
ELSE
EXEC sp_addextendedproperty @name = N'MS_Description', @value = N'纵轴上限'
, @level0type = 'SCHEMA', @level0name = N'dbo'
, @level1type = 'TABLE', @level1name = N'TB_MeasurePoint'
, @level2type = 'COLUMN', @level2name = N'forcemax'
GO
IF ((SELECT COUNT(*) from fn_listextendedproperty('MS_Description', 
'SCHEMA', N'dbo', 
'TABLE', N'TB_MeasurePoint', 
'COLUMN', N'avgmax')) > 0) 
EXEC sp_updateextendedproperty @name = N'MS_Description', @value = N'平均值上限'
, @level0type = 'SCHEMA', @level0name = N'dbo'
, @level1type = 'TABLE', @level1name = N'TB_MeasurePoint'
, @level2type = 'COLUMN', @level2name = N'avgmax'
ELSE
EXEC sp_addextendedproperty @name = N'MS_Description', @value = N'平均值上限'
, @level0type = 'SCHEMA', @level0name = N'dbo'
, @level1type = 'TABLE', @level1name = N'TB_MeasurePoint'
, @level2type = 'COLUMN', @level2name = N'avgmax'
GO
IF ((SELECT COUNT(*) from fn_listextendedproperty('MS_Description', 
'SCHEMA', N'dbo', 
'TABLE', N'TB_MeasurePoint', 
'COLUMN', N'avgmin')) > 0) 
EXEC sp_updateextendedproperty @name = N'MS_Description', @value = N'平均值下限'
, @level0type = 'SCHEMA', @level0name = N'dbo'
, @level1type = 'TABLE', @level1name = N'TB_MeasurePoint'
, @level2type = 'COLUMN', @level2name = N'avgmin'
ELSE
EXEC sp_addextendedproperty @name = N'MS_Description', @value = N'平均值下限'
, @level0type = 'SCHEMA', @level0name = N'dbo'
, @level1type = 'TABLE', @level1name = N'TB_MeasurePoint'
, @level2type = 'COLUMN', @level2name = N'avgmin'
GO
IF ((SELECT COUNT(*) from fn_listextendedproperty('MS_Description', 
'SCHEMA', N'dbo', 
'TABLE', N'TB_MeasurePoint', 
'COLUMN', N'remoteup')) > 0) 
EXEC sp_updateextendedproperty @name = N'MS_Description', @value = N'是否上传数据(0不上传1上传)'
, @level0type = 'SCHEMA', @level0name = N'dbo'
, @level1type = 'TABLE', @level1name = N'TB_MeasurePoint'
, @level2type = 'COLUMN', @level2name = N'remoteup'
ELSE
EXEC sp_addextendedproperty @name = N'MS_Description', @value = N'是否上传数据(0不上传1上传)'
, @level0type = 'SCHEMA', @level0name = N'dbo'
, @level1type = 'TABLE', @level1name = N'TB_MeasurePoint'
, @level2type = 'COLUMN', @level2name = N'remoteup'
GO
IF ((SELECT COUNT(*) from fn_listextendedproperty('MS_Description', 
'SCHEMA', N'dbo', 
'TABLE', N'TB_MeasurePoint', 
'COLUMN', N'morder')) > 0) 
EXEC sp_updateextendedproperty @name = N'MS_Description', @value = N'排列循序'
, @level0type = 'SCHEMA', @level0name = N'dbo'
, @level1type = 'TABLE', @level1name = N'TB_MeasurePoint'
, @level2type = 'COLUMN', @level2name = N'morder'
ELSE
EXEC sp_addextendedproperty @name = N'MS_Description', @value = N'排列循序'
, @level0type = 'SCHEMA', @level0name = N'dbo'
, @level1type = 'TABLE', @level1name = N'TB_MeasurePoint'
, @level2type = 'COLUMN', @level2name = N'morder'
GO
IF ((SELECT COUNT(*) from fn_listextendedproperty('MS_Description', 
'SCHEMA', N'dbo', 
'TABLE', N'TB_MeasurePoint', 
'COLUMN', N'TriggerAlarm')) > 0) 
EXEC sp_updateextendedproperty @name = N'MS_Description', @value = N'是否报警(0不报警1模拟量报警2设备故障报警3设备停机报警)'
, @level0type = 'SCHEMA', @level0name = N'dbo'
, @level1type = 'TABLE', @level1name = N'TB_MeasurePoint'
, @level2type = 'COLUMN', @level2name = N'TriggerAlarm'
ELSE
EXEC sp_addextendedproperty @name = N'MS_Description', @value = N'是否报警(0不报警1模拟量报警2设备故障报警3设备停机报警)'
, @level0type = 'SCHEMA', @level0name = N'dbo'
, @level1type = 'TABLE', @level1name = N'TB_MeasurePoint'
, @level2type = 'COLUMN', @level2name = N'TriggerAlarm'
GO
IF ((SELECT COUNT(*) from fn_listextendedproperty('MS_Description', 
'SCHEMA', N'dbo', 
'TABLE', N'TB_MeasurePoint', 
'COLUMN', N'ConfirmAlarm')) > 0) 
EXEC sp_updateextendedproperty @name = N'MS_Description', @value = N'报警确认(0未确认,1已确认)'
, @level0type = 'SCHEMA', @level0name = N'dbo'
, @level1type = 'TABLE', @level1name = N'TB_MeasurePoint'
, @level2type = 'COLUMN', @level2name = N'ConfirmAlarm'
ELSE
EXEC sp_addextendedproperty @name = N'MS_Description', @value = N'报警确认(0未确认,1已确认)'
, @level0type = 'SCHEMA', @level0name = N'dbo'
, @level1type = 'TABLE', @level1name = N'TB_MeasurePoint'
, @level2type = 'COLUMN', @level2name = N'ConfirmAlarm'
GO
IF ((SELECT COUNT(*) from fn_listextendedproperty('MS_Description', 
'SCHEMA', N'dbo', 
'TABLE', N'TB_MeasurePoint', 
'COLUMN', N'flowset')) > 0) 
EXEC sp_updateextendedproperty @name = N'MS_Description', @value = N'流量和水位设定(泵站专用)'
, @level0type = 'SCHEMA', @level0name = N'dbo'
, @level1type = 'TABLE', @level1name = N'TB_MeasurePoint'
, @level2type = 'COLUMN', @level2name = N'flowset'
ELSE
EXEC sp_addextendedproperty @name = N'MS_Description', @value = N'流量和水位设定(泵站专用)'
, @level0type = 'SCHEMA', @level0name = N'dbo'
, @level1type = 'TABLE', @level1name = N'TB_MeasurePoint'
, @level2type = 'COLUMN', @level2name = N'flowset'
GO
IF ((SELECT COUNT(*) from fn_listextendedproperty('MS_Description', 
'SCHEMA', N'dbo', 
'TABLE', N'TB_MeasurePoint', 
'COLUMN', N'triggerCycle')) > 0) 
EXEC sp_updateextendedproperty @name = N'MS_Description', @value = N'是否监视变化周期'
, @level0type = 'SCHEMA', @level0name = N'dbo'
, @level1type = 'TABLE', @level1name = N'TB_MeasurePoint'
, @level2type = 'COLUMN', @level2name = N'triggerCycle'
ELSE
EXEC sp_addextendedproperty @name = N'MS_Description', @value = N'是否监视变化周期'
, @level0type = 'SCHEMA', @level0name = N'dbo'
, @level1type = 'TABLE', @level1name = N'TB_MeasurePoint'
, @level2type = 'COLUMN', @level2name = N'triggerCycle'
GO
IF ((SELECT COUNT(*) from fn_listextendedproperty('MS_Description', 
'SCHEMA', N'dbo', 
'TABLE', N'TB_MeasurePoint', 
'COLUMN', N'cyclemax')) > 0) 
EXEC sp_updateextendedproperty @name = N'MS_Description', @value = N'不变周期上限'
, @level0type = 'SCHEMA', @level0name = N'dbo'
, @level1type = 'TABLE', @level1name = N'TB_MeasurePoint'
, @level2type = 'COLUMN', @level2name = N'cyclemax'
ELSE
EXEC sp_addextendedproperty @name = N'MS_Description', @value = N'不变周期上限'
, @level0type = 'SCHEMA', @level0name = N'dbo'
, @level1type = 'TABLE', @level1name = N'TB_MeasurePoint'
, @level2type = 'COLUMN', @level2name = N'cyclemax'
GO
IF ((SELECT COUNT(*) from fn_listextendedproperty('MS_Description', 
'SCHEMA', N'dbo', 
'TABLE', N'TB_MeasurePoint', 
'COLUMN', N'cyclemin')) > 0) 
EXEC sp_updateextendedproperty @name = N'MS_Description', @value = N'不变周期下限'
, @level0type = 'SCHEMA', @level0name = N'dbo'
, @level1type = 'TABLE', @level1name = N'TB_MeasurePoint'
, @level2type = 'COLUMN', @level2name = N'cyclemin'
ELSE
EXEC sp_addextendedproperty @name = N'MS_Description', @value = N'不变周期下限'
, @level0type = 'SCHEMA', @level0name = N'dbo'
, @level1type = 'TABLE', @level1name = N'TB_MeasurePoint'
, @level2type = 'COLUMN', @level2name = N'cyclemin'
GO
IF ((SELECT COUNT(*) from fn_listextendedproperty('MS_Description', 
'SCHEMA', N'dbo', 
'TABLE', N'TB_MeasurePoint', 
'COLUMN', N'triggerMutation')) > 0) 
EXEC sp_updateextendedproperty @name = N'MS_Description', @value = N'是否监视突变情况'
, @level0type = 'SCHEMA', @level0name = N'dbo'
, @level1type = 'TABLE', @level1name = N'TB_MeasurePoint'
, @level2type = 'COLUMN', @level2name = N'triggerMutation'
ELSE
EXEC sp_addextendedproperty @name = N'MS_Description', @value = N'是否监视突变情况'
, @level0type = 'SCHEMA', @level0name = N'dbo'
, @level1type = 'TABLE', @level1name = N'TB_MeasurePoint'
, @level2type = 'COLUMN', @level2name = N'triggerMutation'
GO
IF ((SELECT COUNT(*) from fn_listextendedproperty('MS_Description', 
'SCHEMA', N'dbo', 
'TABLE', N'TB_MeasurePoint', 
'COLUMN', N'mutationset')) > 0) 
EXEC sp_updateextendedproperty @name = N'MS_Description', @value = N'突变超限值设定'
, @level0type = 'SCHEMA', @level0name = N'dbo'
, @level1type = 'TABLE', @level1name = N'TB_MeasurePoint'
, @level2type = 'COLUMN', @level2name = N'mutationset'
ELSE
EXEC sp_addextendedproperty @name = N'MS_Description', @value = N'突变超限值设定'
, @level0type = 'SCHEMA', @level0name = N'dbo'
, @level1type = 'TABLE', @level1name = N'TB_MeasurePoint'
, @level2type = 'COLUMN', @level2name = N'mutationset'
GO
IF ((SELECT COUNT(*) from fn_listextendedproperty('MS_Description', 
'SCHEMA', N'dbo', 
'TABLE', N'TB_MeasurePoint', 
'COLUMN', N'causeset')) > 0) 
EXEC sp_updateextendedproperty @name = N'MS_Description', @value = N'未处理初判间隔设定'
, @level0type = 'SCHEMA', @level0name = N'dbo'
, @level1type = 'TABLE', @level1name = N'TB_MeasurePoint'
, @level2type = 'COLUMN', @level2name = N'causeset'
ELSE
EXEC sp_addextendedproperty @name = N'MS_Description', @value = N'未处理初判间隔设定'
, @level0type = 'SCHEMA', @level0name = N'dbo'
, @level1type = 'TABLE', @level1name = N'TB_MeasurePoint'
, @level2type = 'COLUMN', @level2name = N'causeset'
GO
IF ((SELECT COUNT(*) from fn_listextendedproperty('MS_Description', 
'SCHEMA', N'dbo', 
'TABLE', N'TB_MeasurePoint', 
'COLUMN', N'operateset')) > 0) 
EXEC sp_updateextendedproperty @name = N'MS_Description', @value = N'未处理解决间隔设定'
, @level0type = 'SCHEMA', @level0name = N'dbo'
, @level1type = 'TABLE', @level1name = N'TB_MeasurePoint'
, @level2type = 'COLUMN', @level2name = N'operateset'
ELSE
EXEC sp_addextendedproperty @name = N'MS_Description', @value = N'未处理解决间隔设定'
, @level0type = 'SCHEMA', @level0name = N'dbo'
, @level1type = 'TABLE', @level1name = N'TB_MeasurePoint'
, @level2type = 'COLUMN', @level2name = N'operateset'
GO
IF ((SELECT COUNT(*) from fn_listextendedproperty('MS_Description', 
'SCHEMA', N'dbo', 
'TABLE', N'TB_MeasurePoint', 
'COLUMN', N'resultset')) > 0) 
EXEC sp_updateextendedproperty @name = N'MS_Description', @value = N'未处理结果间隔设定'
, @level0type = 'SCHEMA', @level0name = N'dbo'
, @level1type = 'TABLE', @level1name = N'TB_MeasurePoint'
, @level2type = 'COLUMN', @level2name = N'resultset'
ELSE
EXEC sp_addextendedproperty @name = N'MS_Description', @value = N'未处理结果间隔设定'
, @level0type = 'SCHEMA', @level0name = N'dbo'
, @level1type = 'TABLE', @level1name = N'TB_MeasurePoint'
, @level2type = 'COLUMN', @level2name = N'resultset'
GO
IF ((SELECT COUNT(*) from fn_listextendedproperty('MS_Description', 
'SCHEMA', N'dbo', 
'TABLE', N'TB_MeasurePoint', 
'COLUMN', N'triggerEquOff')) > 0) 
EXEC sp_updateextendedproperty @name = N'MS_Description', @value = N'是否触发设备停机报警'
, @level0type = 'SCHEMA', @level0name = N'dbo'
, @level1type = 'TABLE', @level1name = N'TB_MeasurePoint'
, @level2type = 'COLUMN', @level2name = N'triggerEquOff'
ELSE
EXEC sp_addextendedproperty @name = N'MS_Description', @value = N'是否触发设备停机报警'
, @level0type = 'SCHEMA', @level0name = N'dbo'
, @level1type = 'TABLE', @level1name = N'TB_MeasurePoint'
, @level2type = 'COLUMN', @level2name = N'triggerEquOff'
GO
IF ((SELECT COUNT(*) from fn_listextendedproperty('MS_Description', 
'SCHEMA', N'dbo', 
'TABLE', N'TB_MeasurePoint', 
'COLUMN', N'mathop')) > 0) 
EXEC sp_updateextendedproperty @name = N'MS_Description', @value = N'运算操作符,默认为乘法(=-*/)'
, @level0type = 'SCHEMA', @level0name = N'dbo'
, @level1type = 'TABLE', @level1name = N'TB_MeasurePoint'
, @level2type = 'COLUMN', @level2name = N'mathop'
ELSE
EXEC sp_addextendedproperty @name = N'MS_Description', @value = N'运算操作符,默认为乘法(=-*/)'
, @level0type = 'SCHEMA', @level0name = N'dbo'
, @level1type = 'TABLE', @level1name = N'TB_MeasurePoint'
, @level2type = 'COLUMN', @level2name = N'mathop'
GO
IF ((SELECT COUNT(*) from fn_listextendedproperty('MS_Description', 
'SCHEMA', N'dbo', 
'TABLE', N'TB_MeasurePoint', 
'COLUMN', N'active')) > 0) 
EXEC sp_updateextendedproperty @name = N'MS_Description', @value = N'是否在历史数据中显示'
, @level0type = 'SCHEMA', @level0name = N'dbo'
, @level1type = 'TABLE', @level1name = N'TB_MeasurePoint'
, @level2type = 'COLUMN', @level2name = N'active'
ELSE
EXEC sp_addextendedproperty @name = N'MS_Description', @value = N'是否在历史数据中显示'
, @level0type = 'SCHEMA', @level0name = N'dbo'
, @level1type = 'TABLE', @level1name = N'TB_MeasurePoint'
, @level2type = 'COLUMN', @level2name = N'active'
GO
IF ((SELECT COUNT(*) from fn_listextendedproperty('MS_Description', 
'SCHEMA', N'dbo', 
'TABLE', N'TB_MeasurePoint', 
'COLUMN', N'SoundAlarm')) > 0) 
EXEC sp_updateextendedproperty @name = N'MS_Description', @value = N'是否触发声音报警'
, @level0type = 'SCHEMA', @level0name = N'dbo'
, @level1type = 'TABLE', @level1name = N'TB_MeasurePoint'
, @level2type = 'COLUMN', @level2name = N'SoundAlarm'
ELSE
EXEC sp_addextendedproperty @name = N'MS_Description', @value = N'是否触发声音报警'
, @level0type = 'SCHEMA', @level0name = N'dbo'
, @level1type = 'TABLE', @level1name = N'TB_MeasurePoint'
, @level2type = 'COLUMN', @level2name = N'SoundAlarm'
GO
IF ((SELECT COUNT(*) from fn_listextendedproperty('MS_Description', 
'SCHEMA', N'dbo', 
'TABLE', N'TB_MeasurePoint', 
'COLUMN', N'scdtype')) > 0) 
EXEC sp_updateextendedproperty @name = N'MS_Description', @value = N'区域类别'
, @level0type = 'SCHEMA', @level0name = N'dbo'
, @level1type = 'TABLE', @level1name = N'TB_MeasurePoint'
, @level2type = 'COLUMN', @level2name = N'scdtype'
ELSE
EXEC sp_addextendedproperty @name = N'MS_Description', @value = N'区域类别'
, @level0type = 'SCHEMA', @level0name = N'dbo'
, @level1type = 'TABLE', @level1name = N'TB_MeasurePoint'
, @level2type = 'COLUMN', @level2name = N'scdtype'
GO
IF ((SELECT COUNT(*) from fn_listextendedproperty('MS_Description', 
'SCHEMA', N'dbo', 
'TABLE', N'TB_MeasurePoint', 
'COLUMN', N'spanrange')) > 0) 
EXEC sp_updateextendedproperty @name = N'MS_Description', @value = N'量程最大值'
, @level0type = 'SCHEMA', @level0name = N'dbo'
, @level1type = 'TABLE', @level1name = N'TB_MeasurePoint'
, @level2type = 'COLUMN', @level2name = N'spanrange'
ELSE
EXEC sp_addextendedproperty @name = N'MS_Description', @value = N'量程最大值'
, @level0type = 'SCHEMA', @level0name = N'dbo'
, @level1type = 'TABLE', @level1name = N'TB_MeasurePoint'
, @level2type = 'COLUMN', @level2name = N'spanrange'
GO
IF ((SELECT COUNT(*) from fn_listextendedproperty('MS_Description', 
'SCHEMA', N'dbo', 
'TABLE', N'TB_MeasurePoint', 
'COLUMN', N'disname')) > 0) 
EXEC sp_updateextendedproperty @name = N'MS_Description', @value = N'测量点简称'
, @level0type = 'SCHEMA', @level0name = N'dbo'
, @level1type = 'TABLE', @level1name = N'TB_MeasurePoint'
, @level2type = 'COLUMN', @level2name = N'disname'
ELSE
EXEC sp_addextendedproperty @name = N'MS_Description', @value = N'测量点简称'
, @level0type = 'SCHEMA', @level0name = N'dbo'
, @level1type = 'TABLE', @level1name = N'TB_MeasurePoint'
, @level2type = 'COLUMN', @level2name = N'disname'
GO

-- ----------------------------
-- Records of TB_MeasurePoint
-- ----------------------------

-- ----------------------------
-- Table structure for TB_MeasurePoint_copy1
-- ----------------------------
DROP TABLE [dbo].[TB_MeasurePoint_copy1]
GO
CREATE TABLE [dbo].[TB_MeasurePoint_copy1] (
[ID] varchar(50) NULL ,
[MPointID] varchar(50) NOT NULL ,
[MPointCode] varchar(50) NOT NULL ,
[ParmName] varchar(50) NULL ,
[ParmValue] money NULL ,
[MeasureDT] datetime NULL ,
[Unit] nvarchar(50) NULL ,
[alarmmax] money NULL ,
[alarmmin] money NULL ,
[halarmmax] money NULL ,
[lalarmmin] money NULL ,
[Rate] money NULL DEFAULT ((1)) ,
[Freq] int NULL DEFAULT ((1)) ,
[FreqUnit] varchar(10) NULL ,
[SignalType] varchar(50) NULL DEFAULT ('AI') ,
[LEDType] char(1) NULL DEFAULT ('1') ,
[LEDColor] char(1) NULL DEFAULT ('1') ,
[DirectType] char(1) NULL DEFAULT ('1') ,
[BizId] varchar(50) NULL ,
[BizType] varchar(50) NULL DEFAULT ('污水') ,
[NumTail] varchar(10) NULL DEFAULT ('0.00') ,
[prochour] varchar(50) NULL ,
[procday] varchar(50) NULL ,
[procmonth] varchar(50) NULL ,
[ShowName] char(1) NULL DEFAULT ('1') ,
[exp] varchar(50) NULL ,
[forcemin] money NULL ,
[forcemax] money NULL ,
[avgmax] money NULL ,
[avgmin] money NULL ,
[remoteup] char(1) NULL DEFAULT ('0') ,
[morder] int NULL DEFAULT ((999)) ,
[TriggerAlarm] char(1) NULL DEFAULT ('0') ,
[ConfirmAlarm] char(1) NULL DEFAULT ('0') ,
[flowset] money NULL DEFAULT ((0)) ,
[triggerCycle] char(1) NULL DEFAULT ('0') ,
[cyclemax] money NULL ,
[cyclemin] money NULL ,
[triggerMutation] char(1) NULL DEFAULT ('0') ,
[mutationset] money NULL ,
[causeset] money NULL ,
[operateset] money NULL ,
[resultset] money NULL ,
[triggerEquOff] char(1) NULL DEFAULT ('0') ,
[mathop] char(1) NULL DEFAULT ('*') ,
[valuetype] varchar(50) NULL ,
[valueMeaning] varchar(50) NULL ,
[active] varchar(50) NULL DEFAULT ('启用') ,
[SoundAlarm] char(1) NULL DEFAULT ('0') ,
[scdtype] varchar(50) NULL ,
[spanrange] money NULL ,
[modbusfigid] varchar(50) NULL ,
[register] varchar(50) NULL ,
[processSectionCode] varchar(50) NULL ,
[equipmentId] varchar(50) NULL ,
[source_type] varchar(50) NULL ,
[patrol_type] varchar(10) NULL ,
[remark] varchar(500) NULL ,
[alarm_level] int NULL ,
[disname] varchar(50) NULL ,
[pointfunctiondefinition] varchar(50) NULL ,
[type] varchar(50) NULL 
)


GO
IF ((SELECT COUNT(*) from fn_listextendedproperty('MS_Description', 
'SCHEMA', N'dbo', 
'TABLE', N'TB_MeasurePoint_copy1', 
'COLUMN', N'MPointID')) > 0) 
EXEC sp_updateextendedproperty @name = N'MS_Description', @value = N'测量点ID'
, @level0type = 'SCHEMA', @level0name = N'dbo'
, @level1type = 'TABLE', @level1name = N'TB_MeasurePoint_copy1'
, @level2type = 'COLUMN', @level2name = N'MPointID'
ELSE
EXEC sp_addextendedproperty @name = N'MS_Description', @value = N'测量点ID'
, @level0type = 'SCHEMA', @level0name = N'dbo'
, @level1type = 'TABLE', @level1name = N'TB_MeasurePoint_copy1'
, @level2type = 'COLUMN', @level2name = N'MPointID'
GO
IF ((SELECT COUNT(*) from fn_listextendedproperty('MS_Description', 
'SCHEMA', N'dbo', 
'TABLE', N'TB_MeasurePoint_copy1', 
'COLUMN', N'MPointCode')) > 0) 
EXEC sp_updateextendedproperty @name = N'MS_Description', @value = N'测量点名'
, @level0type = 'SCHEMA', @level0name = N'dbo'
, @level1type = 'TABLE', @level1name = N'TB_MeasurePoint_copy1'
, @level2type = 'COLUMN', @level2name = N'MPointCode'
ELSE
EXEC sp_addextendedproperty @name = N'MS_Description', @value = N'测量点名'
, @level0type = 'SCHEMA', @level0name = N'dbo'
, @level1type = 'TABLE', @level1name = N'TB_MeasurePoint_copy1'
, @level2type = 'COLUMN', @level2name = N'MPointCode'
GO
IF ((SELECT COUNT(*) from fn_listextendedproperty('MS_Description', 
'SCHEMA', N'dbo', 
'TABLE', N'TB_MeasurePoint_copy1', 
'COLUMN', N'ParmName')) > 0) 
EXEC sp_updateextendedproperty @name = N'MS_Description', @value = N'测量点中文描述'
, @level0type = 'SCHEMA', @level0name = N'dbo'
, @level1type = 'TABLE', @level1name = N'TB_MeasurePoint_copy1'
, @level2type = 'COLUMN', @level2name = N'ParmName'
ELSE
EXEC sp_addextendedproperty @name = N'MS_Description', @value = N'测量点中文描述'
, @level0type = 'SCHEMA', @level0name = N'dbo'
, @level1type = 'TABLE', @level1name = N'TB_MeasurePoint_copy1'
, @level2type = 'COLUMN', @level2name = N'ParmName'
GO
IF ((SELECT COUNT(*) from fn_listextendedproperty('MS_Description', 
'SCHEMA', N'dbo', 
'TABLE', N'TB_MeasurePoint_copy1', 
'COLUMN', N'ParmValue')) > 0) 
EXEC sp_updateextendedproperty @name = N'MS_Description', @value = N'采集值'
, @level0type = 'SCHEMA', @level0name = N'dbo'
, @level1type = 'TABLE', @level1name = N'TB_MeasurePoint_copy1'
, @level2type = 'COLUMN', @level2name = N'ParmValue'
ELSE
EXEC sp_addextendedproperty @name = N'MS_Description', @value = N'采集值'
, @level0type = 'SCHEMA', @level0name = N'dbo'
, @level1type = 'TABLE', @level1name = N'TB_MeasurePoint_copy1'
, @level2type = 'COLUMN', @level2name = N'ParmValue'
GO
IF ((SELECT COUNT(*) from fn_listextendedproperty('MS_Description', 
'SCHEMA', N'dbo', 
'TABLE', N'TB_MeasurePoint_copy1', 
'COLUMN', N'MeasureDT')) > 0) 
EXEC sp_updateextendedproperty @name = N'MS_Description', @value = N'采集时间'
, @level0type = 'SCHEMA', @level0name = N'dbo'
, @level1type = 'TABLE', @level1name = N'TB_MeasurePoint_copy1'
, @level2type = 'COLUMN', @level2name = N'MeasureDT'
ELSE
EXEC sp_addextendedproperty @name = N'MS_Description', @value = N'采集时间'
, @level0type = 'SCHEMA', @level0name = N'dbo'
, @level1type = 'TABLE', @level1name = N'TB_MeasurePoint_copy1'
, @level2type = 'COLUMN', @level2name = N'MeasureDT'
GO
IF ((SELECT COUNT(*) from fn_listextendedproperty('MS_Description', 
'SCHEMA', N'dbo', 
'TABLE', N'TB_MeasurePoint_copy1', 
'COLUMN', N'Unit')) > 0) 
EXEC sp_updateextendedproperty @name = N'MS_Description', @value = N'单位'
, @level0type = 'SCHEMA', @level0name = N'dbo'
, @level1type = 'TABLE', @level1name = N'TB_MeasurePoint_copy1'
, @level2type = 'COLUMN', @level2name = N'Unit'
ELSE
EXEC sp_addextendedproperty @name = N'MS_Description', @value = N'单位'
, @level0type = 'SCHEMA', @level0name = N'dbo'
, @level1type = 'TABLE', @level1name = N'TB_MeasurePoint_copy1'
, @level2type = 'COLUMN', @level2name = N'Unit'
GO
IF ((SELECT COUNT(*) from fn_listextendedproperty('MS_Description', 
'SCHEMA', N'dbo', 
'TABLE', N'TB_MeasurePoint_copy1', 
'COLUMN', N'alarmmax')) > 0) 
EXEC sp_updateextendedproperty @name = N'MS_Description', @value = N'报警上限'
, @level0type = 'SCHEMA', @level0name = N'dbo'
, @level1type = 'TABLE', @level1name = N'TB_MeasurePoint_copy1'
, @level2type = 'COLUMN', @level2name = N'alarmmax'
ELSE
EXEC sp_addextendedproperty @name = N'MS_Description', @value = N'报警上限'
, @level0type = 'SCHEMA', @level0name = N'dbo'
, @level1type = 'TABLE', @level1name = N'TB_MeasurePoint_copy1'
, @level2type = 'COLUMN', @level2name = N'alarmmax'
GO
IF ((SELECT COUNT(*) from fn_listextendedproperty('MS_Description', 
'SCHEMA', N'dbo', 
'TABLE', N'TB_MeasurePoint_copy1', 
'COLUMN', N'alarmmin')) > 0) 
EXEC sp_updateextendedproperty @name = N'MS_Description', @value = N'报警下限'
, @level0type = 'SCHEMA', @level0name = N'dbo'
, @level1type = 'TABLE', @level1name = N'TB_MeasurePoint_copy1'
, @level2type = 'COLUMN', @level2name = N'alarmmin'
ELSE
EXEC sp_addextendedproperty @name = N'MS_Description', @value = N'报警下限'
, @level0type = 'SCHEMA', @level0name = N'dbo'
, @level1type = 'TABLE', @level1name = N'TB_MeasurePoint_copy1'
, @level2type = 'COLUMN', @level2name = N'alarmmin'
GO
IF ((SELECT COUNT(*) from fn_listextendedproperty('MS_Description', 
'SCHEMA', N'dbo', 
'TABLE', N'TB_MeasurePoint_copy1', 
'COLUMN', N'Rate')) > 0) 
EXEC sp_updateextendedproperty @name = N'MS_Description', @value = N'系数'
, @level0type = 'SCHEMA', @level0name = N'dbo'
, @level1type = 'TABLE', @level1name = N'TB_MeasurePoint_copy1'
, @level2type = 'COLUMN', @level2name = N'Rate'
ELSE
EXEC sp_addextendedproperty @name = N'MS_Description', @value = N'系数'
, @level0type = 'SCHEMA', @level0name = N'dbo'
, @level1type = 'TABLE', @level1name = N'TB_MeasurePoint_copy1'
, @level2type = 'COLUMN', @level2name = N'Rate'
GO
IF ((SELECT COUNT(*) from fn_listextendedproperty('MS_Description', 
'SCHEMA', N'dbo', 
'TABLE', N'TB_MeasurePoint_copy1', 
'COLUMN', N'Freq')) > 0) 
EXEC sp_updateextendedproperty @name = N'MS_Description', @value = N'刷新间隔'
, @level0type = 'SCHEMA', @level0name = N'dbo'
, @level1type = 'TABLE', @level1name = N'TB_MeasurePoint_copy1'
, @level2type = 'COLUMN', @level2name = N'Freq'
ELSE
EXEC sp_addextendedproperty @name = N'MS_Description', @value = N'刷新间隔'
, @level0type = 'SCHEMA', @level0name = N'dbo'
, @level1type = 'TABLE', @level1name = N'TB_MeasurePoint_copy1'
, @level2type = 'COLUMN', @level2name = N'Freq'
GO
IF ((SELECT COUNT(*) from fn_listextendedproperty('MS_Description', 
'SCHEMA', N'dbo', 
'TABLE', N'TB_MeasurePoint_copy1', 
'COLUMN', N'SignalType')) > 0) 
EXEC sp_updateextendedproperty @name = N'MS_Description', @value = N'测量点分类(AI模拟DI数字)'
, @level0type = 'SCHEMA', @level0name = N'dbo'
, @level1type = 'TABLE', @level1name = N'TB_MeasurePoint_copy1'
, @level2type = 'COLUMN', @level2name = N'SignalType'
ELSE
EXEC sp_addextendedproperty @name = N'MS_Description', @value = N'测量点分类(AI模拟DI数字)'
, @level0type = 'SCHEMA', @level0name = N'dbo'
, @level1type = 'TABLE', @level1name = N'TB_MeasurePoint_copy1'
, @level2type = 'COLUMN', @level2name = N'SignalType'
GO
IF ((SELECT COUNT(*) from fn_listextendedproperty('MS_Description', 
'SCHEMA', N'dbo', 
'TABLE', N'TB_MeasurePoint_copy1', 
'COLUMN', N'LEDType')) > 0) 
EXEC sp_updateextendedproperty @name = N'MS_Description', @value = N'开关量分类(1运行0故障)'
, @level0type = 'SCHEMA', @level0name = N'dbo'
, @level1type = 'TABLE', @level1name = N'TB_MeasurePoint_copy1'
, @level2type = 'COLUMN', @level2name = N'LEDType'
ELSE
EXEC sp_addextendedproperty @name = N'MS_Description', @value = N'开关量分类(1运行0故障)'
, @level0type = 'SCHEMA', @level0name = N'dbo'
, @level1type = 'TABLE', @level1name = N'TB_MeasurePoint_copy1'
, @level2type = 'COLUMN', @level2name = N'LEDType'
GO
IF ((SELECT COUNT(*) from fn_listextendedproperty('MS_Description', 
'SCHEMA', N'dbo', 
'TABLE', N'TB_MeasurePoint_copy1', 
'COLUMN', N'LEDColor')) > 0) 
EXEC sp_updateextendedproperty @name = N'MS_Description', @value = N'颜色正反(1红开绿停0绿开红停)'
, @level0type = 'SCHEMA', @level0name = N'dbo'
, @level1type = 'TABLE', @level1name = N'TB_MeasurePoint_copy1'
, @level2type = 'COLUMN', @level2name = N'LEDColor'
ELSE
EXEC sp_addextendedproperty @name = N'MS_Description', @value = N'颜色正反(1红开绿停0绿开红停)'
, @level0type = 'SCHEMA', @level0name = N'dbo'
, @level1type = 'TABLE', @level1name = N'TB_MeasurePoint_copy1'
, @level2type = 'COLUMN', @level2name = N'LEDColor'
GO
IF ((SELECT COUNT(*) from fn_listextendedproperty('MS_Description', 
'SCHEMA', N'dbo', 
'TABLE', N'TB_MeasurePoint_copy1', 
'COLUMN', N'DirectType')) > 0) 
EXEC sp_updateextendedproperty @name = N'MS_Description', @value = N'信号正反(运行信号相反,故障也是)'
, @level0type = 'SCHEMA', @level0name = N'dbo'
, @level1type = 'TABLE', @level1name = N'TB_MeasurePoint_copy1'
, @level2type = 'COLUMN', @level2name = N'DirectType'
ELSE
EXEC sp_addextendedproperty @name = N'MS_Description', @value = N'信号正反(运行信号相反,故障也是)'
, @level0type = 'SCHEMA', @level0name = N'dbo'
, @level1type = 'TABLE', @level1name = N'TB_MeasurePoint_copy1'
, @level2type = 'COLUMN', @level2name = N'DirectType'
GO
IF ((SELECT COUNT(*) from fn_listextendedproperty('MS_Description', 
'SCHEMA', N'dbo', 
'TABLE', N'TB_MeasurePoint_copy1', 
'COLUMN', N'BizType')) > 0) 
EXEC sp_updateextendedproperty @name = N'MS_Description', @value = N'采集站点分类'
, @level0type = 'SCHEMA', @level0name = N'dbo'
, @level1type = 'TABLE', @level1name = N'TB_MeasurePoint_copy1'
, @level2type = 'COLUMN', @level2name = N'BizType'
ELSE
EXEC sp_addextendedproperty @name = N'MS_Description', @value = N'采集站点分类'
, @level0type = 'SCHEMA', @level0name = N'dbo'
, @level1type = 'TABLE', @level1name = N'TB_MeasurePoint_copy1'
, @level2type = 'COLUMN', @level2name = N'BizType'
GO
IF ((SELECT COUNT(*) from fn_listextendedproperty('MS_Description', 
'SCHEMA', N'dbo', 
'TABLE', N'TB_MeasurePoint_copy1', 
'COLUMN', N'NumTail')) > 0) 
EXEC sp_updateextendedproperty @name = N'MS_Description', @value = N'小数位'
, @level0type = 'SCHEMA', @level0name = N'dbo'
, @level1type = 'TABLE', @level1name = N'TB_MeasurePoint_copy1'
, @level2type = 'COLUMN', @level2name = N'NumTail'
ELSE
EXEC sp_addextendedproperty @name = N'MS_Description', @value = N'小数位'
, @level0type = 'SCHEMA', @level0name = N'dbo'
, @level1type = 'TABLE', @level1name = N'TB_MeasurePoint_copy1'
, @level2type = 'COLUMN', @level2name = N'NumTail'
GO
IF ((SELECT COUNT(*) from fn_listextendedproperty('MS_Description', 
'SCHEMA', N'dbo', 
'TABLE', N'TB_MeasurePoint_copy1', 
'COLUMN', N'prochour')) > 0) 
EXEC sp_updateextendedproperty @name = N'MS_Description', @value = N'小时采样点'
, @level0type = 'SCHEMA', @level0name = N'dbo'
, @level1type = 'TABLE', @level1name = N'TB_MeasurePoint_copy1'
, @level2type = 'COLUMN', @level2name = N'prochour'
ELSE
EXEC sp_addextendedproperty @name = N'MS_Description', @value = N'小时采样点'
, @level0type = 'SCHEMA', @level0name = N'dbo'
, @level1type = 'TABLE', @level1name = N'TB_MeasurePoint_copy1'
, @level2type = 'COLUMN', @level2name = N'prochour'
GO
IF ((SELECT COUNT(*) from fn_listextendedproperty('MS_Description', 
'SCHEMA', N'dbo', 
'TABLE', N'TB_MeasurePoint_copy1', 
'COLUMN', N'procday')) > 0) 
EXEC sp_updateextendedproperty @name = N'MS_Description', @value = N'日采样点'
, @level0type = 'SCHEMA', @level0name = N'dbo'
, @level1type = 'TABLE', @level1name = N'TB_MeasurePoint_copy1'
, @level2type = 'COLUMN', @level2name = N'procday'
ELSE
EXEC sp_addextendedproperty @name = N'MS_Description', @value = N'日采样点'
, @level0type = 'SCHEMA', @level0name = N'dbo'
, @level1type = 'TABLE', @level1name = N'TB_MeasurePoint_copy1'
, @level2type = 'COLUMN', @level2name = N'procday'
GO
IF ((SELECT COUNT(*) from fn_listextendedproperty('MS_Description', 
'SCHEMA', N'dbo', 
'TABLE', N'TB_MeasurePoint_copy1', 
'COLUMN', N'procmonth')) > 0) 
EXEC sp_updateextendedproperty @name = N'MS_Description', @value = N'月采样点'
, @level0type = 'SCHEMA', @level0name = N'dbo'
, @level1type = 'TABLE', @level1name = N'TB_MeasurePoint_copy1'
, @level2type = 'COLUMN', @level2name = N'procmonth'
ELSE
EXEC sp_addextendedproperty @name = N'MS_Description', @value = N'月采样点'
, @level0type = 'SCHEMA', @level0name = N'dbo'
, @level1type = 'TABLE', @level1name = N'TB_MeasurePoint_copy1'
, @level2type = 'COLUMN', @level2name = N'procmonth'
GO
IF ((SELECT COUNT(*) from fn_listextendedproperty('MS_Description', 
'SCHEMA', N'dbo', 
'TABLE', N'TB_MeasurePoint_copy1', 
'COLUMN', N'ShowName')) > 0) 
EXEC sp_updateextendedproperty @name = N'MS_Description', @value = N'是否显示标题'
, @level0type = 'SCHEMA', @level0name = N'dbo'
, @level1type = 'TABLE', @level1name = N'TB_MeasurePoint_copy1'
, @level2type = 'COLUMN', @level2name = N'ShowName'
ELSE
EXEC sp_addextendedproperty @name = N'MS_Description', @value = N'是否显示标题'
, @level0type = 'SCHEMA', @level0name = N'dbo'
, @level1type = 'TABLE', @level1name = N'TB_MeasurePoint_copy1'
, @level2type = 'COLUMN', @level2name = N'ShowName'
GO
IF ((SELECT COUNT(*) from fn_listextendedproperty('MS_Description', 
'SCHEMA', N'dbo', 
'TABLE', N'TB_MeasurePoint_copy1', 
'COLUMN', N'exp')) > 0) 
EXEC sp_updateextendedproperty @name = N'MS_Description', @value = N'相关参数公式'
, @level0type = 'SCHEMA', @level0name = N'dbo'
, @level1type = 'TABLE', @level1name = N'TB_MeasurePoint_copy1'
, @level2type = 'COLUMN', @level2name = N'exp'
ELSE
EXEC sp_addextendedproperty @name = N'MS_Description', @value = N'相关参数公式'
, @level0type = 'SCHEMA', @level0name = N'dbo'
, @level1type = 'TABLE', @level1name = N'TB_MeasurePoint_copy1'
, @level2type = 'COLUMN', @level2name = N'exp'
GO
IF ((SELECT COUNT(*) from fn_listextendedproperty('MS_Description', 
'SCHEMA', N'dbo', 
'TABLE', N'TB_MeasurePoint_copy1', 
'COLUMN', N'forcemin')) > 0) 
EXEC sp_updateextendedproperty @name = N'MS_Description', @value = N'纵轴下限'
, @level0type = 'SCHEMA', @level0name = N'dbo'
, @level1type = 'TABLE', @level1name = N'TB_MeasurePoint_copy1'
, @level2type = 'COLUMN', @level2name = N'forcemin'
ELSE
EXEC sp_addextendedproperty @name = N'MS_Description', @value = N'纵轴下限'
, @level0type = 'SCHEMA', @level0name = N'dbo'
, @level1type = 'TABLE', @level1name = N'TB_MeasurePoint_copy1'
, @level2type = 'COLUMN', @level2name = N'forcemin'
GO
IF ((SELECT COUNT(*) from fn_listextendedproperty('MS_Description', 
'SCHEMA', N'dbo', 
'TABLE', N'TB_MeasurePoint_copy1', 
'COLUMN', N'forcemax')) > 0) 
EXEC sp_updateextendedproperty @name = N'MS_Description', @value = N'纵轴上限'
, @level0type = 'SCHEMA', @level0name = N'dbo'
, @level1type = 'TABLE', @level1name = N'TB_MeasurePoint_copy1'
, @level2type = 'COLUMN', @level2name = N'forcemax'
ELSE
EXEC sp_addextendedproperty @name = N'MS_Description', @value = N'纵轴上限'
, @level0type = 'SCHEMA', @level0name = N'dbo'
, @level1type = 'TABLE', @level1name = N'TB_MeasurePoint_copy1'
, @level2type = 'COLUMN', @level2name = N'forcemax'
GO
IF ((SELECT COUNT(*) from fn_listextendedproperty('MS_Description', 
'SCHEMA', N'dbo', 
'TABLE', N'TB_MeasurePoint_copy1', 
'COLUMN', N'avgmax')) > 0) 
EXEC sp_updateextendedproperty @name = N'MS_Description', @value = N'平均值上限'
, @level0type = 'SCHEMA', @level0name = N'dbo'
, @level1type = 'TABLE', @level1name = N'TB_MeasurePoint_copy1'
, @level2type = 'COLUMN', @level2name = N'avgmax'
ELSE
EXEC sp_addextendedproperty @name = N'MS_Description', @value = N'平均值上限'
, @level0type = 'SCHEMA', @level0name = N'dbo'
, @level1type = 'TABLE', @level1name = N'TB_MeasurePoint_copy1'
, @level2type = 'COLUMN', @level2name = N'avgmax'
GO
IF ((SELECT COUNT(*) from fn_listextendedproperty('MS_Description', 
'SCHEMA', N'dbo', 
'TABLE', N'TB_MeasurePoint_copy1', 
'COLUMN', N'avgmin')) > 0) 
EXEC sp_updateextendedproperty @name = N'MS_Description', @value = N'平均值下限'
, @level0type = 'SCHEMA', @level0name = N'dbo'
, @level1type = 'TABLE', @level1name = N'TB_MeasurePoint_copy1'
, @level2type = 'COLUMN', @level2name = N'avgmin'
ELSE
EXEC sp_addextendedproperty @name = N'MS_Description', @value = N'平均值下限'
, @level0type = 'SCHEMA', @level0name = N'dbo'
, @level1type = 'TABLE', @level1name = N'TB_MeasurePoint_copy1'
, @level2type = 'COLUMN', @level2name = N'avgmin'
GO
IF ((SELECT COUNT(*) from fn_listextendedproperty('MS_Description', 
'SCHEMA', N'dbo', 
'TABLE', N'TB_MeasurePoint_copy1', 
'COLUMN', N'remoteup')) > 0) 
EXEC sp_updateextendedproperty @name = N'MS_Description', @value = N'是否上传数据(0不上传1上传)'
, @level0type = 'SCHEMA', @level0name = N'dbo'
, @level1type = 'TABLE', @level1name = N'TB_MeasurePoint_copy1'
, @level2type = 'COLUMN', @level2name = N'remoteup'
ELSE
EXEC sp_addextendedproperty @name = N'MS_Description', @value = N'是否上传数据(0不上传1上传)'
, @level0type = 'SCHEMA', @level0name = N'dbo'
, @level1type = 'TABLE', @level1name = N'TB_MeasurePoint_copy1'
, @level2type = 'COLUMN', @level2name = N'remoteup'
GO
IF ((SELECT COUNT(*) from fn_listextendedproperty('MS_Description', 
'SCHEMA', N'dbo', 
'TABLE', N'TB_MeasurePoint_copy1', 
'COLUMN', N'morder')) > 0) 
EXEC sp_updateextendedproperty @name = N'MS_Description', @value = N'排列循序'
, @level0type = 'SCHEMA', @level0name = N'dbo'
, @level1type = 'TABLE', @level1name = N'TB_MeasurePoint_copy1'
, @level2type = 'COLUMN', @level2name = N'morder'
ELSE
EXEC sp_addextendedproperty @name = N'MS_Description', @value = N'排列循序'
, @level0type = 'SCHEMA', @level0name = N'dbo'
, @level1type = 'TABLE', @level1name = N'TB_MeasurePoint_copy1'
, @level2type = 'COLUMN', @level2name = N'morder'
GO
IF ((SELECT COUNT(*) from fn_listextendedproperty('MS_Description', 
'SCHEMA', N'dbo', 
'TABLE', N'TB_MeasurePoint_copy1', 
'COLUMN', N'TriggerAlarm')) > 0) 
EXEC sp_updateextendedproperty @name = N'MS_Description', @value = N'是否报警(0不报警1模拟量报警2设备故障报警3设备停机报警)'
, @level0type = 'SCHEMA', @level0name = N'dbo'
, @level1type = 'TABLE', @level1name = N'TB_MeasurePoint_copy1'
, @level2type = 'COLUMN', @level2name = N'TriggerAlarm'
ELSE
EXEC sp_addextendedproperty @name = N'MS_Description', @value = N'是否报警(0不报警1模拟量报警2设备故障报警3设备停机报警)'
, @level0type = 'SCHEMA', @level0name = N'dbo'
, @level1type = 'TABLE', @level1name = N'TB_MeasurePoint_copy1'
, @level2type = 'COLUMN', @level2name = N'TriggerAlarm'
GO
IF ((SELECT COUNT(*) from fn_listextendedproperty('MS_Description', 
'SCHEMA', N'dbo', 
'TABLE', N'TB_MeasurePoint_copy1', 
'COLUMN', N'ConfirmAlarm')) > 0) 
EXEC sp_updateextendedproperty @name = N'MS_Description', @value = N'报警确认(0未确认,1已确认)'
, @level0type = 'SCHEMA', @level0name = N'dbo'
, @level1type = 'TABLE', @level1name = N'TB_MeasurePoint_copy1'
, @level2type = 'COLUMN', @level2name = N'ConfirmAlarm'
ELSE
EXEC sp_addextendedproperty @name = N'MS_Description', @value = N'报警确认(0未确认,1已确认)'
, @level0type = 'SCHEMA', @level0name = N'dbo'
, @level1type = 'TABLE', @level1name = N'TB_MeasurePoint_copy1'
, @level2type = 'COLUMN', @level2name = N'ConfirmAlarm'
GO
IF ((SELECT COUNT(*) from fn_listextendedproperty('MS_Description', 
'SCHEMA', N'dbo', 
'TABLE', N'TB_MeasurePoint_copy1', 
'COLUMN', N'flowset')) > 0) 
EXEC sp_updateextendedproperty @name = N'MS_Description', @value = N'流量和水位设定(泵站专用)'
, @level0type = 'SCHEMA', @level0name = N'dbo'
, @level1type = 'TABLE', @level1name = N'TB_MeasurePoint_copy1'
, @level2type = 'COLUMN', @level2name = N'flowset'
ELSE
EXEC sp_addextendedproperty @name = N'MS_Description', @value = N'流量和水位设定(泵站专用)'
, @level0type = 'SCHEMA', @level0name = N'dbo'
, @level1type = 'TABLE', @level1name = N'TB_MeasurePoint_copy1'
, @level2type = 'COLUMN', @level2name = N'flowset'
GO
IF ((SELECT COUNT(*) from fn_listextendedproperty('MS_Description', 
'SCHEMA', N'dbo', 
'TABLE', N'TB_MeasurePoint_copy1', 
'COLUMN', N'triggerCycle')) > 0) 
EXEC sp_updateextendedproperty @name = N'MS_Description', @value = N'是否监视变化周期'
, @level0type = 'SCHEMA', @level0name = N'dbo'
, @level1type = 'TABLE', @level1name = N'TB_MeasurePoint_copy1'
, @level2type = 'COLUMN', @level2name = N'triggerCycle'
ELSE
EXEC sp_addextendedproperty @name = N'MS_Description', @value = N'是否监视变化周期'
, @level0type = 'SCHEMA', @level0name = N'dbo'
, @level1type = 'TABLE', @level1name = N'TB_MeasurePoint_copy1'
, @level2type = 'COLUMN', @level2name = N'triggerCycle'
GO
IF ((SELECT COUNT(*) from fn_listextendedproperty('MS_Description', 
'SCHEMA', N'dbo', 
'TABLE', N'TB_MeasurePoint_copy1', 
'COLUMN', N'cyclemax')) > 0) 
EXEC sp_updateextendedproperty @name = N'MS_Description', @value = N'不变周期上限'
, @level0type = 'SCHEMA', @level0name = N'dbo'
, @level1type = 'TABLE', @level1name = N'TB_MeasurePoint_copy1'
, @level2type = 'COLUMN', @level2name = N'cyclemax'
ELSE
EXEC sp_addextendedproperty @name = N'MS_Description', @value = N'不变周期上限'
, @level0type = 'SCHEMA', @level0name = N'dbo'
, @level1type = 'TABLE', @level1name = N'TB_MeasurePoint_copy1'
, @level2type = 'COLUMN', @level2name = N'cyclemax'
GO
IF ((SELECT COUNT(*) from fn_listextendedproperty('MS_Description', 
'SCHEMA', N'dbo', 
'TABLE', N'TB_MeasurePoint_copy1', 
'COLUMN', N'cyclemin')) > 0) 
EXEC sp_updateextendedproperty @name = N'MS_Description', @value = N'不变周期下限'
, @level0type = 'SCHEMA', @level0name = N'dbo'
, @level1type = 'TABLE', @level1name = N'TB_MeasurePoint_copy1'
, @level2type = 'COLUMN', @level2name = N'cyclemin'
ELSE
EXEC sp_addextendedproperty @name = N'MS_Description', @value = N'不变周期下限'
, @level0type = 'SCHEMA', @level0name = N'dbo'
, @level1type = 'TABLE', @level1name = N'TB_MeasurePoint_copy1'
, @level2type = 'COLUMN', @level2name = N'cyclemin'
GO
IF ((SELECT COUNT(*) from fn_listextendedproperty('MS_Description', 
'SCHEMA', N'dbo', 
'TABLE', N'TB_MeasurePoint_copy1', 
'COLUMN', N'triggerMutation')) > 0) 
EXEC sp_updateextendedproperty @name = N'MS_Description', @value = N'是否监视突变情况'
, @level0type = 'SCHEMA', @level0name = N'dbo'
, @level1type = 'TABLE', @level1name = N'TB_MeasurePoint_copy1'
, @level2type = 'COLUMN', @level2name = N'triggerMutation'
ELSE
EXEC sp_addextendedproperty @name = N'MS_Description', @value = N'是否监视突变情况'
, @level0type = 'SCHEMA', @level0name = N'dbo'
, @level1type = 'TABLE', @level1name = N'TB_MeasurePoint_copy1'
, @level2type = 'COLUMN', @level2name = N'triggerMutation'
GO
IF ((SELECT COUNT(*) from fn_listextendedproperty('MS_Description', 
'SCHEMA', N'dbo', 
'TABLE', N'TB_MeasurePoint_copy1', 
'COLUMN', N'mutationset')) > 0) 
EXEC sp_updateextendedproperty @name = N'MS_Description', @value = N'突变超限值设定'
, @level0type = 'SCHEMA', @level0name = N'dbo'
, @level1type = 'TABLE', @level1name = N'TB_MeasurePoint_copy1'
, @level2type = 'COLUMN', @level2name = N'mutationset'
ELSE
EXEC sp_addextendedproperty @name = N'MS_Description', @value = N'突变超限值设定'
, @level0type = 'SCHEMA', @level0name = N'dbo'
, @level1type = 'TABLE', @level1name = N'TB_MeasurePoint_copy1'
, @level2type = 'COLUMN', @level2name = N'mutationset'
GO
IF ((SELECT COUNT(*) from fn_listextendedproperty('MS_Description', 
'SCHEMA', N'dbo', 
'TABLE', N'TB_MeasurePoint_copy1', 
'COLUMN', N'causeset')) > 0) 
EXEC sp_updateextendedproperty @name = N'MS_Description', @value = N'未处理初判间隔设定'
, @level0type = 'SCHEMA', @level0name = N'dbo'
, @level1type = 'TABLE', @level1name = N'TB_MeasurePoint_copy1'
, @level2type = 'COLUMN', @level2name = N'causeset'
ELSE
EXEC sp_addextendedproperty @name = N'MS_Description', @value = N'未处理初判间隔设定'
, @level0type = 'SCHEMA', @level0name = N'dbo'
, @level1type = 'TABLE', @level1name = N'TB_MeasurePoint_copy1'
, @level2type = 'COLUMN', @level2name = N'causeset'
GO
IF ((SELECT COUNT(*) from fn_listextendedproperty('MS_Description', 
'SCHEMA', N'dbo', 
'TABLE', N'TB_MeasurePoint_copy1', 
'COLUMN', N'operateset')) > 0) 
EXEC sp_updateextendedproperty @name = N'MS_Description', @value = N'未处理解决间隔设定'
, @level0type = 'SCHEMA', @level0name = N'dbo'
, @level1type = 'TABLE', @level1name = N'TB_MeasurePoint_copy1'
, @level2type = 'COLUMN', @level2name = N'operateset'
ELSE
EXEC sp_addextendedproperty @name = N'MS_Description', @value = N'未处理解决间隔设定'
, @level0type = 'SCHEMA', @level0name = N'dbo'
, @level1type = 'TABLE', @level1name = N'TB_MeasurePoint_copy1'
, @level2type = 'COLUMN', @level2name = N'operateset'
GO
IF ((SELECT COUNT(*) from fn_listextendedproperty('MS_Description', 
'SCHEMA', N'dbo', 
'TABLE', N'TB_MeasurePoint_copy1', 
'COLUMN', N'resultset')) > 0) 
EXEC sp_updateextendedproperty @name = N'MS_Description', @value = N'未处理结果间隔设定'
, @level0type = 'SCHEMA', @level0name = N'dbo'
, @level1type = 'TABLE', @level1name = N'TB_MeasurePoint_copy1'
, @level2type = 'COLUMN', @level2name = N'resultset'
ELSE
EXEC sp_addextendedproperty @name = N'MS_Description', @value = N'未处理结果间隔设定'
, @level0type = 'SCHEMA', @level0name = N'dbo'
, @level1type = 'TABLE', @level1name = N'TB_MeasurePoint_copy1'
, @level2type = 'COLUMN', @level2name = N'resultset'
GO
IF ((SELECT COUNT(*) from fn_listextendedproperty('MS_Description', 
'SCHEMA', N'dbo', 
'TABLE', N'TB_MeasurePoint_copy1', 
'COLUMN', N'triggerEquOff')) > 0) 
EXEC sp_updateextendedproperty @name = N'MS_Description', @value = N'是否触发设备停机报警'
, @level0type = 'SCHEMA', @level0name = N'dbo'
, @level1type = 'TABLE', @level1name = N'TB_MeasurePoint_copy1'
, @level2type = 'COLUMN', @level2name = N'triggerEquOff'
ELSE
EXEC sp_addextendedproperty @name = N'MS_Description', @value = N'是否触发设备停机报警'
, @level0type = 'SCHEMA', @level0name = N'dbo'
, @level1type = 'TABLE', @level1name = N'TB_MeasurePoint_copy1'
, @level2type = 'COLUMN', @level2name = N'triggerEquOff'
GO
IF ((SELECT COUNT(*) from fn_listextendedproperty('MS_Description', 
'SCHEMA', N'dbo', 
'TABLE', N'TB_MeasurePoint_copy1', 
'COLUMN', N'mathop')) > 0) 
EXEC sp_updateextendedproperty @name = N'MS_Description', @value = N'运算操作符,默认为乘法(=-*/)'
, @level0type = 'SCHEMA', @level0name = N'dbo'
, @level1type = 'TABLE', @level1name = N'TB_MeasurePoint_copy1'
, @level2type = 'COLUMN', @level2name = N'mathop'
ELSE
EXEC sp_addextendedproperty @name = N'MS_Description', @value = N'运算操作符,默认为乘法(=-*/)'
, @level0type = 'SCHEMA', @level0name = N'dbo'
, @level1type = 'TABLE', @level1name = N'TB_MeasurePoint_copy1'
, @level2type = 'COLUMN', @level2name = N'mathop'
GO
IF ((SELECT COUNT(*) from fn_listextendedproperty('MS_Description', 
'SCHEMA', N'dbo', 
'TABLE', N'TB_MeasurePoint_copy1', 
'COLUMN', N'active')) > 0) 
EXEC sp_updateextendedproperty @name = N'MS_Description', @value = N'是否在历史数据中显示'
, @level0type = 'SCHEMA', @level0name = N'dbo'
, @level1type = 'TABLE', @level1name = N'TB_MeasurePoint_copy1'
, @level2type = 'COLUMN', @level2name = N'active'
ELSE
EXEC sp_addextendedproperty @name = N'MS_Description', @value = N'是否在历史数据中显示'
, @level0type = 'SCHEMA', @level0name = N'dbo'
, @level1type = 'TABLE', @level1name = N'TB_MeasurePoint_copy1'
, @level2type = 'COLUMN', @level2name = N'active'
GO
IF ((SELECT COUNT(*) from fn_listextendedproperty('MS_Description', 
'SCHEMA', N'dbo', 
'TABLE', N'TB_MeasurePoint_copy1', 
'COLUMN', N'SoundAlarm')) > 0) 
EXEC sp_updateextendedproperty @name = N'MS_Description', @value = N'是否触发声音报警'
, @level0type = 'SCHEMA', @level0name = N'dbo'
, @level1type = 'TABLE', @level1name = N'TB_MeasurePoint_copy1'
, @level2type = 'COLUMN', @level2name = N'SoundAlarm'
ELSE
EXEC sp_addextendedproperty @name = N'MS_Description', @value = N'是否触发声音报警'
, @level0type = 'SCHEMA', @level0name = N'dbo'
, @level1type = 'TABLE', @level1name = N'TB_MeasurePoint_copy1'
, @level2type = 'COLUMN', @level2name = N'SoundAlarm'
GO
IF ((SELECT COUNT(*) from fn_listextendedproperty('MS_Description', 
'SCHEMA', N'dbo', 
'TABLE', N'TB_MeasurePoint_copy1', 
'COLUMN', N'scdtype')) > 0) 
EXEC sp_updateextendedproperty @name = N'MS_Description', @value = N'区域类别'
, @level0type = 'SCHEMA', @level0name = N'dbo'
, @level1type = 'TABLE', @level1name = N'TB_MeasurePoint_copy1'
, @level2type = 'COLUMN', @level2name = N'scdtype'
ELSE
EXEC sp_addextendedproperty @name = N'MS_Description', @value = N'区域类别'
, @level0type = 'SCHEMA', @level0name = N'dbo'
, @level1type = 'TABLE', @level1name = N'TB_MeasurePoint_copy1'
, @level2type = 'COLUMN', @level2name = N'scdtype'
GO
IF ((SELECT COUNT(*) from fn_listextendedproperty('MS_Description', 
'SCHEMA', N'dbo', 
'TABLE', N'TB_MeasurePoint_copy1', 
'COLUMN', N'spanrange')) > 0) 
EXEC sp_updateextendedproperty @name = N'MS_Description', @value = N'量程最大值'
, @level0type = 'SCHEMA', @level0name = N'dbo'
, @level1type = 'TABLE', @level1name = N'TB_MeasurePoint_copy1'
, @level2type = 'COLUMN', @level2name = N'spanrange'
ELSE
EXEC sp_addextendedproperty @name = N'MS_Description', @value = N'量程最大值'
, @level0type = 'SCHEMA', @level0name = N'dbo'
, @level1type = 'TABLE', @level1name = N'TB_MeasurePoint_copy1'
, @level2type = 'COLUMN', @level2name = N'spanrange'
GO
IF ((SELECT COUNT(*) from fn_listextendedproperty('MS_Description', 
'SCHEMA', N'dbo', 
'TABLE', N'TB_MeasurePoint_copy1', 
'COLUMN', N'disname')) > 0) 
EXEC sp_updateextendedproperty @name = N'MS_Description', @value = N'测量点简称'
, @level0type = 'SCHEMA', @level0name = N'dbo'
, @level1type = 'TABLE', @level1name = N'TB_MeasurePoint_copy1'
, @level2type = 'COLUMN', @level2name = N'disname'
ELSE
EXEC sp_addextendedproperty @name = N'MS_Description', @value = N'测量点简称'
, @level0type = 'SCHEMA', @level0name = N'dbo'
, @level1type = 'TABLE', @level1name = N'TB_MeasurePoint_copy1'
, @level2type = 'COLUMN', @level2name = N'disname'
GO

-- ----------------------------
-- Records of TB_MeasurePoint_copy1
-- ----------------------------

-- ----------------------------
-- Table structure for TB_MeasurePoint_Cycle
-- ----------------------------
DROP TABLE [dbo].[TB_MeasurePoint_Cycle]
GO
CREATE TABLE [dbo].[TB_MeasurePoint_Cycle] (
[id] bigint NOT NULL IDENTITY(1,1) ,
[mpid] varchar(50) NULL ,
[cyclevalue] money NULL 
)


GO
DBCC CHECKIDENT(N'[dbo].[TB_MeasurePoint_Cycle]', RESEED, 130090)
GO

-- ----------------------------
-- Records of TB_MeasurePoint_Cycle
-- ----------------------------
SET IDENTITY_INSERT [dbo].[TB_MeasurePoint_Cycle] ON
GO
SET IDENTITY_INSERT [dbo].[TB_MeasurePoint_Cycle] OFF
GO

-- ----------------------------
-- Table structure for TB_MeasurePoint_EquOff
-- ----------------------------
DROP TABLE [dbo].[TB_MeasurePoint_EquOff]
GO
CREATE TABLE [dbo].[TB_MeasurePoint_EquOff] (
[id] bigint NOT NULL IDENTITY(1,1) ,
[mpid] varchar(50) NULL ,
[ConfirmAlarm] char(1) NULL 
)


GO

-- ----------------------------
-- Records of TB_MeasurePoint_EquOff
-- ----------------------------
SET IDENTITY_INSERT [dbo].[TB_MeasurePoint_EquOff] ON
GO
SET IDENTITY_INSERT [dbo].[TB_MeasurePoint_EquOff] OFF
GO

-- ----------------------------
-- Table structure for TB_MeasurePoint_Mode
-- ----------------------------
DROP TABLE [dbo].[TB_MeasurePoint_Mode]
GO
CREATE TABLE [dbo].[TB_MeasurePoint_Mode] (
[id] varchar(50) NULL ,
[insdt] datetime NULL ,
[insuser] varchar(50) NULL ,
[mode] varchar(50) NULL ,
[mpid] varchar(50) NULL ,
[pid] varchar(50) NULL 
)


GO

-- ----------------------------
-- Records of TB_MeasurePoint_Mode
-- ----------------------------

-- ----------------------------
-- Table structure for TB_MeasurePoint_Prop
-- ----------------------------
DROP TABLE [dbo].[TB_MeasurePoint_Prop]
GO
CREATE TABLE [dbo].[TB_MeasurePoint_Prop] (
[id] varchar(50) NULL ,
[insdt] datetime NULL ,
[insuser] varchar(50) NULL ,
[project] varchar(50) NULL ,
[index_evaluation] varchar(50) NULL ,
[evaluation_formula] varchar(MAX) NULL ,
[biz_id] varchar(50) NULL ,
[pid] varchar(50) NULL ,
[calFreq] varchar(50) NULL ,
[calRange] varchar(50) NULL ,
[unit_id] varchar(50) NULL 
)


GO

-- ----------------------------
-- Records of TB_MeasurePoint_Prop
-- ----------------------------

-- ----------------------------
-- Table structure for TB_MeasurePoint_PropSource
-- ----------------------------
DROP TABLE [dbo].[TB_MeasurePoint_PropSource]
GO
CREATE TABLE [dbo].[TB_MeasurePoint_PropSource] (
[id] varchar(50) NOT NULL ,
[insdt] datetime NULL ,
[insuser] varchar(50) NULL ,
[pid] varchar(50) NULL ,
[index_details] varchar(50) NULL ,
[value] float(53) NULL ,
[evaluation_rules] varchar(50) NULL ,
[cal_range] int NULL ,
[cal_range_unit] varchar(10) NULL ,
[target_value] float(53) NULL ,
[mpid] varchar(50) NULL ,
[mpname] varchar(50) NULL ,
[value_type] varchar(50) NULL ,
[calculation_type] varchar(50) NULL ,
[score] float(53) NULL ,
[fixed_deduction_value] float(53) NULL ,
[tb_name] varchar(50) NULL ,
[tb_where] varchar(50) NULL ,
[tb_key_name] varchar(50) NULL 
)


GO

-- ----------------------------
-- Records of TB_MeasurePoint_PropSource
-- ----------------------------

-- ----------------------------
-- Table structure for TB_MemoType
-- ----------------------------
DROP TABLE [dbo].[TB_MemoType]
GO
CREATE TABLE [dbo].[TB_MemoType] (
[id] bigint NOT NULL IDENTITY(1,1) ,
[memotypename] varchar(100) NULL ,
[remark] varchar(800) NULL ,
[color] varchar(50) NULL ,
[st] varchar(50) NULL 
)


GO
DBCC CHECKIDENT(N'[dbo].[TB_MemoType]', RESEED, 10)
GO

-- ----------------------------
-- Records of TB_MemoType
-- ----------------------------
SET IDENTITY_INSERT [dbo].[TB_MemoType] ON
GO
SET IDENTITY_INSERT [dbo].[TB_MemoType] OFF
GO

-- ----------------------------
-- Table structure for TB_MemoTypeItem
-- ----------------------------
DROP TABLE [dbo].[TB_MemoTypeItem]
GO
CREATE TABLE [dbo].[TB_MemoTypeItem] (
[id] bigint NOT NULL IDENTITY(1,1) ,
[fid] bigint NULL ,
[memo] varchar(500) NULL 
)


GO
DBCC CHECKIDENT(N'[dbo].[TB_MemoTypeItem]', RESEED, 20)
GO

-- ----------------------------
-- Records of TB_MemoTypeItem
-- ----------------------------
SET IDENTITY_INSERT [dbo].[TB_MemoTypeItem] ON
GO
SET IDENTITY_INSERT [dbo].[TB_MemoTypeItem] OFF
GO

-- ----------------------------
-- Table structure for tb_model_cal_info
-- ----------------------------
DROP TABLE [dbo].[tb_model_cal_info]
GO
CREATE TABLE [dbo].[tb_model_cal_info] (
[id] int NOT NULL IDENTITY(1,1) ,
[insdt] datetime NULL ,
[model_name] varchar(50) NULL ,
[model_param] varchar(100) NULL ,
[cal_result] varchar(100) NULL 
)


GO
DBCC CHECKIDENT(N'[dbo].[tb_model_cal_info]', RESEED, 1891)
GO

-- ----------------------------
-- Records of tb_model_cal_info
-- ----------------------------
SET IDENTITY_INSERT [dbo].[tb_model_cal_info] ON
GO
SET IDENTITY_INSERT [dbo].[tb_model_cal_info] OFF
GO

-- ----------------------------
-- Table structure for tb_PersonalMpCollection
-- ----------------------------
DROP TABLE [dbo].[tb_PersonalMpCollection]
GO
CREATE TABLE [dbo].[tb_PersonalMpCollection] (
[id] varchar(50) NOT NULL ,
[userid] varchar(50) NULL ,
[mpids] varchar(50) NULL ,
[unit_id] varchar(50) NULL 
)


GO

-- ----------------------------
-- Records of tb_PersonalMpCollection
-- ----------------------------

-- ----------------------------
-- Table structure for TB_PRI
-- ----------------------------
DROP TABLE [dbo].[TB_PRI]
GO
CREATE TABLE [dbo].[TB_PRI] (
[id] bigint NOT NULL IDENTITY(1,1) ,
[name] varchar(50) NULL ,
[memo] varchar(100) NULL ,
[tag] int NULL 
)


GO
DBCC CHECKIDENT(N'[dbo].[TB_PRI]', RESEED, 78)
GO

-- ----------------------------
-- Records of TB_PRI
-- ----------------------------
SET IDENTITY_INSERT [dbo].[TB_PRI] ON
GO
SET IDENTITY_INSERT [dbo].[TB_PRI] OFF
GO

-- ----------------------------
-- Table structure for TB_PRO_ALARM
-- ----------------------------
DROP TABLE [dbo].[TB_PRO_ALARM]
GO
CREATE TABLE [dbo].[TB_PRO_ALARM] (
[id] bigint NOT NULL IDENTITY(1,1) ,
[mpid] varchar(50) NOT NULL ,
[parmvalue] varchar(50) NOT NULL ,
[faultdate] datetime NOT NULL ,
[recovervalue] varchar(50) NULL ,
[recoverdate] datetime NULL ,
[alarmtype] varchar(50) NOT NULL ,
[alarmvalue] varchar(50) NOT NULL ,
[status] varchar(50) NOT NULL DEFAULT ('N') ,
[insdt] datetime NULL DEFAULT (getdate()) ,
[insuser] varchar(50) NULL DEFAULT ('system') ,
[cause] varchar(500) NULL ,
[causedt] datetime NULL ,
[causeuser] varchar(50) NULL ,
[operate] varchar(500) NULL ,
[operatedt] datetime NULL ,
[operateuser] varchar(50) NULL ,
[result] varchar(500) NULL ,
[resultdt] datetime NULL ,
[resultuser] varchar(50) NULL ,
[causeid] varchar(50) NULL ,
[display] varchar(500) NULL ,
[ConfirmAlarm] char(1) NULL DEFAULT ('0') 
)


GO
DBCC CHECKIDENT(N'[dbo].[TB_PRO_ALARM]', RESEED, 66)
GO
IF ((SELECT COUNT(*) from fn_listextendedproperty('MS_Description', 
'SCHEMA', N'dbo', 
'TABLE', N'TB_PRO_ALARM', 
'COLUMN', N'ConfirmAlarm')) > 0) 
EXEC sp_updateextendedproperty @name = N'MS_Description', @value = N'报警确认(0未确认,1已确认)'
, @level0type = 'SCHEMA', @level0name = N'dbo'
, @level1type = 'TABLE', @level1name = N'TB_PRO_ALARM'
, @level2type = 'COLUMN', @level2name = N'ConfirmAlarm'
ELSE
EXEC sp_addextendedproperty @name = N'MS_Description', @value = N'报警确认(0未确认,1已确认)'
, @level0type = 'SCHEMA', @level0name = N'dbo'
, @level1type = 'TABLE', @level1name = N'TB_PRO_ALARM'
, @level2type = 'COLUMN', @level2name = N'ConfirmAlarm'
GO

-- ----------------------------
-- Records of TB_PRO_ALARM
-- ----------------------------
SET IDENTITY_INSERT [dbo].[TB_PRO_ALARM] ON
GO
SET IDENTITY_INSERT [dbo].[TB_PRO_ALARM] OFF
GO

-- ----------------------------
-- Table structure for tb_reportcomment
-- ----------------------------
DROP TABLE [dbo].[tb_reportcomment]
GO
CREATE TABLE [dbo].[tb_reportcomment] (
[id] bigint NOT NULL IDENTITY(1,1) ,
[dt] datetime NULL ,
[comment] varchar(500) NULL ,
[insdt] datetime NULL DEFAULT (getdate()) 
)


GO
DBCC CHECKIDENT(N'[dbo].[tb_reportcomment]', RESEED, 21)
GO

-- ----------------------------
-- Records of tb_reportcomment
-- ----------------------------
SET IDENTITY_INSERT [dbo].[tb_reportcomment] ON
GO
SET IDENTITY_INSERT [dbo].[tb_reportcomment] OFF
GO

-- ----------------------------
-- Table structure for TB_RTConsole
-- ----------------------------
DROP TABLE [dbo].[TB_RTConsole]
GO
CREATE TABLE [dbo].[TB_RTConsole] (
[ConsoleID] varchar(10) NOT NULL ,
[ConsoleCode] varchar(50) NOT NULL ,
[Name] varchar(50) NOT NULL ,
[Parent] varchar(10) NULL ,
[OrderID] int NULL ,
[BizTypeID] varchar(10) NULL ,
[Icon] varchar(20) NULL ,
[Memo] varchar(100) NULL ,
[ConsoleLink] varchar(100) NOT NULL ,
[morder] int NULL DEFAULT ((99)) ,
[active] varchar(50) NULL ,
[diswidth] int NULL 
)


GO

-- ----------------------------
-- Records of TB_RTConsole
-- ----------------------------

-- ----------------------------
-- Table structure for TB_RTConsoleCamera
-- ----------------------------
DROP TABLE [dbo].[TB_RTConsoleCamera]
GO
CREATE TABLE [dbo].[TB_RTConsoleCamera] (
[ConsoleID] varchar(10) NOT NULL ,
[CameraID] varchar(50) NOT NULL ,
[ControlType] varchar(10) NULL ,
[EmpID] varchar(10) NULL ,
[XPos] int NOT NULL ,
[YPos] int NOT NULL ,
[Width] int NOT NULL ,
[Height] int NOT NULL ,
[Memo] varchar(100) NULL ,
[Style] varchar(50) NULL ,
[Color_Font] int NULL ,
[Color_Back] int NULL ,
[IsShowName] char(1) NULL ,
[IsShowUnit] char(1) NULL ,
[ControlName] varchar(50) NULL 
)


GO

-- ----------------------------
-- Records of TB_RTConsoleCamera
-- ----------------------------

-- ----------------------------
-- Table structure for TB_RTConsoleDItem
-- ----------------------------
DROP TABLE [dbo].[TB_RTConsoleDItem]
GO
CREATE TABLE [dbo].[TB_RTConsoleDItem] (
[ConsoleID] varchar(10) NOT NULL ,
[MPointID] varchar(50) NOT NULL ,
[ControlType] varchar(10) NULL ,
[EmpID] varchar(10) NULL ,
[XPos] int NOT NULL ,
[YPos] int NOT NULL ,
[Width] int NOT NULL ,
[Height] int NOT NULL ,
[Memo] varchar(100) NULL ,
[Style] varchar(50) NULL ,
[Color_Font] int NULL ,
[Color_Back] int NULL ,
[IsShowName] char(1) NULL ,
[IsShowUnit] char(1) NULL ,
[ControlName] varchar(50) NULL 
)


GO

-- ----------------------------
-- Records of TB_RTConsoleDItem
-- ----------------------------

-- ----------------------------
-- Table structure for TB_RTConsoleTxt
-- ----------------------------
DROP TABLE [dbo].[TB_RTConsoleTxt]
GO
CREATE TABLE [dbo].[TB_RTConsoleTxt] (
[ConsoleID] varchar(10) NOT NULL ,
[TxtID] varchar(50) NOT NULL ,
[ControlType] varchar(10) NULL ,
[EmpID] varchar(10) NULL ,
[XPos] int NOT NULL ,
[YPos] int NOT NULL ,
[Width] int NOT NULL ,
[Height] int NOT NULL ,
[Memo] varchar(100) NULL ,
[Style] varchar(50) NULL ,
[Color_Font] int NULL ,
[Color_Back] int NULL ,
[IsShowName] char(1) NULL ,
[IsShowUnit] char(1) NULL ,
[ControlName] varchar(50) NULL 
)


GO

-- ----------------------------
-- Records of TB_RTConsoleTxt
-- ----------------------------

-- ----------------------------
-- Table structure for tb_scada_alarm
-- ----------------------------
DROP TABLE [dbo].[tb_scada_alarm]
GO
CREATE TABLE [dbo].[tb_scada_alarm] (
[id] bigint NOT NULL IDENTITY(1,1) ,
[insdt] datetime NULL ,
[confirm_time] datetime NULL ,
[recover_time] datetime NULL ,
[recover_value] money NULL ,
[mpoint_code] varchar(50) NULL ,
[mpoint_name] varchar(50) NULL ,
[status] char(1) NULL ,
[alarm_level] int NULL ,
[process_section_code] varchar(50) NULL ,
[process_section_name] varchar(50) NULL ,
[patrol_point_id] varchar(50) NULL ,
[biz_id] varchar(50) NULL ,
[describe] varchar(1000) NULL ,
[alarm_type_lv1] varchar(10) NULL ,
[alarm_type_lv2] varchar(10) NULL ,
[original_status] varchar(255) NULL 
)


GO
DBCC CHECKIDENT(N'[dbo].[tb_scada_alarm]', RESEED, 844174)
GO

-- ----------------------------
-- Records of tb_scada_alarm
-- ----------------------------
SET IDENTITY_INSERT [dbo].[tb_scada_alarm] ON
GO
SET IDENTITY_INSERT [dbo].[tb_scada_alarm] OFF
GO

-- ----------------------------
-- Table structure for TB_User
-- ----------------------------
DROP TABLE [dbo].[TB_User]
GO
CREATE TABLE [dbo].[TB_User] (
[id] varchar(50) NULL ,
[name] varchar(10) NULL ,
[password] varchar(100) NULL ,
[active] char(1) NULL ,
[caption] varchar(100) NULL ,
[pri] int NULL 
)


GO

-- ----------------------------
-- Records of TB_User
-- ----------------------------

-- ----------------------------
-- View structure for TB_upload
-- ----------------------------
DROP VIEW [dbo].[TB_upload]
GO
CREATE VIEW [dbo].[TB_upload] AS 
SELECT   dbo.TB_MeasurePoint.MPointID, dbo.TB_MeasurePoint.MPointCode, dbo.A_UPLOAD.mpointcode AS Expr1, 
                dbo.A_UPLOAD.scdtype, dbo.A_UPLOAD.upload, dbo.A_UPLOAD.parmname, dbo.TB_MeasurePoint.MeasureDT, 
                dbo.TB_MeasurePoint.ParmValue, dbo.TB_MeasurePoint.SignalType, dbo.TB_MeasurePoint.BizType
FROM      dbo.TB_MeasurePoint INNER JOIN
                dbo.A_UPLOAD ON dbo.TB_MeasurePoint.MPointCode = dbo.A_UPLOAD.mpointcode
GO

-- ----------------------------
-- View structure for UV_ALARM
-- ----------------------------
DROP VIEW [dbo].[UV_ALARM]
GO
CREATE VIEW [dbo].[UV_ALARM] AS 
SELECT   TOP (500) TagName AS mpid, CAST(AlarmValue AS varchar(100)) AS parmvalue, CAST(LimitValue AS varchar(100)) 
                AS alarmvalue, DATEADD(hour, 8, AlarmTime) AS faultdate, CAST(AlarmType AS varchar(100)) AS AlarmType, 
                Pri AS alarmlvl, EventType AS ConfirmAlarm, CAST(ExtendField8 AS varchar(100)) AS bizid, 
                CAST(ExtendField7 AS varchar(100)) AS equcardid, CAST(AlarmText AS varchar(100)) AS describe, 
                CAST(ExtendField6 AS varchar(100)) AS limitHH, CAST(ExtendField5 AS varchar(100)) AS limitLL, DATEADD(hour, 8, 
                EventTime) AS insdt
FROM      dbo.Alarm AS Alarm_1
WHERE   (EventType IN ('报警', '报警恢复')) AND (IOServerName = 'SERVER') AND (TagName IN
                    (SELECT   SUBSTRING(MPointID, 6, LEN(MPointID)) AS Expr1
                     FROM      dbo.TB_MeasurePoint)) AND (DATEADD(hour, 8, EventTime) > DATEADD(hh, - 4, GETDATE()))
GO

-- ----------------------------
-- View structure for UV_FQ_F
-- ----------------------------
DROP VIEW [dbo].[UV_FQ_F]
GO
CREATE VIEW [dbo].[UV_FQ_F] AS 
SELECT   MPointCode, ParmName, ParmValue, MeasureDT
FROM      dbo.TB_MeasurePoint
WHERE   (BizType = '污水FQ')
GO

-- ----------------------------
-- Procedure structure for altertable
-- ----------------------------
DROP PROCEDURE [dbo].[altertable]
GO



CREATE PROCEDURE [dbo].[altertable]
 as 
declare @mpoincode as varchar(800)
Declare @strSQL nvarchar(4000)

declare tc cursor for select mpointcode from TB_MeasurePoint
open tc
FETCH NEXT FROM tc INTO @mpoincode

WHILE @@FETCH_STATUS = 0
begin
	
	SET @strSQL = N''
	SET @strSQL = @strSQL + N'alter TABLE tb_mp_'+@mpoincode+N' add memotype varchar(100) '
	print @strSQL
	print 'go'
	SET @strSQL = N''
	SET @strSQL = @strSQL + N'alter TABLE tb_mp_'+@mpoincode+N' add memo varchar(500) '
	print @strSQL
	print 'go'
	FETCH NEXT FROM tc INTO @mpoincode
	
end 
CLOSE tc
DEALLOCATE tc


GO

-- ----------------------------
-- Procedure structure for cleardirty
-- ----------------------------
DROP PROCEDURE [dbo].[cleardirty]
GO



CREATE PROCEDURE [dbo].[cleardirty]  AS
declare @tablename as varchar(800)
Declare @strSQL nvarchar(4000)

declare tc cursor for select [name] from sysobjects where xtype='u' and [name] like '%TB_MP_%'
open tc
FETCH NEXT FROM tc INTO @tablename

WHILE @@FETCH_STATUS = 0
begin
	
	SET @strSQL = N''
	SET @strSQL = @strSQL + N'delete from '+@tablename+N' where measuredt between ''2021-01-25 22:30:21'' and ''2021-02-02 22:30:21'''
	print @strSQL
	FETCH NEXT FROM tc INTO @tablename
	exec sp_executesql @strsql
end 
CLOSE tc
DEALLOCATE tc


GO

-- ----------------------------
-- Procedure structure for datagen
-- ----------------------------
DROP PROCEDURE [dbo].[datagen]
GO



CREATE PROCEDURE [dbo].[datagen]
@stime datetime,@etime datetime,@tablename varchar(50),@hivalue float,@lovalue float,@diff int
as
Declare @str varchar(8000)
Declare @tempvalue float
Declare @tempdate datetime
declare @count float
set @count=0
set @tempdate = @stime
set @str = 'delete from '+@tablename
print @str
EXECUTE(@str)	
while @tempdate<=@etime
begin
	set @count = @count+1
	set @tempvalue = @lovalue + (@hivalue-@lovalue)*RAND()
	
	SET @str = 'insert into ' + @tablename+'(ParmValue,MeasureDT) values('+cast(@tempvalue as varchar(12))+','''+cast(@tempdate as varchar(20))+''')'
	print @str
	EXECUTE(@str)
	set @tempdate = dateadd(minute,@diff,@tempdate)
end


GO

-- ----------------------------
-- Procedure structure for datasql
-- ----------------------------
DROP PROCEDURE [dbo].[datasql]
GO



CREATE PROCEDURE [dbo].[datasql]
--@stime datetime,@etime datetime,@tablename varchar(50),@hivalue float,@lovalue float,@diff int
@stime1 datetime,@etime1 datetime,@stime2 datetime,@etime2 datetime
as

Declare @sstime1 datetime
Declare @setime1 datetime
Declare @sstime2 datetime
Declare @setime2 datetime
Declare @count int
Declare @i int
--Declare @j int
--Declare @x int
--Declare @y int
Declare @ParmValue MONEY
Declare @ItemID int
Declare @msg varchar(255)


set @count=0
set @i=1
--set @j=0
--set @x=1
--set @y=1

set @sstime1 = @stime1
set @setime1 = @etime1

set @sstime2 = @stime2
set @setime2 = @etime2

--TB_MP_AO712_DO2
--TB_MP_AO712_DO5
--TB_MP_AO741_DO2
--TB_MP_AO741_DO5
--TB_MP_AO742_DO2
--TB_MP_AO742_DO5
--TB_MP_DO2
--TB_MP_DO5
--TB_MP_MLSS
--TB_MP_AO742_MLSS

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[TB_MP_TEMP]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[TB_MP_TEMP]


CREATE TABLE [dbo].[TB_MP_TEMP] (
	[ItemID]    [bigint] IDENTITY (1, 1) NOT NULL ,
	[ParmValue] [money] NOT NULL ,
	[MeasureDT] [datetime] NOT NULL
) ON [PRIMARY]

BEGIN TRANSACTION

select @count=count(*) from EIP_PRD_NJCN.dbo.TB_MP_AT1102_JSSS where MeasureDT >= @sstime1 and MeasureDT <= @setime1 --正确表

--select @j=count(*) from TB_MP_DO2 where MeasureDT >= @sstime2 and MeasureDT <= @setime2 --修改表


insert into TB_MP_TEMP (ParmValue,MeasureDT) select ParmValue,MeasureDT from EIP_PRD_NJCN.dbo.TB_MP_AT1102_JSSS where MeasureDT >= @sstime1 and MeasureDT <= @setime1


DECLARE curRunStatus cursor for 
select ItemID,ParmValue from EIP_PRD_NJCN.dbo.TB_MP_AT1102_JSSS where MeasureDT >= @sstime2 and MeasureDT <= @setime2
open curRunStatus

FETCH curRunStatus into @ItemID,@ParmValue

while(@i<=@count)
begin

select @ParmValue = ParmValue from TB_MP_TEMP where ItemID = @i

update EIP_PRD_NJCN.dbo.TB_MP_AT1102_JSSS set ParmValue=@ParmValue where ItemID=@ItemID

set @i=@i+1

FETCH curRunStatus into @ItemID,@ParmValue

end

close curRunStatus
DEALLOCATE curRunStatus




			
If @@ERROR <> 0
Begin
Set @Msg = '数据插入出错！'
Goto ErrorHandle
End
			

Select * from TB_MP_TEMP

ErrorHandle:

IF @msg IS null
    Commit transaction
ELSE
BEGIN
Rollback transaction
RAISERROR (@Msg, 16, 1)
END


GO

-- ----------------------------
-- Procedure structure for dt_addtosourcecontrol
-- ----------------------------
DROP PROCEDURE [dbo].[dt_addtosourcecontrol]
GO



CREATE PROCEDURE [dbo].[dt_addtosourcecontrol]
    @vchSourceSafeINI varchar(255) = '',
    @vchProjectName   varchar(255) ='',
    @vchComment       varchar(255) ='',
    @vchLoginName     varchar(255) ='',
    @vchPassword      varchar(255) =''

as

set nocount on

declare @iReturn int
declare @iObjectId int
select @iObjectId = 0

declare @iStreamObjectId int
select @iStreamObjectId = 0

declare @VSSGUID varchar(100)
select @VSSGUID = 'SQLVersionControl.VCS_SQL'

declare @vchDatabaseName varchar(255)
select @vchDatabaseName = db_name()

declare @iReturnValue int
select @iReturnValue = 0

declare @iPropertyObjectId int
declare @vchParentId varchar(255)

declare @iObjectCount int
select @iObjectCount = 0

    exec @iReturn = master.dbo.sp_OACreate @VSSGUID, @iObjectId OUT
    if @iReturn <> 0 GOTO E_OAError


    /* Create Project in SS */
    exec @iReturn = master.dbo.sp_OAMethod @iObjectId,
											'AddProjectToSourceSafe',
											NULL,
											@vchSourceSafeINI,
											@vchProjectName output,
											@@SERVERNAME,
											@vchDatabaseName,
											@vchLoginName,
											@vchPassword,
											@vchComment


    if @iReturn <> 0 GOTO E_OAError

    /* Set Database Properties */

    begin tran SetProperties

    /* add high level object */

    exec @iPropertyObjectId = dbo.dt_adduserobject_vcs 'VCSProjectID'

    select @vchParentId = CONVERT(varchar(255),@iPropertyObjectId)

    exec dbo.dt_setpropertybyid @iPropertyObjectId, 'VCSProjectID', @vchParentId , NULL
    exec dbo.dt_setpropertybyid @iPropertyObjectId, 'VCSProject' , @vchProjectName , NULL
    exec dbo.dt_setpropertybyid @iPropertyObjectId, 'VCSSourceSafeINI' , @vchSourceSafeINI , NULL
    exec dbo.dt_setpropertybyid @iPropertyObjectId, 'VCSSQLServer', @@SERVERNAME, NULL
    exec dbo.dt_setpropertybyid @iPropertyObjectId, 'VCSSQLDatabase', @vchDatabaseName, NULL

    if @@error <> 0 GOTO E_General_Error

    commit tran SetProperties
    
    select @iObjectCount = 0;

CleanUp:
    select @vchProjectName
    select @iObjectCount
    return

E_General_Error:
    /* this is an all or nothing.  No specific error messages */
    goto CleanUp

E_OAError:
    exec dbo.dt_displayoaerror @iObjectId, @iReturn
    goto CleanUp


GO

-- ----------------------------
-- Procedure structure for dt_addtosourcecontrol_u
-- ----------------------------
DROP PROCEDURE [dbo].[dt_addtosourcecontrol_u]
GO



CREATE PROCEDURE [dbo].[dt_addtosourcecontrol_u]
    @vchSourceSafeINI nvarchar(255) = '',
    @vchProjectName   nvarchar(255) ='',
    @vchComment       nvarchar(255) ='',
    @vchLoginName     nvarchar(255) ='',
    @vchPassword      nvarchar(255) =''

as
	-- This procedure should no longer be called;  dt_addtosourcecontrol should be called instead.
	-- Calls are forwarded to dt_addtosourcecontrol to maintain backward compatibility
	set nocount on
	exec dbo.dt_addtosourcecontrol 
		@vchSourceSafeINI, 
		@vchProjectName, 
		@vchComment, 
		@vchLoginName, 
		@vchPassword


GO

-- ----------------------------
-- Procedure structure for dt_adduserobject
-- ----------------------------
DROP PROCEDURE [dbo].[dt_adduserobject]
GO



CREATE PROCEDURE [dbo].[dt_adduserobject]
as
	set nocount on
	/*
	** Create the user object if it does not exist already
	*/
	begin transaction
		insert dbo.dtproperties (property) VALUES ('DtgSchemaOBJECT')
		update dbo.dtproperties set objectid=@@identity 
			where id=@@identity and property='DtgSchemaOBJECT'
	commit
	return @@identity


GO

-- ----------------------------
-- Procedure structure for dt_adduserobject_vcs
-- ----------------------------
DROP PROCEDURE [dbo].[dt_adduserobject_vcs]
GO



CREATE PROCEDURE [dbo].[dt_adduserobject_vcs]
    @vchProperty varchar(64)

as

set nocount on

declare @iReturn int
    /*
    ** Create the user object if it does not exist already
    */
    begin transaction
        select @iReturn = objectid from dbo.dtproperties where property = @vchProperty
        if @iReturn IS NULL
        begin
            insert dbo.dtproperties (property) VALUES (@vchProperty)
            update dbo.dtproperties set objectid=@@identity
                    where id=@@identity and property=@vchProperty
            select @iReturn = @@identity
        end
    commit
    return @iReturn


GO

-- ----------------------------
-- Procedure structure for dt_checkinobject
-- ----------------------------
DROP PROCEDURE [dbo].[dt_checkinobject]
GO



CREATE PROCEDURE [dbo].[dt_checkinobject]
    @chObjectType  char(4),
    @vchObjectName varchar(255),
    @vchComment    varchar(255)='',
    @vchLoginName  varchar(255),
    @vchPassword   varchar(255)='',
    @iVCSFlags     int = 0,
    @iActionFlag   int = 0,   /* 0 => AddFile, 1 => CheckIn */
    @txStream1     Text = '', /* drop stream   */ /* There is a bug that if items are NULL they do not pass to OLE servers */
    @txStream2     Text = '', /* create stream */
    @txStream3     Text = ''  /* grant stream  */


as

	set nocount on

	declare @iReturn int
	declare @iObjectId int
	select @iObjectId = 0
	declare @iStreamObjectId int

	declare @VSSGUID varchar(100)
	select @VSSGUID = 'SQLVersionControl.VCS_SQL'

	declare @iPropertyObjectId int
	select @iPropertyObjectId  = 0

    select @iPropertyObjectId = (select objectid from dbo.dtproperties where property = 'VCSProjectID')

    declare @vchProjectName   varchar(255)
    declare @vchSourceSafeINI varchar(255)
    declare @vchServerName    varchar(255)
    declare @vchDatabaseName  varchar(255)
    declare @iReturnValue	  int
    declare @pos			  int
    declare @vchProcLinePiece varchar(255)

    
    exec dbo.dt_getpropertiesbyid_vcs @iPropertyObjectId, 'VCSProject',       @vchProjectName   OUT
    exec dbo.dt_getpropertiesbyid_vcs @iPropertyObjectId, 'VCSSourceSafeINI', @vchSourceSafeINI OUT
    exec dbo.dt_getpropertiesbyid_vcs @iPropertyObjectId, 'VCSSQLServer',     @vchServerName    OUT
    exec dbo.dt_getpropertiesbyid_vcs @iPropertyObjectId, 'VCSSQLDatabase',   @vchDatabaseName  OUT

    if @chObjectType = 'PROC'
    begin
        if @iActionFlag = 1
        begin
            /* Procedure Can have up to three streams
            Drop Stream, Create Stream, GRANT stream */

            begin tran compile_all

            /* try to compile the streams */
            exec (@txStream1)
            if @@error <> 0 GOTO E_Compile_Fail

            exec (@txStream2)
            if @@error <> 0 GOTO E_Compile_Fail

            exec (@txStream3)
            if @@error <> 0 GOTO E_Compile_Fail
        end

        exec @iReturn = master.dbo.sp_OACreate @VSSGUID, @iObjectId OUT
        if @iReturn <> 0 GOTO E_OAError

        exec @iReturn = master.dbo.sp_OAGetProperty @iObjectId, 'GetStreamObject', @iStreamObjectId OUT
        if @iReturn <> 0 GOTO E_OAError
        
        if @iActionFlag = 1
        begin
            
            declare @iStreamLength int
			
			select @pos=1
			select @iStreamLength = datalength(@txStream2)
			
			if @iStreamLength > 0
			begin
			
				while @pos < @iStreamLength
				begin
						
					select @vchProcLinePiece = substring(@txStream2, @pos, 255)
					
					exec @iReturn = master.dbo.sp_OAMethod @iStreamObjectId, 'AddStream', @iReturnValue OUT, @vchProcLinePiece
            		if @iReturn <> 0 GOTO E_OAError
            		
					select @pos = @pos + 255
					
				end
            
				exec @iReturn = master.dbo.sp_OAMethod @iObjectId,
														'CheckIn_StoredProcedure',
														NULL,
														@sProjectName = @vchProjectName,
														@sSourceSafeINI = @vchSourceSafeINI,
														@sServerName = @vchServerName,
														@sDatabaseName = @vchDatabaseName,
														@sObjectName = @vchObjectName,
														@sComment = @vchComment,
														@sLoginName = @vchLoginName,
														@sPassword = [REDACTED]
														@iVCSFlags = @iVCSFlags,
														@iActionFlag = @iActionFlag,
														@sStream = ''
                                        
			end
        end
        else
        begin
        
            select colid, text into #ProcLines
            from syscomments
            where id = object_id(@vchObjectName)
            order by colid

            declare @iCurProcLine int
            declare @iProcLines int
            select @iCurProcLine = 1
            select @iProcLines = (select count(*) from #ProcLines)
            while @iCurProcLine <= @iProcLines
            begin
                select @pos = 1
                declare @iCurLineSize int
                select @iCurLineSize = len((select text from #ProcLines where colid = @iCurProcLine))
                while @pos <= @iCurLineSize
                begin                
                    select @vchProcLinePiece = convert(varchar(255),
                        substring((select text from #ProcLines where colid = @iCurProcLine),
                                  @pos, 255 ))
                    exec @iReturn = master.dbo.sp_OAMethod @iStreamObjectId, 'AddStream', @iReturnValue OUT, @vchProcLinePiece
                    if @iReturn <> 0 GOTO E_OAError
                    select @pos = @pos + 255                  
                end
                select @iCurProcLine = @iCurProcLine + 1
            end
            drop table #ProcLines

            exec @iReturn = master.dbo.sp_OAMethod @iObjectId,
													'CheckIn_StoredProcedure',
													NULL,
													@sProjectName = @vchProjectName,
													@sSourceSafeINI = @vchSourceSafeINI,
													@sServerName = @vchServerName,
													@sDatabaseName = @vchDatabaseName,
													@sObjectName = @vchObjectName,
													@sComment = @vchComment,
													@sLoginName = @vchLoginName,
													@sPassword = [REDACTED]
													@iVCSFlags = @iVCSFlags,
													@iActionFlag = @iActionFlag,
													@sStream = ''
        end

        if @iReturn <> 0 GOTO E_OAError

        if @iActionFlag = 1
        begin
            commit tran compile_all
            if @@error <> 0 GOTO E_Compile_Fail
        end

    end

CleanUp:
	return

E_Compile_Fail:
	declare @lerror int
	select @lerror = @@error
	rollback tran compile_all
	RAISERROR (@lerror,16,-1)
	goto CleanUp

E_OAError:
	if @iActionFlag = 1 rollback tran compile_all
	exec dbo.dt_displayoaerror @iObjectId, @iReturn
	goto CleanUp


GO

-- ----------------------------
-- Procedure structure for dt_checkinobject_u
-- ----------------------------
DROP PROCEDURE [dbo].[dt_checkinobject_u]
GO



CREATE PROCEDURE [dbo].[dt_checkinobject_u]
    @chObjectType  char(4),
    @vchObjectName nvarchar(255),
    @vchComment    nvarchar(255)='',
    @vchLoginName  nvarchar(255),
    @vchPassword   nvarchar(255)='',
    @iVCSFlags     int = 0,
    @iActionFlag   int = 0,   /* 0 => AddFile, 1 => CheckIn */
    @txStream1     text = '',  /* drop stream   */ /* There is a bug that if items are NULL they do not pass to OLE servers */
    @txStream2     text = '',  /* create stream */
    @txStream3     text = ''   /* grant stream  */

as	
	-- This procedure should no longer be called;  dt_checkinobject should be called instead.
	-- Calls are forwarded to dt_checkinobject to maintain backward compatibility.
	set nocount on
	exec dbo.dt_checkinobject
		@chObjectType,
		@vchObjectName,
		@vchComment,
		@vchLoginName,
		@vchPassword,
		@iVCSFlags,
		@iActionFlag,   
		@txStream1,		
		@txStream2,		
		@txStream3


GO

-- ----------------------------
-- Procedure structure for dt_checkoutobject
-- ----------------------------
DROP PROCEDURE [dbo].[dt_checkoutobject]
GO



CREATE PROCEDURE [dbo].[dt_checkoutobject]
    @chObjectType  char(4),
    @vchObjectName varchar(255),
    @vchComment    varchar(255),
    @vchLoginName  varchar(255),
    @vchPassword   varchar(255),
    @iVCSFlags     int = 0,
    @iActionFlag   int = 0/* 0 => Checkout, 1 => GetLatest, 2 => UndoCheckOut */

as

	set nocount on

	declare @iReturn int
	declare @iObjectId int
	select @iObjectId =0

	declare @VSSGUID varchar(100)
	select @VSSGUID = 'SQLVersionControl.VCS_SQL'

	declare @iReturnValue int
	select @iReturnValue = 0

	declare @vchTempText varchar(255)

	/* this is for our strings */
	declare @iStreamObjectId int
	select @iStreamObjectId = 0

    declare @iPropertyObjectId int
    select @iPropertyObjectId = (select objectid from dbo.dtproperties where property = 'VCSProjectID')

    declare @vchProjectName   varchar(255)
    declare @vchSourceSafeINI varchar(255)
    declare @vchServerName    varchar(255)
    declare @vchDatabaseName  varchar(255)
    exec dbo.dt_getpropertiesbyid_vcs @iPropertyObjectId, 'VCSProject',       @vchProjectName   OUT
    exec dbo.dt_getpropertiesbyid_vcs @iPropertyObjectId, 'VCSSourceSafeINI', @vchSourceSafeINI OUT
    exec dbo.dt_getpropertiesbyid_vcs @iPropertyObjectId, 'VCSSQLServer',     @vchServerName    OUT
    exec dbo.dt_getpropertiesbyid_vcs @iPropertyObjectId, 'VCSSQLDatabase',   @vchDatabaseName  OUT

    if @chObjectType = 'PROC'
    begin
        /* Procedure Can have up to three streams
           Drop Stream, Create Stream, GRANT stream */

        exec @iReturn = master.dbo.sp_OACreate @VSSGUID, @iObjectId OUT

        if @iReturn <> 0 GOTO E_OAError

        exec @iReturn = master.dbo.sp_OAMethod @iObjectId,
												'CheckOut_StoredProcedure',
												NULL,
												@sProjectName = @vchProjectName,
												@sSourceSafeINI = @vchSourceSafeINI,
												@sObjectName = @vchObjectName,
												@sServerName = @vchServerName,
												@sDatabaseName = @vchDatabaseName,
												@sComment = @vchComment,
												@sLoginName = @vchLoginName,
												@sPassword = [REDACTED]
												@iVCSFlags = @iVCSFlags,
												@iActionFlag = @iActionFlag

        if @iReturn <> 0 GOTO E_OAError


        exec @iReturn = master.dbo.sp_OAGetProperty @iObjectId, 'GetStreamObject', @iStreamObjectId OUT

        if @iReturn <> 0 GOTO E_OAError

        create table #commenttext (id int identity, sourcecode varchar(255))


        select @vchTempText = 'STUB'
        while @vchTempText is not null
        begin
            exec @iReturn = master.dbo.sp_OAMethod @iStreamObjectId, 'GetStream', @iReturnValue OUT, @vchTempText OUT
            if @iReturn <> 0 GOTO E_OAError
            
            if (@vchTempText = '') set @vchTempText = null
            if (@vchTempText is not null) insert into #commenttext (sourcecode) select @vchTempText
        end

        select 'VCS'=sourcecode from #commenttext order by id
        select 'SQL'=text from syscomments where id = object_id(@vchObjectName) order by colid

    end

CleanUp:
    return

E_OAError:
    exec dbo.dt_displayoaerror @iObjectId, @iReturn
    GOTO CleanUp


GO

-- ----------------------------
-- Procedure structure for dt_checkoutobject_u
-- ----------------------------
DROP PROCEDURE [dbo].[dt_checkoutobject_u]
GO



CREATE PROCEDURE [dbo].[dt_checkoutobject_u]
    @chObjectType  char(4),
    @vchObjectName nvarchar(255),
    @vchComment    nvarchar(255),
    @vchLoginName  nvarchar(255),
    @vchPassword   nvarchar(255),
    @iVCSFlags     int = 0,
    @iActionFlag   int = 0/* 0 => Checkout, 1 => GetLatest, 2 => UndoCheckOut */

as

	-- This procedure should no longer be called;  dt_checkoutobject should be called instead.
	-- Calls are forwarded to dt_checkoutobject to maintain backward compatibility.
	set nocount on
	exec dbo.dt_checkoutobject
		@chObjectType,  
		@vchObjectName, 
		@vchComment,    
		@vchLoginName,  
		@vchPassword,  
		@iVCSFlags,    
		@iActionFlag


GO

-- ----------------------------
-- Procedure structure for dt_displayoaerror
-- ----------------------------
DROP PROCEDURE [dbo].[dt_displayoaerror]
GO



CREATE PROCEDURE [dbo].[dt_displayoaerror]
    @iObject int,
    @iresult int
as

set nocount on

declare @vchOutput      varchar(255)
declare @hr             int
declare @vchSource      varchar(255)
declare @vchDescription varchar(255)

    exec @hr = master.dbo.sp_OAGetErrorInfo @iObject, @vchSource OUT, @vchDescription OUT

    select @vchOutput = @vchSource + ': ' + @vchDescription
    raiserror (@vchOutput,16,-1)

    return


GO

-- ----------------------------
-- Procedure structure for dt_displayoaerror_u
-- ----------------------------
DROP PROCEDURE [dbo].[dt_displayoaerror_u]
GO



CREATE PROCEDURE [dbo].[dt_displayoaerror_u]
    @iObject int,
    @iresult int
as
	-- This procedure should no longer be called;  dt_displayoaerror should be called instead.
	-- Calls are forwarded to dt_displayoaerror to maintain backward compatibility.
	set nocount on
	exec dbo.dt_displayoaerror
		@iObject,
		@iresult


GO

-- ----------------------------
-- Procedure structure for dt_droppropertiesbyid
-- ----------------------------
DROP PROCEDURE [dbo].[dt_droppropertiesbyid]
GO



CREATE PROCEDURE [dbo].[dt_droppropertiesbyid]
	@id int,
	@property varchar(64)
as
	set nocount on

	if (@property is null) or (@property = '')
		delete from dbo.dtproperties where objectid=@id
	else
		delete from dbo.dtproperties 
			where objectid=@id and property=@property


GO

-- ----------------------------
-- Procedure structure for dt_dropuserobjectbyid
-- ----------------------------
DROP PROCEDURE [dbo].[dt_dropuserobjectbyid]
GO



CREATE PROCEDURE [dbo].[dt_dropuserobjectbyid]
	@id int
as
	set nocount on
	delete from dbo.dtproperties where objectid=@id


GO

-- ----------------------------
-- Procedure structure for dt_generateansiname
-- ----------------------------
DROP PROCEDURE [dbo].[dt_generateansiname]
GO



CREATE PROCEDURE [dbo].[dt_generateansiname](@name varchar(255) output) 
as 
	declare @prologue varchar(20) 
	declare @indexstring varchar(20) 
	declare @index integer 
 
	set @prologue = 'MSDT-A-' 
	set @index = 1 
 
	while 1 = 1 
	begin 
		set @indexstring = cast(@index as varchar(20)) 
		set @name = @prologue + @indexstring 
		if not exists (select value from dtproperties where value = @name) 
			break 
		 
		set @index = @index + 1 
 
		if (@index = 10000) 
			goto TooMany 
	end 
 
Leave: 
 
	return 
 
TooMany: 
 
	set @name = 'DIAGRAM' 
	goto Leave


GO

-- ----------------------------
-- Procedure structure for dt_getobjwithprop
-- ----------------------------
DROP PROCEDURE [dbo].[dt_getobjwithprop]
GO



CREATE PROCEDURE [dbo].[dt_getobjwithprop]
	@property varchar(30),
	@value varchar(255)
as
	set nocount on

	if (@property is null) or (@property = '')
	begin
		raiserror('Must specify a property name.',-1,-1)
		return (1)
	end

	if (@value is null)
		select objectid id from dbo.dtproperties
			where property=@property

	else
		select objectid id from dbo.dtproperties
			where property=@property and value=@value


GO

-- ----------------------------
-- Procedure structure for dt_getobjwithprop_u
-- ----------------------------
DROP PROCEDURE [dbo].[dt_getobjwithprop_u]
GO



CREATE PROCEDURE [dbo].[dt_getobjwithprop_u]
	@property varchar(30),
	@uvalue nvarchar(255)
as
	set nocount on

	if (@property is null) or (@property = '')
	begin
		raiserror('Must specify a property name.',-1,-1)
		return (1)
	end

	if (@uvalue is null)
		select objectid id from dbo.dtproperties
			where property=@property

	else
		select objectid id from dbo.dtproperties
			where property=@property and uvalue=@uvalue


GO

-- ----------------------------
-- Procedure structure for dt_getpropertiesbyid
-- ----------------------------
DROP PROCEDURE [dbo].[dt_getpropertiesbyid]
GO



CREATE PROCEDURE [dbo].[dt_getpropertiesbyid]
	@id int,
	@property varchar(64)
as
	set nocount on

	if (@property is null) or (@property = '')
		select property, version, value, lvalue
			from dbo.dtproperties
			where  @id=objectid
	else
		select property, version, value, lvalue
			from dbo.dtproperties
			where  @id=objectid and @property=property


GO

-- ----------------------------
-- Procedure structure for dt_getpropertiesbyid_u
-- ----------------------------
DROP PROCEDURE [dbo].[dt_getpropertiesbyid_u]
GO



CREATE PROCEDURE [dbo].[dt_getpropertiesbyid_u]
	@id int,
	@property varchar(64)
as
	set nocount on

	if (@property is null) or (@property = '')
		select property, version, uvalue, lvalue
			from dbo.dtproperties
			where  @id=objectid
	else
		select property, version, uvalue, lvalue
			from dbo.dtproperties
			where  @id=objectid and @property=property


GO

-- ----------------------------
-- Procedure structure for dt_getpropertiesbyid_vcs
-- ----------------------------
DROP PROCEDURE [dbo].[dt_getpropertiesbyid_vcs]
GO



CREATE PROCEDURE [dbo].[dt_getpropertiesbyid_vcs]
    @id       int,
    @property varchar(64),
    @value    varchar(255) = NULL OUT

as

    set nocount on

    select @value = (
        select value
                from dbo.dtproperties
                where @id=objectid and @property=property
                )


GO

-- ----------------------------
-- Procedure structure for dt_getpropertiesbyid_vcs_u
-- ----------------------------
DROP PROCEDURE [dbo].[dt_getpropertiesbyid_vcs_u]
GO



CREATE PROCEDURE [dbo].[dt_getpropertiesbyid_vcs_u]
    @id       int,
    @property varchar(64),
    @value    nvarchar(255) = NULL OUT

as

    -- This procedure should no longer be called;  dt_getpropertiesbyid_vcsshould be called instead.
	-- Calls are forwarded to dt_getpropertiesbyid_vcs to maintain backward compatibility.
	set nocount on
    exec dbo.dt_getpropertiesbyid_vcs
		@id,
		@property,
		@value output


GO

-- ----------------------------
-- Procedure structure for dt_isundersourcecontrol
-- ----------------------------
DROP PROCEDURE [dbo].[dt_isundersourcecontrol]
GO



CREATE PROCEDURE [dbo].[dt_isundersourcecontrol]
    @vchLoginName varchar(255) = '',
    @vchPassword  varchar(255) = '',
    @iWhoToo      int = 0 /* 0 => Just check project; 1 => get list of objs */

as

	set nocount on

	declare @iReturn int
	declare @iObjectId int
	select @iObjectId = 0

	declare @VSSGUID varchar(100)
	select @VSSGUID = 'SQLVersionControl.VCS_SQL'

	declare @iReturnValue int
	select @iReturnValue = 0

	declare @iStreamObjectId int
	select @iStreamObjectId   = 0

	declare @vchTempText varchar(255)

    declare @iPropertyObjectId int
    select @iPropertyObjectId = (select objectid from dbo.dtproperties where property = 'VCSProjectID')

    declare @vchProjectName   varchar(255)
    declare @vchSourceSafeINI varchar(255)
    declare @vchServerName    varchar(255)
    declare @vchDatabaseName  varchar(255)
    exec dbo.dt_getpropertiesbyid_vcs @iPropertyObjectId, 'VCSProject',       @vchProjectName   OUT
    exec dbo.dt_getpropertiesbyid_vcs @iPropertyObjectId, 'VCSSourceSafeINI', @vchSourceSafeINI OUT
    exec dbo.dt_getpropertiesbyid_vcs @iPropertyObjectId, 'VCSSQLServer',     @vchServerName    OUT
    exec dbo.dt_getpropertiesbyid_vcs @iPropertyObjectId, 'VCSSQLDatabase',   @vchDatabaseName  OUT

    if (@vchProjectName = '')	set @vchProjectName		= null
    if (@vchSourceSafeINI = '') set @vchSourceSafeINI	= null
    if (@vchServerName = '')	set @vchServerName		= null
    if (@vchDatabaseName = '')	set @vchDatabaseName	= null
    
    if (@vchProjectName is null) or (@vchSourceSafeINI is null) or (@vchServerName is null) or (@vchDatabaseName is null)
    begin
        RAISERROR('Not Under Source Control',16,-1)
        return
    end

    if @iWhoToo = 1
    begin

        /* Get List of Procs in the project */
        exec @iReturn = master.dbo.sp_OACreate @VSSGUID, @iObjectId OUT
        if @iReturn <> 0 GOTO E_OAError

        exec @iReturn = master.dbo.sp_OAMethod @iObjectId,
												'GetListOfObjects',
												NULL,
												@vchProjectName,
												@vchSourceSafeINI,
												@vchServerName,
												@vchDatabaseName,
												@vchLoginName,
												@vchPassword

        if @iReturn <> 0 GOTO E_OAError

        exec @iReturn = master.dbo.sp_OAGetProperty @iObjectId, 'GetStreamObject', @iStreamObjectId OUT

        if @iReturn <> 0 GOTO E_OAError

        create table #ObjectList (id int identity, vchObjectlist varchar(255))

        select @vchTempText = 'STUB'
        while @vchTempText is not null
        begin
            exec @iReturn = master.dbo.sp_OAMethod @iStreamObjectId, 'GetStream', @iReturnValue OUT, @vchTempText OUT
            if @iReturn <> 0 GOTO E_OAError
            
            if (@vchTempText = '') set @vchTempText = null
            if (@vchTempText is not null) insert into #ObjectList (vchObjectlist ) select @vchTempText
        end

        select vchObjectlist from #ObjectList order by id
    end

CleanUp:
    return

E_OAError:
    exec dbo.dt_displayoaerror @iObjectId, @iReturn
    goto CleanUp


GO

-- ----------------------------
-- Procedure structure for dt_isundersourcecontrol_u
-- ----------------------------
DROP PROCEDURE [dbo].[dt_isundersourcecontrol_u]
GO



CREATE PROCEDURE [dbo].[dt_isundersourcecontrol_u]
    @vchLoginName nvarchar(255) = '',
    @vchPassword  nvarchar(255) = '',
    @iWhoToo      int = 0 /* 0 => Just check project; 1 => get list of objs */

as
	-- This procedure should no longer be called;  dt_isundersourcecontrol should be called instead.
	-- Calls are forwarded to dt_isundersourcecontrol to maintain backward compatibility.
	set nocount on
	exec dbo.dt_isundersourcecontrol
		@vchLoginName,
		@vchPassword,
		@iWhoToo


GO

-- ----------------------------
-- Procedure structure for dt_removefromsourcecontrol
-- ----------------------------
DROP PROCEDURE [dbo].[dt_removefromsourcecontrol]
GO



CREATE PROCEDURE [dbo].[dt_removefromsourcecontrol]

as

    set nocount on

    declare @iPropertyObjectId int
    select @iPropertyObjectId = (select objectid from dbo.dtproperties where property = 'VCSProjectID')

    exec dbo.dt_droppropertiesbyid @iPropertyObjectId, null

    /* -1 is returned by dt_droppopertiesbyid */
    if @@error <> 0 and @@error <> -1 return 1

    return 0


GO

-- ----------------------------
-- Procedure structure for dt_setpropertybyid
-- ----------------------------
DROP PROCEDURE [dbo].[dt_setpropertybyid]
GO



CREATE PROCEDURE [dbo].[dt_setpropertybyid]
	@id int,
	@property varchar(64),
	@value varchar(255),
	@lvalue image
as
	set nocount on
	declare @uvalue nvarchar(255) 
	set @uvalue = convert(nvarchar(255), @value) 
	if exists (select * from dbo.dtproperties 
			where objectid=@id and property=@property)
	begin
		--
		-- bump the version count for this row as we update it
		--
		update dbo.dtproperties set value=@value, uvalue=@uvalue, lvalue=@lvalue, version=version+1
			where objectid=@id and property=@property
	end
	else
	begin
		--
		-- version count is auto-set to 0 on initial insert
		--
		insert dbo.dtproperties (property, objectid, value, uvalue, lvalue)
			values (@property, @id, @value, @uvalue, @lvalue)
	end


GO

-- ----------------------------
-- Procedure structure for dt_setpropertybyid_u
-- ----------------------------
DROP PROCEDURE [dbo].[dt_setpropertybyid_u]
GO



CREATE PROCEDURE [dbo].[dt_setpropertybyid_u]
	@id int,
	@property varchar(64),
	@uvalue nvarchar(255),
	@lvalue image
as
	set nocount on
	-- 
	-- If we are writing the name property, find the ansi equivalent. 
	-- If there is no lossless translation, generate an ansi name. 
	-- 
	declare @avalue varchar(255) 
	set @avalue = null 
	if (@uvalue is not null) 
	begin 
		if (convert(nvarchar(255), convert(varchar(255), @uvalue)) = @uvalue) 
		begin 
			set @avalue = convert(varchar(255), @uvalue) 
		end 
		else 
		begin 
			if 'DtgSchemaNAME' = @property 
			begin 
				exec dbo.dt_generateansiname @avalue output 
			end 
		end 
	end 
	if exists (select * from dbo.dtproperties 
			where objectid=@id and property=@property)
	begin
		--
		-- bump the version count for this row as we update it
		--
		update dbo.dtproperties set value=@avalue, uvalue=@uvalue, lvalue=@lvalue, version=version+1
			where objectid=@id and property=@property
	end
	else
	begin
		--
		-- version count is auto-set to 0 on initial insert
		--
		insert dbo.dtproperties (property, objectid, value, uvalue, lvalue)
			values (@property, @id, @avalue, @uvalue, @lvalue)
	end


GO

-- ----------------------------
-- Procedure structure for dt_validateloginparams
-- ----------------------------
DROP PROCEDURE [dbo].[dt_validateloginparams]
GO



CREATE PROCEDURE [dbo].[dt_validateloginparams]
    @vchLoginName  varchar(255),
    @vchPassword   varchar(255)
as

set nocount on

declare @iReturn int
declare @iObjectId int
select @iObjectId =0

declare @VSSGUID varchar(100)
select @VSSGUID = 'SQLVersionControl.VCS_SQL'

    declare @iPropertyObjectId int
    select @iPropertyObjectId = (select objectid from dbo.dtproperties where property = 'VCSProjectID')

    declare @vchSourceSafeINI varchar(255)
    exec dbo.dt_getpropertiesbyid_vcs @iPropertyObjectId, 'VCSSourceSafeINI', @vchSourceSafeINI OUT

    exec @iReturn = master.dbo.sp_OACreate @VSSGUID, @iObjectId OUT
    if @iReturn <> 0 GOTO E_OAError

    exec @iReturn = master.dbo.sp_OAMethod @iObjectId,
											'ValidateLoginParams',
											NULL,
											@sSourceSafeINI = @vchSourceSafeINI,
											@sLoginName = @vchLoginName,
											@sPassword = [REDACTED]
    if @iReturn <> 0 GOTO E_OAError

CleanUp:
    return

E_OAError:
    exec dbo.dt_displayoaerror @iObjectId, @iReturn
    GOTO CleanUp


GO

-- ----------------------------
-- Procedure structure for dt_validateloginparams_u
-- ----------------------------
DROP PROCEDURE [dbo].[dt_validateloginparams_u]
GO



CREATE PROCEDURE [dbo].[dt_validateloginparams_u]
    @vchLoginName  nvarchar(255),
    @vchPassword   nvarchar(255)
as

	-- This procedure should no longer be called;  dt_validateloginparams should be called instead.
	-- Calls are forwarded to dt_validateloginparams to maintain backward compatibility.
	set nocount on
	exec dbo.dt_validateloginparams
		@vchLoginName,
		@vchPassword


GO

-- ----------------------------
-- Procedure structure for dt_vcsenabled
-- ----------------------------
DROP PROCEDURE [dbo].[dt_vcsenabled]
GO



CREATE PROCEDURE [dbo].[dt_vcsenabled]

as

set nocount on

declare @iObjectId int
select @iObjectId = 0

declare @VSSGUID varchar(100)
select @VSSGUID = 'SQLVersionControl.VCS_SQL'

    declare @iReturn int
    exec @iReturn = master.dbo.sp_OACreate @VSSGUID, @iObjectId OUT
    if @iReturn <> 0 raiserror('', 16, -1) /* Can't Load Helper DLLC */


GO

-- ----------------------------
-- Procedure structure for dt_verstamp006
-- ----------------------------
DROP PROCEDURE [dbo].[dt_verstamp006]
GO



CREATE PROCEDURE [dbo].[dt_verstamp006]
as
	select 7000


GO

-- ----------------------------
-- Procedure structure for dt_verstamp007
-- ----------------------------
DROP PROCEDURE [dbo].[dt_verstamp007]
GO



CREATE PROCEDURE [dbo].[dt_verstamp007]
as
	select 7005


GO

-- ----------------------------
-- Procedure structure for dt_whocheckedout
-- ----------------------------
DROP PROCEDURE [dbo].[dt_whocheckedout]
GO



CREATE PROCEDURE [dbo].[dt_whocheckedout]
        @chObjectType  char(4),
        @vchObjectName varchar(255),
        @vchLoginName  varchar(255),
        @vchPassword   varchar(255)

as

set nocount on

declare @iReturn int
declare @iObjectId int
select @iObjectId =0

declare @VSSGUID varchar(100)
select @VSSGUID = 'SQLVersionControl.VCS_SQL'

    declare @iPropertyObjectId int

    select @iPropertyObjectId = (select objectid from dbo.dtproperties where property = 'VCSProjectID')

    declare @vchProjectName   varchar(255)
    declare @vchSourceSafeINI varchar(255)
    declare @vchServerName    varchar(255)
    declare @vchDatabaseName  varchar(255)
    exec dbo.dt_getpropertiesbyid_vcs @iPropertyObjectId, 'VCSProject',       @vchProjectName   OUT
    exec dbo.dt_getpropertiesbyid_vcs @iPropertyObjectId, 'VCSSourceSafeINI', @vchSourceSafeINI OUT
    exec dbo.dt_getpropertiesbyid_vcs @iPropertyObjectId, 'VCSSQLServer',     @vchServerName    OUT
    exec dbo.dt_getpropertiesbyid_vcs @iPropertyObjectId, 'VCSSQLDatabase',   @vchDatabaseName  OUT

    if @chObjectType = 'PROC'
    begin
        exec @iReturn = master.dbo.sp_OACreate @VSSGUID, @iObjectId OUT

        if @iReturn <> 0 GOTO E_OAError

        declare @vchReturnValue varchar(255)
        select @vchReturnValue = ''

        exec @iReturn = master.dbo.sp_OAMethod @iObjectId,
												'WhoCheckedOut',
												@vchReturnValue OUT,
												@sProjectName = @vchProjectName,
												@sSourceSafeINI = @vchSourceSafeINI,
												@sObjectName = @vchObjectName,
												@sServerName = @vchServerName,
												@sDatabaseName = @vchDatabaseName,
												@sLoginName = @vchLoginName,
												@sPassword = [REDACTED]

        if @iReturn <> 0 GOTO E_OAError

        select @vchReturnValue

    end

CleanUp:
    return

E_OAError:
    exec dbo.dt_displayoaerror @iObjectId, @iReturn
    GOTO CleanUp


GO

-- ----------------------------
-- Procedure structure for dt_whocheckedout_u
-- ----------------------------
DROP PROCEDURE [dbo].[dt_whocheckedout_u]
GO



CREATE PROCEDURE [dbo].[dt_whocheckedout_u]
        @chObjectType  char(4),
        @vchObjectName nvarchar(255),
        @vchLoginName  nvarchar(255),
        @vchPassword   nvarchar(255)

as

	-- This procedure should no longer be called;  dt_whocheckedout should be called instead.
	-- Calls are forwarded to dt_whocheckedout to maintain backward compatibility.
	set nocount on
	exec dbo.dt_whocheckedout
		@chObjectType, 
		@vchObjectName,
		@vchLoginName, 
		@vchPassword


GO

-- ----------------------------
-- Procedure structure for eipdel
-- ----------------------------
DROP PROCEDURE [dbo].[eipdel]
GO



CREATE PROCEDURE [dbo].[eipdel]
 as 
declare @tablename as varchar(800)
Declare @strSQL nvarchar(4000)

declare tc cursor for select [name] from sysobjects where [name] like '%TB_MP_%'
open tc
FETCH NEXT FROM tc INTO @tablename

WHILE @@FETCH_STATUS = 0
begin
	
	SET @strSQL = N''
	SET @strSQL = @strSQL + N'DROP TABLE '+@tablename+N' '
	print @strSQL
	FETCH NEXT FROM tc INTO @tablename
	exec sp_executesql @strsql
end 
CLOSE tc
DEALLOCATE tc


GO

-- ----------------------------
-- Procedure structure for eipprcd
-- ----------------------------
DROP PROCEDURE [dbo].[eipprcd]
GO



CREATE PROCEDURE [dbo].[eipprcd] 
 as 
declare @strsql nvarchar(4000)
declare @mptable as varchar(500)
declare @i as int
declare mp cursor for select mpointcode from tb_measurepoint where signaltype ='AI' 
open mp
fetch next from mp into @mptable
while @@fetch_status=0
begin
	begin
		set @strsql=N'CREATE TABLE dbo.TB_MP_'+@mptable+N'_day([ItemID] [bigint] IDENTITY(1,1) NOT NULL,[ParmValue] [money] NOT NULL,[MeasureDT] [datetime] NOT NULL,[memotype] [varchar](100) NULL,[memo] [varchar](500) NULL,[userid] [varchar](50) NULL,[insdt] [varchar](50) NULL)'


		print @strsql
		print 'go '
     	set @strsql=N'CREATE TABLE dbo.TB_MP_'+@mptable+N'_hour([ItemID] [bigint] IDENTITY(1,1) NOT NULL,[ParmValue] [money] NOT NULL,[MeasureDT] [datetime] NOT NULL,[memotype] [varchar](100) NULL,[memo] [varchar](500) NULL,[userid] [varchar](50) NULL,[insdt] [varchar](50) NULL)'


		print @strsql
		print 'go '

		
	end
fetch from mp into @mptable	
end
close mp
deallocate mp


GO

-- ----------------------------
-- Procedure structure for getdatafromalarm
-- ----------------------------
DROP PROCEDURE [dbo].[getdatafromalarm]
GO



CREATE PROCEDURE [dbo].[getdatafromalarm] as  
BEGIN 
declare @strSQL nvarchar(4000)
declare @MP varchar(200)
declare @tbn varchar(200)

declare @V varchar(50)
declare @D datetime

declare @tagname varchar(50)
declare @parmname varchar(50)
declare @alarmtype varchar(50)

declare @stype int

set @V = 0

--DECLARE @StartDate DateTime
--DECLARE @EndDate DateTime
--SET @StartDate = DateAdd(mi,-4,GetDate())
--SET @EndDate = DateAdd(mi,-1,GetDate())

if OBJECT_ID('tempdb..#alarmtemp') is not null
drop table #alarmtemp

SELECT substring(a.MPointID,6,len(a.MPointID)) as MPointID,a.mpointcode,a.ParmName,b.faultdate as DataTime, convert(varchar(50),b.parmvalue) as DataValue,b.mpid as tagname,b.ConfirmAlarm  into #alarmtemp   FROM  [EIP_PRD_HQWS].[dbo].[TB_MeasurePoint] a  right JOIN UV_ALARM b ON substring(a.MPointID,6,len(a.MPointID))=CAST(b.mpid as varchar(50)) where a.BizType='污水'  and a.source_type='auto'

DECLARE curRunStatus cursor for 

select MPointID,mpointcode,ParmName,DataTime,DataValue,tagname,ConfirmAlarm  from #alarmtemp
open curRunStatus

FETCH curRunStatus into @MP,@tbn,@parmname,@D,@V,@tagname,@alarmtype

while (@@fetch_status=0)
begin

if(@alarmtype='报警')
begin
set @V='1'
end
if(@alarmtype='报警恢复')
begin
set @V='0'
end

SET @strSQL = N''
SET @strSQL = @strSQL + N'if not exists (select * from  EIP_PRD_HQWS.dbo.TB_MeasurePoint where parmvalue='+cast(@v as varchar(20))+' and mpointcode='''+@tbn+''')'+ char(10)
SET @strSQL = @strSQL + N'insert into EIP_PRD_HQWS.dbo.[tb_mp_'+@tbn+'](parmvalue,measuredt) values('+cast(@v as varchar(20))+','''+CONVERT (varchar(20), @d,20)+''')' + char(10)
--SET @strSQL = @strSQL + N'go' + char(10)
SET @strSQL = @strSQL + N'update EIP_PRD_HQWS.dbo.TB_MeasurePoint set ParmValue = '+cast(@v as varchar(20))+',measuredt='''  +CONVERT (varchar(20), @d,20)+''' where mpointcode='''+@tbn+'''' + char(10)
print @strsql
exec sp_executesql @strsql

--SET @strSQL = N''
--SET @strSQL = @strSQL + N'if not exists (select * from  EIP_PRD_THWS.dbo.TB_MeasurePoint where parmvalue='+cast(@v as varchar(20))+' and mpointcode='''+@tbn+''')'+ char(10)
--SET @strSQL = @strSQL + N'update EIP_PRD_HQWS.dbo.TB_MeasurePoint set ParmValue = '+cast(@v as varchar(20))+',measuredt='''  +CONVERT (varchar(20), @d,20)+''' where mpointcode='''+@tbn+'''' + char(10)
----SET @strSQL = @strSQL + N'go' + char(10)
--print @strsql
----exec sp_executesql @strsql

FETCH curRunStatus into @MP,@tbn,@parmname,@D,@V,@tagname,@alarmtype

--FETCH curRunStatus into @MP,@tbn,@parmname

end

close curRunStatus
DEALLOCATE curRunStatus

END


GO

-- ----------------------------
-- Procedure structure for getdatafrominsql
-- ----------------------------
DROP PROCEDURE [dbo].[getdatafrominsql]
GO



CREATE PROCEDURE [dbo].[getdatafrominsql] as  
BEGIN 
declare @strSQL nvarchar(4000)
declare @MP varchar(200)
declare @tbn varchar(200)

DECLARE @StartDate DateTime
DECLARE @EndDate DateTime
declare @V money
declare @D datetime
SET @StartDate = DateAdd(mi,-4,GetDate())
SET @EndDate = GetDate()

DECLARE curRunStatus cursor for 
SELECT [MPointID],[mpointcode] FROM [EIP_PRD_FSCCSK].[dbo].[TB_MeasurePoint] where BizType='自来水'
open curRunStatus

FETCH curRunStatus into @MP,@tbn

while (@@fetch_status=0)
begin

set @strSQL = 'SELECT top 1 @SD = DateTime, @SV = Value FROM [INSQLDB].[RUNTIME].[DBO].v_History WHERE TagName IN ('''+ @MP +''')  AND wwVersion = ''Latest''  AND wwRetrievalMode = ''Cyclic'' and wwcyclecount=100 and wwqualityrule=''Extended''  AND DateTime >= '''+convert(nvarchar,@StartDate,21)+''' AND DateTime <= '''+ convert(nvarchar,@EndDate,21)+''' order by datetime desc' 
Print @strSQL
exec sp_executesql @strSQL,N'@SD DateTime output,@SV money output',@d output,@v output
print cast(@v as varchar(20))
print convert(nvarchar,@d,21)


set @strSQL ='insert into EIP_PRD_FSCCSK.dbo.[tb_mp_'+@tbn+'](parmvalue,measuredt) values('+cast(@v as varchar(20))+','''+CONVERT (varchar(20), @d,20)+''')' 
print @strsql
--exec sp_executesql @strsql

set @strSQL ='update EIP_PRD_FSCCSK.dbo.TB_MeasurePoint set ParmValue = '+cast(@v as varchar(20))+',measuredt='''  +CONVERT (varchar(20), @d,20)+''' where MPointid='''+@mp+''''
print @strsql
--exec sp_executesql @strsql

FETCH curRunStatus into @MP,@tbn

end

close curRunStatus
DEALLOCATE curRunStatus

END


GO

-- ----------------------------
-- Procedure structure for getdatafrominsqlold
-- ----------------------------
DROP PROCEDURE [dbo].[getdatafrominsqlold]
GO



CREATE PROCEDURE [dbo].[getdatafrominsqlold]  @mpname varchar(100) as  
BEGIN 
declare @strSQL nvarchar(4000)
declare @MP varchar(200)
declare @tbn varchar(200)

DECLARE @StartDate DateTime
DECLARE @EndDate DateTime
declare @V money
declare @D datetime
SET @StartDate = DateAdd(mi,-4,GetDate())
SET @EndDate = GetDate()
set @MP =@mpname

set @strSQL = 'SELECT top 1 @SD = DateTime, @SV = Value FROM [INSQLDB].[RUNTIME].[DBO].v_History WHERE TagName IN ('''+ @MP +''')  AND wwVersion = ''Latest''  AND wwRetrievalMode = ''Cyclic'' and wwcyclecount=100 and wwqualityrule=''Extended''  AND DateTime >= '''+convert(nvarchar,@StartDate,21)+''' AND DateTime <= '''+ convert(nvarchar,@EndDate,21)+''' order by datetime desc' 
Print @strSQL
exec sp_executesql @strSQL,N'@SD DateTime output,@SV money output',@d output,@v output
print cast(@v as varchar(20))
print convert(nvarchar,@d,21)

set @strSQL ='select @t = MPointCode from EIP_PRD_FSCCSK.dbo.TB_MeasurePoint where MPointid='''+@mp+''''
Print @strSQL
exec sp_executesql @strSQL,N'@t varchar(200) output',@tbn output
Print @tbn


set @strSQL ='insert into EIP_PRD_FSCCSK.dbo.[tb_mp_'+@tbn+'](parmvalue,measuredt) values('+cast(@v as varchar(20))+','''+CONVERT (varchar(20), @d,20)+''')' 
print @strsql
--exec sp_executesql @strsql

set @strSQL ='update EIP_PRD_FSCCSK.dbo.TB_MeasurePoint set ParmValue = '+cast(@v as varchar(20))+',measuredt='''  +CONVERT (varchar(20), @d,20)+''' where MPointid='''+@mp+''''
print @strsql
--exec sp_executesql @strsql

END


GO

-- ----------------------------
-- Procedure structure for LinShi
-- ----------------------------
DROP PROCEDURE [dbo].[LinShi]
GO



CREATE PROCEDURE [dbo].[LinShi] 
 as 
declare @strsql nvarchar(4000)
declare @mptable as varchar(500)
declare @i as int
declare mp cursor for select mpointcode from eip_prd.dbo.TB_MeasurePoint where MPointID in (select MPointID FROM [EIP_PRD_Water].[dbo].[TB_MeasurePoint]) and BizType='opc'
open mp
fetch next from mp into @mptable
while @@fetch_status=0
begin
	begin
		set @strsql=N'drop table [TB_MP_'+@mptable+N'] '
		print @strsql
		print 'go '
		set @strsql=N'create view [dbo].[TB_MP_'+@mptable+N'] '
		print @strsql
		print 'as '
		set @strsql=N'select * from [EIP_PRD_Water].[dbo].[TB_MP_'+@mptable+N'] '
		print @strsql
		print 'go '
	end
fetch from mp into @mptable	
end
close mp
deallocate mp


GO

-- ----------------------------
-- Procedure structure for p_splitpage
-- ----------------------------
DROP PROCEDURE [dbo].[p_splitpage]
GO



CREATE PROCEDURE [dbo].[p_splitpage]         
  @sql   nvarchar(4000), --要执行的sql语句   
  @page   int=1,   --要显示的页码   
  @pageSize   int=20, --每页的大小   
  @pageCount   int=0   out, --总页数   
  @recordCount   int=0   out --总记录数   
  as   
  set   nocount   on   
  declare   @p1   int   
    
  exec   sp_cursoropen   @p1   output,@sql,@scrollopt=1,@ccopt=1,@rowcount=@pagecount   output   
  set   @recordCount   =   @pageCount   
  select   @pagecount=ceiling(1.0*@pagecount/@pagesize)   
  ,@page=(@page-1)*@pagesize+1   
  exec   sp_cursorfetch   @p1,16,@page,@pagesize     
  exec   sp_cursorclose   @p1


GO

-- ----------------------------
-- Procedure structure for procday
-- ----------------------------
DROP PROCEDURE [dbo].[procday]
GO



CREATE PROCEDURE [dbo].[procday] AS
declare @mpcode as varchar(50)
declare @procday as varchar(50)
Declare @sdate   datetime
Declare @edate  datetime
Declare @strSQL nvarchar(4000)
declare @v money
declare @v1 money
declare @v2 money

set @edate = getdate();
set @edate = dateadd(hh,-datepart(hh,@edate),@edate)
set @edate = dateadd(ss,-datepart(ss,@edate),@edate)
set @edate = dateadd(mi,-datepart(mi,@edate),@edate)
--set @edate = dateadd(hh,8,@edate) --当日8点到明日8点
print @edate
set @sdate = dateadd(day,-1,@edate)
print @sdate
declare mp cursor for select mpointcode,procday from tb_measurepoint 
open mp
FETCH NEXT FROM mp

INTO @mpcode, @procday

WHILE @@FETCH_STATUS = 0
begin

	if @procday is not null and len(@procday)>0
	begin
		SET @strSQL = N''
		SET @strSQL = @strSQL + N'if not exists (select 1 ' + char(10)
		SET @strSQL = @strSQL + N'            from  sysobjects ' + char(10)
		SET @strSQL = @strSQL + N'           where  id = object_id(''dbo.TB_MP_' + @mpcode + N'_day'') ' + char(10)
		SET @strSQL = @strSQL + N'            and   type = ''U'') ' + char(10)
		SET @strSQL = @strSQL + N'   create table dbo.TB_MP_' + @mpcode + N'_day ('+char(10) + char(10)
		SET @strSQL = @strSQL + N'   ItemID               bigint               identity, ' + char(10)
		SET @strSQL = @strSQL + N'   ParmValue            money                not null default 0, ' + char(10)
		SET @strSQL = @strSQL + N'   MeasureDT            datetime             not null default getdate(), ' + char(10)
		SET @strSQL = @strSQL + N'   userid            varchar(50)            , ' + char(10)
		SET @strSQL = @strSQL + N'   insdt            varchar(50) 	default getdate() , ' + char(10)
		SET @strSQL = @strSQL + N'   constraint PK_TB_MP_' + @mpcode + N'_day primary key  (ItemID) ' + char(10)
		SET @strSQL = @strSQL + N') ' + char(10) + char(10)
		print @strsql
		exec sp_executesql @strsql

		if(@procday='avg') 
		begin
			SET @strSQL = N''
			SET @strSQL = @strSQL + N'select @av=avg(parmvalue) from tb_mp_' + @mpcode+N' ' +char(10)
			SET @strSQL = @strSQL + N'where measuredt between ''' +cast(@sdate as varchar(20)) +N''' and '''+cast(@edate as varchar(20))+N''' and parmvalue <> 0 '+char(10)
			print @strsql
			exec sp_executesql @strsql,N'@av money output',@v output
			if(@v =null) set @v=0
			print cast(@v as varchar(20))
		end
		if(@procday='min') 
		begin
			SET @strSQL = N''
			SET @strSQL = @strSQL + N'select @av=min(parmvalue) from tb_mp_' + @mpcode+N' ' +char(10)
			SET @strSQL = @strSQL + N'where measuredt between ''' +cast(@sdate as varchar(20)) +N''' and '''+cast(@edate as varchar(20))+N''''+char(10)
			print @strsql
			exec sp_executesql @strsql,N'@av money output',@v output
			if(@v =null) set @v=0
			print cast(@v as varchar(20))
		end
		if(@procday='max') 
		begin
			SET @strSQL = N''
			SET @strSQL = @strSQL + N'select @av=max(parmvalue) from tb_mp_' + @mpcode+N' ' +char(10)
			SET @strSQL = @strSQL + N'where measuredt between ''' +cast(@sdate as varchar(20)) +N''' and '''+cast(@edate as varchar(20))+N''''+char(10)
			print @strsql
			exec sp_executesql @strsql,N'@av money output',@v output
			if(@v =null) set @v=0
			print cast(@v as varchar(20))
		end
		if(@procday='first') 
		begin
			SET @strSQL = N''
			SET @strSQL = @strSQL + N'select top 1 @av=parmvalue from tb_mp_' + @mpcode+N' ' +char(10)
			SET @strSQL = @strSQL + N'where measuredt between ''' +cast(@sdate as varchar(20)) +N''' and '''+cast(@edate as varchar(20))+N''' order by measuredt asc'+char(10)
			print @strsql
			exec sp_executesql @strsql,N'@av money output',@v output
			if(@v =null) set @v=0
			print cast(@v as varchar(20))
		end
		if(@procday='last') 
		begin
			SET @strSQL = N''
			SET @strSQL = @strSQL + N'select top 1 @av=parmvalue from tb_mp_' + @mpcode+N' ' +char(10)
			SET @strSQL = @strSQL + N'where measuredt between ''' +cast(@sdate as varchar(20)) +N''' and '''+cast(@edate as varchar(20))+N''' order by measuredt desc'+char(10)
			print @strsql
			exec sp_executesql @strsql,N'@av money output',@v output
			if(@v =null) set @v=0
			print cast(@v as varchar(20))
		end
		if(@procday='diff') 
		begin
			SET @strSQL = N''
			SET @strSQL = @strSQL + N'select top 1 @av=parmvalue from tb_mp_' + @mpcode+N' ' +char(10)
			SET @strSQL = @strSQL + N'where measuredt between ''' +cast(@sdate as varchar(20)) +N''' and '''+cast(@edate as varchar(20))+N''' order by measuredt asc'+char(10)
			print @strsql
			exec sp_executesql @strsql,N'@av money output',@v1 output
			if(@v1 =null) set @v1=0
			print cast(@v1 as varchar(20))

			SET @strSQL = N''
			SET @strSQL = @strSQL + N'select top 1 @av=parmvalue from tb_mp_' + @mpcode+N' ' +char(10)
			SET @strSQL = @strSQL + N'where measuredt between ''' +cast(dateadd(day,1,@sdate) as varchar(20)) +N''' and '''+cast(dateadd(day,1,@edate) as varchar(20))+N''' order by measuredt asc'+char(10)
			print @strsql
			exec sp_executesql @strsql,N'@av money output',@v2 output
			if(@v2 =null) set @v2=0
			print cast(@v2 as varchar(20))
			
			if(@v2 - @v1 > 0)
			set @v = @v2 - @v1
			else
			set @v = 0
		end
		if(@procday='time')
		begin
			SET @strSQL = N''
			SET @strSQL = @strSQL + N'select top 1 @av=parmvalue from tb_mp_' + @mpcode+N' ' +char(10)
			SET @strSQL = @strSQL + N'where measuredt between ''' +cast(@sdate as varchar(20)) +N''' and '''+cast(@edate as varchar(20))+N''' order by measuredt asc'+char(10)
			print @strsql
			exec sp_executesql @strsql,N'@av money output',@v1 output
			if(@v1 =null) set @v1=0
			print cast(@v1 as varchar(20))

			SET @strSQL = N''
			SET @strSQL = @strSQL + N'select top 1 @av=parmvalue from tb_mp_' + @mpcode+N' ' +char(10)
			SET @strSQL = @strSQL + N'where measuredt between ''' +cast(dateadd(day,1,@sdate) as varchar(20)) +N''' and '''+cast(dateadd(day,1,@edate) as varchar(20))+N''' order by measuredt asc'+char(10)
			print @strsql
			exec sp_executesql @strsql,N'@av money output',@v2 output
			if(@v2 =null) set @v2=0
			print cast(@v2 as varchar(20))
			
			if(@v2 - @v1 > 0)
			begin
			if((@v2*60 - @v1*60) >= 1440 )
			begin
				set @v = 1440
			end
			else
			begin
				set @v = @v2*60 - @v1*60
			end			
			end
			else
			set @v = 0
		end				
		SET @strSQL = N''
		SET @strSQL = @strSQL + N'insert into tb_mp_' + @mpcode+N'_day(parmvalue,measuredt) values( ' +cast(@v as varchar(20))+','''
		SET @strSQL = @strSQL + CONVERT (varchar(20), @sdate,20)+N''')'+char(10)  
		print @strsql
		exec sp_executesql @strsql
	end
	FETCH NEXT FROM mp
	INTO @mpcode, @procday
end 
CLOSE mp
DEALLOCATE mp


GO

-- ----------------------------
-- Procedure structure for procdayX
-- ----------------------------
DROP PROCEDURE [dbo].[procdayX]
GO



CREATE PROCEDURE [dbo].[procdayX] @sdatein datetime,@edatein datetime,@mpcode varchar(50),@procday varchar(50) AS

Declare @strSQL nvarchar(4000)
declare @sdate datetime
declare @edate datetime
declare @v money
declare @v1 money
declare @v2 money

set @edate = @edatein
set @edate = dateadd(hh,-datepart(hh,@edate),@edate)
set @edate = dateadd(ss,-datepart(ss,@edate),@edate)
set @edate = dateadd(mi,-datepart(mi,@edate),@edate)
--set @edate = dateadd(hh,8,@edate) --当日8点到明日8点
print @edate
set @sdate = @sdatein
set @sdate = dateadd(hh,-datepart(hh,@sdate),@sdate)
set @sdate = dateadd(ss,-datepart(ss,@sdate),@sdate)
set @sdate = dateadd(mi,-datepart(mi,@sdate),@sdate)
--set @sdate = dateadd(hh,8,@sdate) --当日8点到明日8点
print @sdate

SET @strSQL = N''
SET @strSQL = @strSQL + N'if not exists (select 1 ' + char(10)
SET @strSQL = @strSQL + N'            from  sysobjects ' + char(10)
SET @strSQL = @strSQL + N'           where  id = object_id(''dbo.TB_MP_' + @mpcode + N'_day'') ' + char(10)
SET @strSQL = @strSQL + N'            and   type = ''U'') ' + char(10)
SET @strSQL = @strSQL + N'   create table dbo.TB_MP_' + @mpcode + N'_day ('+char(10) + char(10)
SET @strSQL = @strSQL + N'   ItemID               bigint               identity, ' + char(10)
SET @strSQL = @strSQL + N'   ParmValue            money                not null default 0, ' + char(10)
SET @strSQL = @strSQL + N'   MeasureDT            datetime             not null default getdate(), ' + char(10)
SET @strSQL = @strSQL + N'   userid            varchar(50)            , ' + char(10)
SET @strSQL = @strSQL + N'   insdt            varchar(50) 	default getdate() , ' + char(10)
SET @strSQL = @strSQL + N'   constraint PK_TB_MP_' + @mpcode + N'_day primary key  (ItemID) ' + char(10)
SET @strSQL = @strSQL + N') ' + char(10) + char(10)
print @strsql
exec sp_executesql @strsql
SET @strSQL = N''
SET @strSQL = @strSQL + N'delete from tb_mp_' +  @mpcode + N'_day where MeasureDT between'''+cast(@sdate as varchar)+N''' and '''+cast(@edate as varchar)+N'''';
print @strsql
exec sp_executesql @strsql

while (@sdate<=@edate )
begin
	if(@procday='avg') 
	begin
		SET @strSQL = N''
		SET @strSQL = @strSQL + N'select @av=avg(parmvalue) from tb_mp_' + @mpcode+N' ' +char(10)
		SET @strSQL = @strSQL + N'where measuredt between ''' +cast(@sdate as varchar(20)) +N''' and '''+cast(dateadd(day,1,@sdate) as varchar(20))+N''' and parmvalue <> 0 '+char(10)
		print @strsql
		exec sp_executesql @strsql,N'@av money output',@v output
		print cast(@v as varchar(20))
	end
	if(@procday='min') 
	begin
		SET @strSQL = N''
		SET @strSQL = @strSQL + N'select @av=min(parmvalue) from tb_mp_' + @mpcode+N' ' +char(10)
		SET @strSQL = @strSQL + N'where measuredt between ''' +cast(@sdate as varchar(20)) +N''' and '''+cast(dateadd(day,1,@sdate) as varchar(20))+N''''+char(10)
		print @strsql
		exec sp_executesql @strsql,N'@av money output',@v output
		print cast(@v as varchar(20))
	end
	if(@procday='max') 
	begin
		SET @strSQL = N''
		SET @strSQL = @strSQL + N'select @av=max(parmvalue) from tb_mp_' + @mpcode+N' ' +char(10)
		SET @strSQL = @strSQL + N'where measuredt between ''' +cast(@sdate as varchar(20)) +N''' and '''+cast(dateadd(day,1,@sdate) as varchar(20))+N''''+char(10)
		print @strsql
		exec sp_executesql @strsql,N'@av money output',@v output
		print cast(@v as varchar(20))
	end
	if(@procday='first') 
	begin
		SET @strSQL = N''
		SET @strSQL = @strSQL + N'select top 1 @av=parmvalue from tb_mp_' + @mpcode+N' ' +char(10)
		SET @strSQL = @strSQL + N'where measuredt between ''' +cast(@sdate as varchar(20)) +N''' and '''+cast(dateadd(day,1,@sdate) as varchar(20))+N''' order by measuredt asc'+char(10)
		print @strsql
		exec sp_executesql @strsql,N'@av money output',@v output
		print cast(@v as varchar(20))
	end
	if(@procday='last') 
	begin
		SET @strSQL = N''
		SET @strSQL = @strSQL + N'select top 1 @av=parmvalue from tb_mp_' + @mpcode+N' ' +char(10)
		SET @strSQL = @strSQL + N'where measuredt between ''' +cast(@sdate as varchar(20)) +N''' and '''+cast(dateadd(day,1,@sdate) as varchar(20))+N''' order by measuredt desc'+char(10)
		print @strsql
		exec sp_executesql @strsql,N'@av money output',@v output
		print cast(@v as varchar(20))
	end
	if(@procday='diff') 
		begin
			SET @strSQL = N''
		SET @strSQL = @strSQL + N'select top 1 @av=parmvalue from tb_mp_' + @mpcode+N' ' +char(10)
		SET @strSQL = @strSQL + N'where measuredt between ''' +cast(@sdate as varchar(20)) +N''' and '''+cast(dateadd(day,1,@sdate) as varchar(20))+N''' order by measuredt asc'+char(10)
		print @strsql
		exec sp_executesql @strsql,N'@av money output',@v1 output
		print cast(@v1 as varchar(20))

			SET @strSQL = N''
		SET @strSQL = @strSQL + N'select top 1 @av=parmvalue from tb_mp_' + @mpcode+N' ' +char(10)
		SET @strSQL = @strSQL + N'where measuredt between ''' +cast(dateadd(day,1,@sdate) as varchar(20)) +N''' and '''+cast(dateadd(day,2,@sdate) as varchar(20))+N''' order by measuredt asc'+char(10)
		print @strsql
		exec sp_executesql @strsql,N'@av money output',@v2 output
		print cast(@v2 as varchar(20))
			
			if(@v2 - @v1 > 0)
			set @v = @v2 - @v1
			else
			set @v = 0
		end
	if(@procday='time') 
		begin
			SET @strSQL = N''
		SET @strSQL = @strSQL + N'select top 1 @av=parmvalue from tb_mp_' + @mpcode+N' ' +char(10)
		SET @strSQL = @strSQL + N'where measuredt between ''' +cast(@sdate as varchar(20)) +N''' and '''+cast(dateadd(day,1,@sdate) as varchar(20))+N''' order by measuredt asc'+char(10)
		print @strsql
		exec sp_executesql @strsql,N'@av money output',@v1 output
		print cast(@v1 as varchar(20))

			SET @strSQL = N''
		SET @strSQL = @strSQL + N'select top 1 @av=parmvalue from tb_mp_' + @mpcode+N' ' +char(10)
		SET @strSQL = @strSQL + N'where measuredt between ''' +cast(dateadd(day,1,@sdate) as varchar(20)) +N''' and '''+cast(dateadd(day,2,@sdate) as varchar(20))+N''' order by measuredt asc'+char(10)
		print @strsql
		exec sp_executesql @strsql,N'@av money output',@v2 output
		print cast(@v2 as varchar(20))
			
			if(@v2 - @v1 > 0)
			begin
			if((@v2*60 - @v1*60) >= 1440 )
			begin
				set @v = 1440
			end
			else
			begin
				set @v = @v2*60 - @v1*60
			end			
			end
			else
			set @v = 0
		end			
	if @v IS NULL
	begin
		set  @v=0
	end
	SET @strSQL = N''
	SET @strSQL = @strSQL + N'insert into tb_mp_' + @mpcode+N'_day(parmvalue,measuredt) values( ' +cast(@v as varchar(20))+','''
	SET @strSQL = @strSQL + CONVERT (varchar(20), @sdate,20)+N''')'+char(10)  
	print @strsql
	exec sp_executesql @strsql

	set @sdate = dateadd(day,1,@sdate)
	
end


GO

-- ----------------------------
-- Procedure structure for prochour
-- ----------------------------
DROP PROCEDURE [dbo].[prochour]
GO



CREATE PROCEDURE [dbo].[prochour] AS
declare @mpcode as varchar(50)
declare @prochour as varchar(50)
Declare @sdate   datetime
Declare @edate  datetime
Declare @strSQL nvarchar(4000)
declare @v money
declare @v1 money
declare @v2 money

set @edate = getdate();
set @edate = dateadd(ss,-datepart(ss,@edate),@edate)
set @edate = dateadd(mi,-datepart(mi,@edate),@edate)
print @edate
set @sdate = dateadd(hh,-1,@edate)
print @sdate
declare mp cursor for select mpointcode,prochour from tb_measurepoint 
open mp
FETCH NEXT FROM mp

INTO @mpcode, @prochour

WHILE @@FETCH_STATUS = 0
begin

	if @prochour is not null and len(@prochour)>0
	begin
		SET @strSQL = N''
		SET @strSQL = @strSQL + N'if not exists (select 1 ' + char(10)
		SET @strSQL = @strSQL + N'            from  sysobjects ' + char(10)
		SET @strSQL = @strSQL + N'           where  id = object_id(''dbo.TB_MP_' + @mpcode + N'_hour'') ' + char(10)
		SET @strSQL = @strSQL + N'            and   type = ''U'') ' + char(10)
		SET @strSQL = @strSQL + N'   create table dbo.TB_MP_' + @mpcode + N'_hour ('+char(10) + char(10)
		SET @strSQL = @strSQL + N'   ItemID               bigint               identity, ' + char(10)
		SET @strSQL = @strSQL + N'   ParmValue            money                not null default 0, ' + char(10)
		SET @strSQL = @strSQL + N'   MeasureDT            datetime             not null default getdate(), ' + char(10)
		SET @strSQL = @strSQL + N'   userid            varchar(50)            , ' + char(10)
		SET @strSQL = @strSQL + N'   insdt            varchar(50) 	default getdate() , ' + char(10)
		SET @strSQL = @strSQL + N'   constraint PK_TB_MP_' + @mpcode + N'_hour primary key  (ItemID) ' + char(10)
		SET @strSQL = @strSQL + N') ' + char(10) + char(10)
		print @strsql
		exec sp_executesql @strsql

		if(@prochour='avg') 
		begin
			SET @strSQL = N''
			SET @strSQL = @strSQL + N'select @av=avg(parmvalue) from tb_mp_' + @mpcode+N' ' +char(10)
			SET @strSQL = @strSQL + N'where measuredt between ''' +cast(@sdate as varchar(20)) +N''' and '''+cast(@edate as varchar(20))+N''' and parmvalue <> 0 '+char(10)
			print @strsql
			exec sp_executesql @strsql,N'@av money output',@v output
			if(@v =null) set @v=0
			print cast(@v as varchar(20))
		end
		if(@prochour='min') 
		begin
			SET @strSQL = N''
			SET @strSQL = @strSQL + N'select @av=min(parmvalue) from tb_mp_' + @mpcode+N' ' +char(10)
			SET @strSQL = @strSQL + N'where measuredt between ''' +cast(@sdate as varchar(20)) +N''' and '''+cast(@edate as varchar(20))+N''''+char(10)
			print @strsql
			exec sp_executesql @strsql,N'@av money output',@v output
			if(@v =null) set @v=0
			print cast(@v as varchar(20))
		end
		if(@prochour='max') 
		begin
			SET @strSQL = N''
			SET @strSQL = @strSQL + N'select @av=max(parmvalue) from tb_mp_' + @mpcode+N' ' +char(10)
			SET @strSQL = @strSQL + N'where measuredt between ''' +cast(@sdate as varchar(20)) +N''' and '''+cast(@edate as varchar(20))+N''''+char(10)
			print @strsql
			exec sp_executesql @strsql,N'@av money output',@v output
			if(@v =null) set @v=0
			print cast(@v as varchar(20))
		end
		if(@prochour='first') 
		begin
			SET @strSQL = N''
			SET @strSQL = @strSQL + N'select top 1 @av=parmvalue from tb_mp_' + @mpcode+N' ' +char(10)
			SET @strSQL = @strSQL + N'where measuredt between ''' +cast(@sdate as varchar(20)) +N''' and '''+cast(@edate as varchar(20))+N''' order by measuredt asc'+char(10)
			print @strsql
			exec sp_executesql @strsql,N'@av money output',@v output
			if(@v =null) set @v=0
			print cast(@v as varchar(20))
		end
		if(@prochour='last') 
		begin
			SET @strSQL = N''
			SET @strSQL = @strSQL + N'select top 1 @av=parmvalue from tb_mp_' + @mpcode+N' ' +char(10)
			SET @strSQL = @strSQL + N'where measuredt between ''' +cast(@sdate as varchar(20)) +N''' and '''+cast(@edate as varchar(20))+N''' order by measuredt desc'+char(10)
			print @strsql
			exec sp_executesql @strsql,N'@av money output',@v output
			if(@v =null) set @v=0
			print cast(@v as varchar(20))
		end
		if(@prochour='diff') 
		begin
			SET @strSQL = N''
			SET @strSQL = @strSQL + N'select top 1 @av=parmvalue from tb_mp_' + @mpcode+N' ' +char(10)
			SET @strSQL = @strSQL + N'where measuredt between ''' +cast(@sdate as varchar(20)) +N''' and '''+cast(@edate as varchar(20))+N''' order by measuredt asc'+char(10)
			print @strsql
			exec sp_executesql @strsql,N'@av money output',@v1 output
			if(@v1 =null) set @v1=0
			print cast(@v1 as varchar(20))

			SET @strSQL = N''
			SET @strSQL = @strSQL + N'select top 1 @av=parmvalue from tb_mp_' + @mpcode+N' ' +char(10)
			SET @strSQL = @strSQL + N'where measuredt between ''' +cast(dateadd(hh,1,@sdate) as varchar(20)) +N''' and '''+cast(dateadd(hh,1,@edate) as varchar(20))+N''' order by measuredt asc'+char(10)
			print @strsql
			exec sp_executesql @strsql,N'@av money output',@v2 output
			if(@v2 =null) set @v2=0
			print cast(@v2 as varchar(20))
			
			if(@v2 - @v1 > 0)
			set @v = @v2 - @v1
			else
			set @v = 0
		end
		if(@prochour='time') 
		begin
			SET @strSQL = N''
			SET @strSQL = @strSQL + N'select top 1 @av=parmvalue from tb_mp_' + @mpcode+N' ' +char(10)
			SET @strSQL = @strSQL + N'where measuredt between ''' +cast(@sdate as varchar(20)) +N''' and '''+cast(@edate as varchar(20))+N''' order by measuredt asc'+char(10)
			print @strsql
			exec sp_executesql @strsql,N'@av money output',@v1 output
			if(@v1 =null) set @v1=0
			print cast(@v1 as varchar(20))

			SET @strSQL = N''
			SET @strSQL = @strSQL + N'select top 1 @av=parmvalue from tb_mp_' + @mpcode+N' ' +char(10)
			SET @strSQL = @strSQL + N'where measuredt between ''' +cast(dateadd(hh,1,@sdate) as varchar(20)) +N''' and '''+cast(dateadd(hh,1,@edate) as varchar(20))+N''' order by measuredt asc'+char(10)
			print @strsql
			exec sp_executesql @strsql,N'@av money output',@v2 output
			if(@v2 =null) set @v2=0
			print cast(@v2 as varchar(20))
			
			if(@v2 - @v1 > 0)
			begin
			if((@v2*60 - @v1*60) >= 60 )
			begin
				set @v = 60
			end
			else
			begin
				set @v = @v2*60 - @v1*60
			end			
			end
			else
			set @v = 0
		end			
		SET @strSQL = N''
		SET @strSQL = @strSQL + N'insert into tb_mp_' + @mpcode+N'_hour(parmvalue,measuredt) values( ' +cast(@v as varchar(20))+','''
		SET @strSQL = @strSQL + CONVERT (varchar(20), @sdate,20)+N''')'+char(10)  
		print @strsql
		exec sp_executesql @strsql
	end
	FETCH NEXT FROM mp
	INTO @mpcode, @prochour
end 
CLOSE mp
DEALLOCATE mp


GO

-- ----------------------------
-- Procedure structure for prochourX
-- ----------------------------
DROP PROCEDURE [dbo].[prochourX]
GO



CREATE PROCEDURE [dbo].[prochourX] @sdatein datetime,@edatein datetime,@mpcode varchar(50),@prochour varchar(50) AS

Declare @strSQL nvarchar(4000)
declare @sdate datetime
declare @edate datetime
declare @v money
declare @v1 money
declare @v2 money

set @edate = @edatein
set @edate = dateadd(ss,-datepart(ss,@edate),@edate)
set @edate = dateadd(mi,-datepart(mi,@edate),@edate)
print @edate
set @sdate = @sdatein
set @sdate = dateadd(ss,-datepart(ss,@sdate),@sdate)
set @sdate = dateadd(mi,-datepart(mi,@sdate),@sdate)
print @sdate

SET @strSQL = N''
SET @strSQL = @strSQL + N'if not exists (select 1 ' + char(10)
SET @strSQL = @strSQL + N'            from  sysobjects ' + char(10)
SET @strSQL = @strSQL + N'           where  id = object_id(''dbo.TB_MP_' + @mpcode + N'_hour'') ' + char(10)
SET @strSQL = @strSQL + N'            and   type = ''U'') ' + char(10)
SET @strSQL = @strSQL + N'   create table dbo.TB_MP_' + @mpcode + N'_hour ('+char(10) + char(10)
SET @strSQL = @strSQL + N'   ItemID               bigint               identity, ' + char(10)
SET @strSQL = @strSQL + N'   ParmValue            money                not null default 0, ' + char(10)
SET @strSQL = @strSQL + N'   MeasureDT            datetime             not null default getdate(), ' + char(10)
SET @strSQL = @strSQL + N'   userid            varchar(50)            , ' + char(10)
SET @strSQL = @strSQL + N'   insdt            varchar(50) 	default getdate() , ' + char(10)
SET @strSQL = @strSQL + N'   constraint PK_TB_MP_' + @mpcode + N'_hour primary key  (ItemID) ' + char(10)
SET @strSQL = @strSQL + N') ' + char(10) + char(10)
print @strsql
exec sp_executesql @strsql
SET @strSQL = N''
SET @strSQL = @strSQL + N'delete from tb_mp_' +  @mpcode + N'_hour where MeasureDT between '''+cast(@sdate as varchar)+N''' and '''+cast(@edate as varchar)+N'''';
print @strsql
exec sp_executesql @strsql

while (@sdate<=@edate )
begin
	if(@prochour='avg') 
	begin
		SET @strSQL = N''
		SET @strSQL = @strSQL + N'select @av=avg(parmvalue) from tb_mp_' + @mpcode+N' ' +char(10)
		SET @strSQL = @strSQL + N'where measuredt between ''' +cast(@sdate as varchar(20)) +N''' and '''+cast(dateadd(hh,1,@sdate) as varchar(20))+N''' and parmvalue <> 0 '+char(10)
		print @strsql
		exec sp_executesql @strsql,N'@av money output',@v output
		print cast(@v as varchar(20))
	end
	if(@prochour='min') 
	begin
		SET @strSQL = N''
		SET @strSQL = @strSQL + N'select @av=min(parmvalue) from tb_mp_' + @mpcode+N' ' +char(10)
		SET @strSQL = @strSQL + N'where measuredt between ''' +cast(@sdate as varchar(20)) +N''' and '''+cast(dateadd(hh,1,@sdate) as varchar(20))+N''''+char(10)
		print @strsql
		exec sp_executesql @strsql,N'@av money output',@v output
		print cast(@v as varchar(20))
	end
	if(@prochour='max') 
	begin
		SET @strSQL = N''
		SET @strSQL = @strSQL + N'select @av=max(parmvalue) from tb_mp_' + @mpcode+N' ' +char(10)
		SET @strSQL = @strSQL + N'where measuredt between ''' +cast(@sdate as varchar(20)) +N''' and '''+cast(dateadd(hh,1,@sdate) as varchar(20))+N''''+char(10)
		print @strsql
		exec sp_executesql @strsql,N'@av money output',@v output
		print cast(@v as varchar(20))
	end
	if(@prochour='first') 
	begin
		SET @strSQL = N''
		SET @strSQL = @strSQL + N'select top 1 @av=parmvalue from tb_mp_' + @mpcode+N' ' +char(10)
		SET @strSQL = @strSQL + N'where measuredt between ''' +cast(@sdate as varchar(20)) +N''' and '''+cast(dateadd(hh,1,@sdate) as varchar(20))+N''' order by measuredt asc'+char(10)
		print @strsql
		exec sp_executesql @strsql,N'@av money output',@v output
		print cast(@v as varchar(20))
	end
	if(@prochour='last') 
	begin
		SET @strSQL = N''
		SET @strSQL = @strSQL + N'select top 1 @av=parmvalue from tb_mp_' + @mpcode+N' ' +char(10)
		SET @strSQL = @strSQL + N'where measuredt between ''' +cast(@sdate as varchar(20)) +N''' and '''+cast(dateadd(hh,1,@sdate) as varchar(20))+N''' order by measuredt desc'+char(10)
		print @strsql
		exec sp_executesql @strsql,N'@av money output',@v output
		print cast(@v as varchar(20))
	end
	if(@prochour='diff') 
		begin
			SET @strSQL = N''
		SET @strSQL = @strSQL + N'select top 1 @av=parmvalue from tb_mp_' + @mpcode+N' ' +char(10)
		SET @strSQL = @strSQL + N'where measuredt between ''' +cast(@sdate as varchar(20)) +N''' and '''+cast(dateadd(hh,1,@sdate) as varchar(20))+N''' order by measuredt asc'+char(10)
		print @strsql
		exec sp_executesql @strsql,N'@av money output',@v1 output
		print cast(@v1 as varchar(20))

			SET @strSQL = N''
		SET @strSQL = @strSQL + N'select top 1 @av=parmvalue from tb_mp_' + @mpcode+N' ' +char(10)
		SET @strSQL = @strSQL + N'where measuredt between ''' +cast(dateadd(hh,1,@sdate) as varchar(20)) +N''' and '''+cast(dateadd(hh,2,@sdate) as varchar(20))+N''' order by measuredt asc'+char(10)
		print @strsql
		exec sp_executesql @strsql,N'@av money output',@v2 output
		print cast(@v2 as varchar(20))
			
			if(@v2 - @v1 > 0)
			set @v = @v2 - @v1
			else
			set @v = 0
		end
	if(@prochour='time') 
		begin
			SET @strSQL = N''
		SET @strSQL = @strSQL + N'select top 1 @av=parmvalue from tb_mp_' + @mpcode+N' ' +char(10)
		SET @strSQL = @strSQL + N'where measuredt between ''' +cast(@sdate as varchar(20)) +N''' and '''+cast(dateadd(hh,1,@sdate) as varchar(20))+N''' order by measuredt asc'+char(10)
		print @strsql
		exec sp_executesql @strsql,N'@av money output',@v1 output
		print cast(@v1 as varchar(20))

			SET @strSQL = N''
		SET @strSQL = @strSQL + N'select top 1 @av=parmvalue from tb_mp_' + @mpcode+N' ' +char(10)
		SET @strSQL = @strSQL + N'where measuredt between ''' +cast(dateadd(hh,1,@sdate) as varchar(20)) +N''' and '''+cast(dateadd(hh,2,@sdate) as varchar(20))+N''' order by measuredt asc'+char(10)
		print @strsql
		exec sp_executesql @strsql,N'@av money output',@v2 output
		print cast(@v2 as varchar(20))
			
			if(@v2 - @v1 > 0)
			begin
			if((@v2*60 - @v1*60) >= 60 )
			begin
				set @v = 60
			end
			else
			begin
				set @v = @v2*60 - @v1*60
			end			
			end
			else
			set @v = 0
		end				
	if @v IS NULL
	begin
		set  @v=0
	end
	SET @strSQL = N''
	SET @strSQL = @strSQL + N'insert into tb_mp_' + @mpcode+N'_hour(parmvalue,measuredt) values( ' +cast(@v as varchar(20))+','''
	SET @strSQL = @strSQL + CONVERT (varchar(20), @sdate,20)+N''')'+char(10)  
	print @strsql
	exec sp_executesql @strsql

	set @sdate = dateadd(hh,1,@sdate)
	
end


GO

-- ----------------------------
-- Procedure structure for procruntimeX
-- ----------------------------
DROP PROCEDURE [dbo].[procruntimeX]
GO



CREATE PROCEDURE [dbo].[procruntimeX] @sdatein datetime,@edatein datetime,@mpcode varchar(50) AS

Declare @strSQL nvarchar(4000)
declare @sdate datetime
declare @edate datetime
--declare @s money
--declare @s1 money
--declare @s2 money

set @edate = @edatein
set @edate = dateadd(hh,-datepart(hh,@edate),@edate)
set @edate = dateadd(ss,-datepart(ss,@edate),@edate)
set @edate = dateadd(mi,-datepart(mi,@edate),@edate)
--select @edate = dateadd(hh,8,@edate) --当日8点到明日8点
print @edate
set @sdate = @sdatein
set @sdate = dateadd(hh,-datepart(hh,@sdate),@sdate)
set @sdate = dateadd(ss,-datepart(ss,@sdate),@sdate)
set @sdate = dateadd(mi,-datepart(mi,@sdate),@sdate)


--select @sdate = dateadd(hh,8,@sdate) --当日8点到明日8点
print @sdate

SET @strSQL = N''
SET @strSQL = @strSQL + N'if not exists (select 1 ' + char(10)
SET @strSQL = @strSQL + N'            from  sysobjects ' + char(10)
SET @strSQL = @strSQL + N'           where  id = object_id(''dbo.TB_MP_' + @mpcode + N'_runtime'') ' + char(10)
SET @strSQL = @strSQL + N'            and   type = ''U'') ' + char(10)
SET @strSQL = @strSQL + N'   create table dbo.TB_MP_' + @mpcode + N'_runtime ('+char(10) + char(10)
SET @strSQL = @strSQL + N'   ItemID               bigint               identity, ' + char(10)
SET @strSQL = @strSQL + N'   ParmValue            money                not null default 0, ' + char(10)
SET @strSQL = @strSQL + N'   MeasureDT            datetime             not null default getdate(), ' + char(10)
SET @strSQL = @strSQL + N'   userid            varchar(50)            , ' + char(10)
SET @strSQL = @strSQL + N'   insdt            varchar(50) 	default getdate() , ' + char(10)
SET @strSQL = @strSQL + N'   constraint PK_TB_MP_' + @mpcode + N'_runtime primary key  (ItemID) ' + char(10)
SET @strSQL = @strSQL + N') ' + char(10) + char(10)
print @strsql
exec sp_executesql @strsql
SET @strSQL = N''
SET @strSQL = @strSQL + N'delete from tb_mp_' +  @mpcode + N'_runtime where MeasureDT between '''+cast(@sdate as varchar)+N''' and '''+cast(@edate as varchar)+N'''';
print @strsql
exec sp_executesql @strsql

while (@sdate<@edate )
begin
	SET @strSQL = N''
	SET @strSQL = @strSQL+N'declare @count int'+char(10)
	SET @strSQL = @strSQL+N'declare @ParmValue money'+char(10)
	SET @strSQL = @strSQL+N'declare @MeasureDT datetime'+char(10)
	SET @strSQL = @strSQL+N'declare @lastParmValue money'+char(10)
	SET @strSQL = @strSQL+N'declare @lastMeasureDT datetime'+char(10)
	SET @strSQL = @strSQL+N'declare @str nvarchar(4000)'+char(10)
	SET @strSQL = @strSQL+N'declare @s money'+char(10)
	SET @strSQL = @strSQL+N'declare @s1 money'+char(10)
	SET @strSQL = @strSQL+N'declare @s2 money'+char(10)
	

	SET @strSQL = @strSQL + N'select top 1 @lastParmValue =ParmValue ,@lastMeasureDT=MeasureDT from TB_MP_'+@mpcode+N' where measuredt between '''+cast(@sdate as varchar(20))+N''' and ''' +cast(dateadd(day,1,@sdate) as varchar(20))+ N''' and measuredt > ''2000-01-01''  order by measuredt asc ' +char(10)

	--如果第一天记录代表运行
	SET @strSQL = @strSQL + N'if(@lastParmValue=1)'+char(10)
	SET @strSQL = @strSQL + N'begin'+char(10)
	--寻找今天之前最后一条记录
	SET @strSQL = @strSQL + N'select top 1 @lastParmValue =ParmValue ,@lastMeasureDT=MeasureDT from TB_MP_'+@mpcode+N' where measuredt < '''+cast(@sdate as varchar(20))+N'''  and measuredt > ''2000-01-01''  order by measuredt desc' +char(10)
	--如果记录存在且为运行
	SET @strSQL = @strSQL + N'if(not @lastParmValue is null and @lastParmValue=1)'+char(10)
	SET @strSQL = @strSQL + N'begin'+char(10)
	--做一条假的开机记录
	SET @strSQL = @strSQL + N'set @lastMeasureDT='''+cast(@sdate as varchar(20))+N''''+char(10)
	SET @strSQL = @strSQL + N'set @lastParmValue=1 '+char(10)
	SET @strSQL = @strSQL + N'end'+char(10)
	SET @strSQL = @strSQL + N'end'+char(10)


	SET @strSQL = @strSQL + N'if not exists (select * from TB_MP_'+@mpcode+N' where measuredt between '''+cast(@sdate as varchar(20))+N''' and ''' +cast(dateadd(day,1,@sdate) as varchar(20))+ N'''  and measuredt > ''2000-01-01''  and ParmValue = 0)'+char(10)
	SET @strSQL = @strSQL + N'begin'+char(10)
	SET @strSQL = @strSQL + N'set @s = 1440'+char(10)
	SET @strSQL = @strSQL + N'insert into tb_mp_' + @mpcode+N'_runtime(parmvalue,measuredt) values( @s,'''
	SET @strSQL = @strSQL + CONVERT (varchar(20), @sdate,20)+N''')'+char(10)  
	SET @strSQL = @strSQL + N'end'+char(10)

	SET @strSQL = @strSQL + N'if not exists (select * from TB_MP_'+@mpcode+N' where measuredt between '''+cast(@sdate as varchar(20))+N''' and ''' +cast(dateadd(day,1,@sdate) as varchar(20))+ N'''  and measuredt > ''2000-01-01''  and ParmValue = 1)'+char(10)
	SET @strSQL = @strSQL + N'begin'+char(10)
	SET @strSQL = @strSQL + N'set @s = 0'+char(10)
	SET @strSQL = @strSQL + N'insert into tb_mp_' + @mpcode+N'_runtime(parmvalue,measuredt) values( @s,'''
	SET @strSQL = @strSQL + CONVERT (varchar(20), @sdate,20)+N''')'+char(10)  
	SET @strSQL = @strSQL + N'end'+char(10)

	SET @strSQL = @strSQL + N'if  exists (select * from TB_MP_'+@mpcode+N' where measuredt between '''+cast(@sdate as varchar(20))+N''' and ''' +cast(dateadd(day,1,@sdate) as varchar(20))+ N'''  and measuredt > ''2000-01-01''  and ParmValue = 0)'+char(10)
	SET @strSQL = @strSQL + N'begin'+char(10)
	SET @strSQL = @strSQL + N'if  exists (select * from TB_MP_'+@mpcode+N' where measuredt between '''+cast(@sdate as varchar(20))+N''' and ''' +cast(dateadd(day,1,@sdate) as varchar(20))+ N'''  and measuredt > ''2000-01-01''  and ParmValue = 1)'+char(10)
	SET @strSQL = @strSQL + N'begin'+char(10)

	SET @strSQL = @strSQL + N'set @s=0 '+char(10)

	SET @strSQL = @strSQL + N'DECLARE curRunStatus cursor for select ParmValue,MeasureDT from TB_MP_' + @mpcode+N' ' +char(10)
	SET @strSQL = @strSQL + N'where measuredt between ''' +cast(@sdate as varchar(20)) +N''' and '''+cast(dateadd(day,1,@sdate) as varchar(20))+N'''  and measuredt > ''2000-01-01''  order by MeasureDT asc'+char(10)
	SET @strSQL = @strSQL + N'open curRunStatus'+char(10)
	SET @strSQL = @strSQL + N'FETCH curRunStatus into @ParmValue,@MeasureDT'+char(10)
	SET @strSQL = @strSQL + N'while (@@fetch_status=0)'+char(10)
	SET @strSQL = @strSQL + N'begin'+char(10)
	SET @strSQL = @strSQL + N'if(@lastParmValue is null)'+char(10)
	SET @strSQL = @strSQL + N'begin'+char(10)
	SET @strSQL = @strSQL + N'set @lastParmValue = @ParmValue'+char(10)	
	SET @strSQL = @strSQL + N'set @lastMeasureDT = @MeasureDT'+char(10)	
	SET @strSQL = @strSQL + N'end'+char(10)

	SET @strSQL = @strSQL + N'if(@ParmValue<>@lastParmValue)'+char(10)
	SET @strSQL = @strSQL + N'begin'+char(10)

	SET @strSQL  = @strSQL + N'if(@ParmValue=0)'++char(10)

	SET @strSQL = @strSQL + N'begin'+char(10)
	SET @strSQL = @strSQL + N'select @s1 = datediff(mi,@lastMeasureDT,@MeasureDT)'+char(10)
	SET @strSQL = @strSQL + N'set @s = @s + @s1 '+char(10)
	SET @strSQL = @strSQL + N'end'+char(10)

	SET @strSQL  = @strSQL + N'set @lastParmValue = @ParmValue'+char(10)	
	SET @strSQL  = @strSQL + N'set @lastMeasureDT = @MeasureDT'+char(10)	
	SET @strSQL  = @strSQL + N'end'+char(10)


	SET @strSQL  = @strSQL + N'FETCH curRunStatus into @ParmValue,@MeasureDT'+char(10)	
	SET @strSQL  = @strSQL + N'end'+char(10)


	SET @strSQL  = @strSQL + N'close curRunStatus'+char(10)
	SET @strSQL  = @strSQL + N'DEALLOCATE curRunStatus'+char(10)


	SET @strSQL  = @strSQL + N'if (@lastParmValue=1 and @ParmValue=1)'+char(10)

	SET @strSQL = @strSQL + N'begin'+char(10)
	SET @strSQL  = @strSQL + N'select @s2 = datediff(mi,@lastMeasureDT, ''' +cast(dateadd(day,1,@sdate) as varchar(20))+ N''')'+char(10)
	SET @strSQL  = @strSQL + N'set @s = @s + @s2 '+char(10)
	SET @strSQL  = @strSQL + N'end'+char(10)

	SET @strSQL  = @strSQL + N'insert into tb_mp_' + @mpcode+N'_runtime(parmvalue,measuredt) values( @s,'''
	SET @strSQL  = @strSQL + CONVERT (varchar(20), @sdate,20)+N''')'+char(10)  
	SET @strSQL  = @strSQL + N'end'+char(10)
	SET @strSQL  = @strSQL + N'end'+char(10)
	
	print @strSQL
	exec sp_executesql @strSQL


	set @sdate = dateadd(day,1,@sdate)
	
end


GO

-- ----------------------------
-- Procedure structure for sp_artday_1
-- ----------------------------
DROP PROCEDURE [dbo].[sp_artday_1]
GO



CREATE PROCEDURE [dbo].[sp_artday_1]
@stime varchar(10),  @starthour int,  @intval int 
 AS
declare @starttime datetime
declare @endtime datetime
declare @tempdate datetime
Select @starttime=cast(@stime as datetime)

--Select @starttime=DateAdd(day,-1,@starttime)  -- 鏄ㄥぉ
Select @starttime=DateAdd(hour,@starthour,@starttime) 
--Select @starttime=dateadd(minute,30,@starttime)  --澧炲姞鍗婂皬鏃?
select @endtime = DateAdd(day,1,@starttime)

delete from tb_mp_temp_rpt

while(@starttime<@endtime)
begin
	set @tempdate = dateadd(hour,@intval,@starttime)


    exec sp_getdata @starttime,@tempdate,'first','-'
	exec sp_getdata @starttime,@tempdate,'first','P1JS_FLOW1_T'
	exec sp_getdata @starttime,@tempdate,'first','P1JS_FLOW2_T'
	exec sp_getdata @starttime,@tempdate,'first','P1JS_FLOW3_T'
	exec sp_getdata @starttime,@tempdate,'first','-'
	exec sp_getdata @starttime,@tempdate,'first','-'
	exec sp_getdata @starttime,@tempdate,'first','JS_COD'
    exec sp_getdata @starttime,@tempdate,'first','JS_NH4_N'
	exec sp_getdata @starttime,@tempdate,'first','JS_TP'
	exec sp_getdata @starttime,@tempdate,'first','JS_PH'
	exec sp_getdata @starttime,@tempdate,'first','WHLWN_LL1'
	exec sp_getdata @starttime,@tempdate,'first','WHLWN_LL2'
	exec sp_getdata @starttime,@tempdate,'first','-'
	exec sp_getdata @starttime,@tempdate,'first','-'
	exec sp_getdata @starttime,@tempdate,'first','-'
	exec sp_getdata @starttime,@tempdate,'first','-'
    exec sp_getdata @starttime,@tempdate,'first','-'
	exec sp_getdata @starttime,@tempdate,'first','-'
	exec sp_getdata @starttime,@tempdate,'first','-'
	exec sp_getdata @starttime,@tempdate,'first','-'
	exec sp_getdata @starttime,@tempdate,'first','-'
	exec sp_getdata @starttime,@tempdate,'first','-'
	exec sp_getdata @starttime,@tempdate,'first','-'
	exec sp_getdata @starttime,@tempdate,'first','-'
	exec sp_getdata @starttime,@tempdate,'first','SWC1_DO1'
	exec sp_getdata @starttime,@tempdate,'first','SWC1_DO2'
    exec sp_getdata @starttime,@tempdate,'first','SWC2_DO1'
	exec sp_getdata @starttime,@tempdate,'first','SWC2_DO2'
	exec sp_getdata @starttime,@tempdate,'first','-'
	exec sp_getdata @starttime,@tempdate,'first','-'
    exec sp_getdata @starttime,@tempdate,'first','-'
    exec sp_getdata @starttime,@tempdate,'first','-'
    exec sp_getdata @starttime,@tempdate,'first','-'
    exec sp_getdata @starttime,@tempdate,'first','CS_COD'
    exec sp_getdata @starttime,@tempdate,'first','CS_PH'
    exec sp_getdata @starttime,@tempdate,'first','CS_NH3N'
    exec sp_getdata @starttime,@tempdate,'first','CS_TP'
 
 
	set @starttime = @tempdate
end

Select * from TB_MP_TEMP_RPT order by itemid,insdt


GO

-- ----------------------------
-- Procedure structure for Sp_CustomReportData
-- ----------------------------
DROP PROCEDURE [dbo].[Sp_CustomReportData]
GO



CREATE PROCEDURE [dbo].[Sp_CustomReportData]
	 @sdt varchar(19),
	 @edt varchar(19),
	 @calculation varchar(20),--取值类型 first,avg之类
	 @frequencyType varchar(20),--范围 min,hour,day之类
	 @frequency int,--间隔数值
	 @mpids varchar(5000),
	 @EPName varchar(50),
	 @chooseDataString varchar(5000)
  as
  begin
  	declare @LSSQL nvarchar(4000)

	declare @diffTime int

	declare @calculationSql varchar(500)
	set @calculationSql=''
	declare @leftOutSql varchar(5000)
	set @leftOutSql=''
	declare @groupSql varchar(5000)
	set @groupSql=''
	declare @orderSql varchar(5000)
	set @orderSql=''
	declare @firstGroupOtherSql varchar(5000)
	set @firstGroupOtherSql=''
	declare @whereSql varchar(5000)
	set @whereSql=' where 1=1 '+@chooseDataString

	if(@frequencyType='0') set @calculationSql='substring(CONVERT(varchar(19),ls.MeasureDT, 120 ),0,17) as MeasureDT,'
	if(@frequencyType='1') set @calculationSql='substring(CONVERT(varchar(19),ls.MeasureDT, 120 ),0,14) as MeasureDT,'
	if(@frequencyType='2') set @calculationSql='substring(CONVERT(varchar(19),ls.MeasureDT, 120 ),0,11) as MeasureDT,'
	if(@frequencyType='3') set @calculationSql='substring(CONVERT(varchar(19),ls.MeasureDT, 120 ),0,8) as MeasureDT,'

		declare @cc2 varchar(50)--拆分后每行的数据
		declare c2 cursor for
		select a from dbo.f_get_exp_split(@mpids,',')
		open c2
		fetch next from c2 into @cc2
		while @@fetch_status=0
		begin
			--print @cc2
			if(@cc2!=',') begin
				if(@calculation!='first') begin 
					set @calculationSql=@calculationSql+@calculation+'('+@cc2+'.ParmValue) as '+@cc2+'_Value, '
					set @firstGroupOtherSql=@firstGroupOtherSql+''+@cc2+'.MeasureDT,'
				end
				else begin
					--declare @jsType varchar(10)
					--if(@frequencyType='0') set @jsType='MINUTE'
					--if(@frequencyType='1') set @jsType='hour'
					--if(@frequencyType='2') set @jsType='day'
					--if(@frequencyType='3') set @jsType='month'

					set @calculationSql=@calculationSql+''+@cc2+'.ParmValue as '+@cc2+'_Value, '
					set @firstGroupOtherSql=@firstGroupOtherSql+''+@cc2+'.ParmValue,'
				end

				if(@frequencyType='0')begin
					set @leftOutSql=@leftOutSql+'left outer join ['+@EPName+'].[dbo].[tb_mp_'+@cc2+'] as '+@cc2+' on DATEDIFF(MINUTE,ls.MeasureDT,'+@cc2+'.MeasureDT)=0 and ('+@cc2+'.MeasureDT>=(substring(CONVERT(varchar(19),(select top 1 MeasureDT from #Tb_CustomReportData_SP_LS order by MeasureDT ), 120 ),0,17)+'':00'' ) and '+@cc2+'.MeasureDT<=(select top 1 MeasureDT from #Tb_CustomReportData_SP_LS order by MeasureDT desc)) '
				end
				else if(@frequencyType='1')begin
					set @leftOutSql=@leftOutSql+'left outer join ['+@EPName+'].[dbo].[tb_mp_'+@cc2+'] as '+@cc2+' on DATEDIFF(HOUR,ls.MeasureDT,'+@cc2+'.MeasureDT)=0 and ('+@cc2+'.MeasureDT>=(substring(CONVERT(varchar(19),(select top 1 MeasureDT from #Tb_CustomReportData_SP_LS order by MeasureDT ), 120 ),0,14)+'':00:00'' ) and '+@cc2+'.MeasureDT<=(select top 1 MeasureDT from #Tb_CustomReportData_SP_LS order by MeasureDT desc))'
				end
				else if(@frequencyType='2')begin
					set @leftOutSql=@leftOutSql+'left outer join ['+@EPName+'].[dbo].[tb_mp_'+@cc2+'] as '+@cc2+' on DATEDIFF(day,ls.MeasureDT,'+@cc2+'.MeasureDT)=0 and ('+@cc2+'.MeasureDT>=(substring(CONVERT(varchar(19),(select top 1 MeasureDT from #Tb_CustomReportData_SP_LS order by MeasureDT ), 120 ),0,11)+'' 00:00:00'' ) and '+@cc2+'.MeasureDT<=(select top 1 MeasureDT from #Tb_CustomReportData_SP_LS order by MeasureDT desc))'
				end
				else if(@frequencyType='3')begin
					set @leftOutSql=@leftOutSql+'left outer join ['+@EPName+'].[dbo].[tb_mp_'+@cc2+'] as '+@cc2+' on DATEDIFF(month,ls.MeasureDT,'+@cc2+'.MeasureDT)=0 and ('+@cc2+'.MeasureDT>=(substring(CONVERT(varchar(19),(select top 1 MeasureDT from #Tb_CustomReportData_SP_LS order by MeasureDT ), 120 ),0,8)+''-01 00:00:00'' ) and '+@cc2+'.MeasureDT<=(select top 1 MeasureDT from #Tb_CustomReportData_SP_LS order by MeasureDT desc))'
				end
			end
	
		fetch next from c2 into @cc2
		end
		close c2
		deallocate c2


		--print @firstGroupOtherSql

		if(@calculation='first') begin
			set @groupSql=' group by ls.MeasureDT,'+@firstGroupOtherSql
			set @groupSql=substring(@groupSql,0,DATALENGTH(@groupSql))
		end
		else begin
			set @groupSql=' group by ls.MeasureDT '
			
			set @orderSql=' order by ls.MeasureDT '
		end
		

		--print DATALENGTH(@calculationSql)
		set @calculationSql=substring(@calculationSql,0,DATALENGTH(@calculationSql)-1)
		--print @calculationSql
	
	
    --建立数据临时表
	create table [dbo].[#Tb_CustomReportData_SP_LS] 
	(
		MeasureDT datetime
	)
	

	if(@frequencyType='0')begin --分钟开始
	set @diffTime = (select DATEDIFF(day,SUBSTRING(@sdt,0,11),SUBSTRING(@edt,0,11)))  --起始时间和结束时间相差天数

	 declare @minNum int
	 set @minNum = 60/@frequency
	 declare @minStartNum int

		--拼接分钟的时间
		declare @minDiffNum int 
		set @minDiffNum=0
		while(@minDiffNum <= @diffTime)begin
			declare @minInsertTime varchar(50)
			declare @nowminTime varchar(50)
			set @nowminTime = CONVERT(varchar(19),dateadd(day,@minDiffNum,@sdt), 120 )

			declare @minI int
			set @minI=0
			set @minStartNum=0

			while(@minI < @minNum)begin
				if (@minI > 0) begin
					if (@frequency + @minStartNum != 60) begin
						set @minStartNum = @minStartNum+@frequency
					end
				end 
				--print @minStartNum
				if(@minDiffNum=0) begin
					if(SUBSTRING(@sdt,15,2)<=@minStartNum)begin
						if(@minStartNum<10) set @minInsertTime = SUBSTRING(@nowminTime,0,14)+':0'+cast(@minStartNum as varchar(2))
						if(@minStartNum>=10) set @minInsertTime = SUBSTRING(@nowminTime,0,14)+':'+cast(@minStartNum as varchar(2))

						insert into #Tb_CustomReportData_SP_LS values (@minInsertTime)
					end
				end
				else if(@minDiffNum=@diffTime)begin
					if(SUBSTRING(@edt,15,2)>=@minStartNum)begin
						if(@minStartNum<10) set @minInsertTime = SUBSTRING(@nowminTime,0,14)+':0'+cast(@minStartNum as varchar(2))
						if(@minStartNum>=10) set @minInsertTime = SUBSTRING(@nowminTime,0,14)+':'+cast(@minStartNum as varchar(2))

						insert into #Tb_CustomReportData_SP_LS values (@minInsertTime)
					end
					
				end
				else begin
					if(@minStartNum<10) set @minInsertTime = SUBSTRING(@nowminTime,0,14)+':0'+cast(@minStartNum as varchar(2))
					if(@minStartNum>=10) set @minInsertTime = SUBSTRING(@nowminTime,0,14)+':'+cast(@minStartNum as varchar(2))

					insert into #Tb_CustomReportData_SP_LS values (@minInsertTime)
				end

				set @minI=@minI+1
			end

			set @minDiffNum=@minDiffNum+1
		end

		--select * from #Tb_CustomReportData_SP_LS




	end --分钟结束
	else if(@frequencyType='1')begin --小时开始
		set @diffTime = (select DATEDIFF(day,SUBSTRING(@sdt,0,11),SUBSTRING(@edt,0,11)))  --起始时间和结束时间相差天数
		--print @diffTime
		--print '==='

		declare @hourNum int
		set @hourNum = 24/@frequency
		declare @hourStartNum int



		--拼接小时的时间
		declare @hourDiffNum int 
		set @hourDiffNum=0
		while(@hourDiffNum <= @diffTime)begin
			declare @hourInsertTime varchar(50)
			declare @nowHourTime varchar(50)
			set @nowHourTime = CONVERT(varchar(19),dateadd(day,@hourDiffNum,@sdt), 120 )

			declare @hourI int
			set @hourI=0
			set @hourStartNum=0

			while(@hourI < @hourNum)begin
				if (@hourI > 0) begin
					if (@frequency + @hourStartNum != 24) begin
						set @hourStartNum = @hourStartNum+@frequency
					end
				end 
				--print @hourStartNum
				if(@hourDiffNum=0) begin
					if(SUBSTRING(@sdt,12,2)<=@hourStartNum)begin
						if(@hourStartNum<10) set @hourInsertTime = SUBSTRING(@nowHourTime,0,11)+' 0'+cast(@hourStartNum as varchar(2))+':00'
						if(@hourStartNum>=10) set @hourInsertTime = SUBSTRING(@nowHourTime,0,11)+' '+cast(@hourStartNum as varchar(2))+':00'

						insert into #Tb_CustomReportData_SP_LS values (substring(@hourInsertTime,0,14)+':59:59' )
					end
				end
				else if(@hourDiffNum=@diffTime)begin
					if(SUBSTRING(@edt,12,2)>=@hourStartNum)begin
						if(@hourStartNum<10) set @hourInsertTime = SUBSTRING(@nowHourTime,0,11)+' 0'+cast(@hourStartNum as varchar(2))+':00'
						if(@hourStartNum>=10) set @hourInsertTime = SUBSTRING(@nowHourTime,0,11)+' '+cast(@hourStartNum as varchar(2))+':00'

						insert into #Tb_CustomReportData_SP_LS values (substring(@hourInsertTime,0,14)+':59:59' )
					end
					
				end
				else begin
					if(@hourStartNum<10) set @hourInsertTime = SUBSTRING(@nowHourTime,0,11)+' 0'+cast(@hourStartNum as varchar(2))+':00'
					if(@hourStartNum>=10) set @hourInsertTime = SUBSTRING(@nowHourTime,0,11)+' '+cast(@hourStartNum as varchar(2))+':00'

					insert into #Tb_CustomReportData_SP_LS values (substring(@hourInsertTime,0,14)+':59:59' )
				end

				set @hourI=@hourI+1
			end

			set @hourDiffNum=@hourDiffNum+1
		end

		
		


		--select * into #Tb_CustomReportData_SP_LS_Out from #Tb_CustomReportData_SP_LS
		--select * from #Tb_CustomReportData_SP_LS_Out

		
	end  --小时结束
	else if(@frequencyType='2')begin --天开始
		set @diffTime = (select DATEDIFF(day,SUBSTRING(@sdt,0,11),SUBSTRING(@edt,0,11)))  --起始时间和结束时间相差天数
	

		declare @dayDiffNum int 
		set @dayDiffNum=0
		while(@dayDiffNum <= @diffTime)begin

			declare @dayInsertTime varchar(50)
			if(@dayDiffNum>0)begin
				set @dayInsertTime=CONVERT(varchar(19),DATEADD(day,@frequency,@dayInsertTime), 120 )
			end
			else begin
				set @dayInsertTime=@sdt
			end

			print @dayInsertTime
			if(DATEDIFF(day,@dayInsertTime,@edt)>=0) 
			insert into #Tb_CustomReportData_SP_LS values (substring(@dayInsertTime,0,11)+' 23:59:59')

			set @dayDiffNum=@dayDiffNum+1
		end


	end --天结束
	else if(@frequencyType='3')begin --月开始
		set @diffTime = (select DATEDIFF(month,SUBSTRING(@sdt,0,11),SUBSTRING(@edt,0,11)))  --起始时间和结束时间相差天数
	

		declare @monthDiffNum int 
		set @monthDiffNum=0
		while(@monthDiffNum <= @diffTime)begin

			declare @monthInsertTime varchar(50)
			if(@monthDiffNum>0)begin
				set @monthInsertTime=CONVERT(varchar(19),DATEADD(month,@frequency,@monthInsertTime), 120 )
			end
			else begin
				set @monthInsertTime=@sdt
			end

			--print @monthInsertTime
			if(DATEDIFF(day,@monthInsertTime,@edt)>=0) 
			
			set @monthInsertTime=dateadd(month,1,@monthInsertTime)
			set @monthInsertTime=CONVERT(varchar(19),dateadd(day,-1,@monthInsertTime), 120 )
			
			insert into #Tb_CustomReportData_SP_LS values (@monthInsertTime)

			set @monthDiffNum=@monthDiffNum+1
		end


	end --月结束

	if(@calculation!='first') begin 
		set @LSSQL=N''
		set @LSSQL=@LSSQL+N'select '+@calculationSql
		set @LSSQL=@LSSQL+N' into #Tb_CustomReportData_SP_LS_Out from #Tb_CustomReportData_SP_LS as ls  '
		set @LSSQL=@LSSQL+N' '+@leftOutSql
		set @LSSQL=@LSSQL+N' '+@groupSql
		set @LSSQL=@LSSQL+N' '+@orderSql
		set @LSSQL=@LSSQL+N' select * from #Tb_CustomReportData_SP_LS_Out '+@whereSql
		set @LSSQL=@LSSQL+N' drop table #Tb_CustomReportData_SP_LS_Out '
		print @LSSQL
		EXEC sp_executesql @LSSQL
	end
	else begin
		declare @PARTITIONSql varchar(5000)
		set @PARTITIONSql=N''
		if(@frequencyType='0') set @PARTITIONSql=@PARTITIONSql+N'DATEPART(year, MeasureDT+'':00''), DATEPART(month, MeasureDT+'':00''),DATEPART(day, MeasureDT+'':00''),DATEPART(hour, MeasureDT+'':00''),DATEPART(MINUTE, MeasureDT+'':00'') '
		if(@frequencyType='1') set @PARTITIONSql=@PARTITIONSql+N'DATEPART(year, MeasureDT+'':00:00''), DATEPART(month, MeasureDT+'':00:00''),DATEPART(day, MeasureDT+'':00:00''),DATEPART(hour, MeasureDT+'':00:00'') '
		if(@frequencyType='2') set @PARTITIONSql=@PARTITIONSql+N'DATEPART(year, MeasureDT+'' 00:00:00''), DATEPART(month, MeasureDT+'' 00:00:00''),DATEPART(day, MeasureDT+'' 00:00:00'') '
		if(@frequencyType='3') set @PARTITIONSql=@PARTITIONSql+N'DATEPART(year, MeasureDT+''-01 00:00:00''), DATEPART(month, MeasureDT+''-01 00:00:00'') '
		

		set @LSSQL=N''
		set @LSSQL=@LSSQL+N'select * into #Tb_CustomReportData_SP_LS_Out from '
		set @LSSQL=@LSSQL+N'(select *,ROW_NUMBER() over(PARTITION by '+@PARTITIONSql+' order by MeasureDT ) as num from'
		
		set @LSSQL=@LSSQL+N'(select '+@calculationSql
		set @LSSQL=@LSSQL+N' from #Tb_CustomReportData_SP_LS as ls  '
		set @LSSQL=@LSSQL+N' '+@leftOutSql
		set @LSSQL=@LSSQL+N' '+@groupSql
		set @LSSQL=@LSSQL+N' ) main'
		set @LSSQL=@LSSQL+N' ) d'
		set @LSSQL=@LSSQL+N' where d.num=1 '
		set @LSSQL=@LSSQL+N' select * from #Tb_CustomReportData_SP_LS_Out '+@whereSql
		set @LSSQL=@LSSQL+N' drop table #Tb_CustomReportData_SP_LS_Out '
		print @LSSQL
		EXEC sp_executesql @LSSQL
	end

	--删除临时表
	drop table #Tb_CustomReportData_SP_LS
	

  end


GO

-- ----------------------------
-- Procedure structure for sp_genformuladay
-- ----------------------------
DROP PROCEDURE [dbo].[sp_genformuladay]
GO



CREATE PROCEDURE [dbo].[sp_genformuladay]  AS

declare @id varchar(50)
declare @name varchar(50)
declare @exp varchar(500)
declare @intv varchar(50)
declare @starthour int

declare @sqlstr varchar(1000)
declare @tmpstr varchar(20)
declare @numstr varchar(20)
declare @tid int
declare @value varchar(50)
declare @mp varchar(50)
declare @type varchar(50)
declare @scadacode varchar(500)
declare @count int


Declare @starttime datetime
Declare @endtime datetime
Declare @tempdate datetime



set @tmpstr='()+-*/'


DECLARE rptfieldcs CURSOR FOR SELECT id,name, exp,intv,starthour FROM TB_FORMULA  where active = '启用' and intv = 'day' and  len(ltrim(rtrim(exp)))>0  order by morder


open rptfieldcs
FETCH next FROM rptfieldcs INTO @id,@name,@exp,@intv,@starthour

WHILE (@@FETCH_STATUS=0)
BEGIN

set @endtime = getdate();
set @endtime = dateadd(hh,-datepart(hh,@endtime),@endtime)
set @endtime = dateadd(ss,-datepart(ss,@endtime),@endtime)
set @endtime = dateadd(mi,-datepart(mi,@endtime),@endtime)
set @endtime = dateadd(ms,-datepart(ms,@endtime),@endtime)
set @endtime = dateadd(hh,@starthour,@endtime)

set @starttime = dateadd(day,-1,@endtime)

	if EXISTS (select * from TB_MeasurePoint where exp = @id)
	begin
	declare @sql1 nvarchar(1000)
	SET @sql1 = 'select @scadacode = MPointCode  from TB_MeasurePoint  where exp = '''+@id+''' '
	--print @sql1	
	exec sp_executesql @sql1,N'@scadacode varchar(50) output',@scadacode output
	end
	else
	begin
	set @scadacode = @name
	end

	
    set @sqlstr=''
    declare @cc varchar(20)
	set @exp = ltrim(rtrim(@exp))
    declare c1 cursor for
	select a from dbo.f_get_exp_split(@exp,@tmpstr)
    open c1
    fetch next from c1 into @cc
    while @@fetch_status=0
    begin
     if (charindex(@cc,@tmpstr)>0)
     begin
      set @sqlstr=@sqlstr+@cc
     end
     --else if (charindex(substring(@cc,1,1),@numstr)>0)
	else if (ISNUMERIC(@cc)=1)
    begin
     set @sqlstr=@sqlstr+cast(@cc as varchar(30))
     end
     else
     begin

	declare @sql2 nvarchar(1000)
	SET @sql2 = 'select  @mp = mp, @type = type from TB_FORMULADETAIL where pid = '''+@id+''' and name='''+@cc+''''
	--print @sql2	
	exec sp_executesql @sql2,N'@mp varchar(50) output,@type varchar(50) output',@mp output,@type output

	delete from tb_mp_temp_rpt

	while(@starttime<@endtime)
	begin
	set @tempdate = dateadd(day,1,@starttime)
	exec sp_getdata @starttime,@tempdate,@type,@mp
	set @starttime = @tempdate
	end
	
	set @starttime = dateadd(day,-1,@endtime)

	declare @sql3 nvarchar(1000)
	SET @sql3 = 'select top 1 @value = ParmValue from TB_MP_TEMP_rpt order by MeasureDT asc '
	--print @sql3	
	exec sp_executesql @sql3,N'@value varchar(50) output',@value output

	--print @value
	if (@value='-')
	begin
	set @sqlstr=@sqlstr+'0.0' --A A和B两选一
	set @value=0.0
	--set @sqlstr=null        --B A和B两选一
	end
	else
	begin
	set @sqlstr=@sqlstr+cast(@value as varchar(50))
	end
	--print @sqlstr

	declare @sql4 nvarchar(2000)
	SET @sql4 = N''

	set @sql4 = 'select @count = count(*) from TB_FORMULAPARM where MpointName = '''+@scadacode+''' and ParmName = '''+@cc+''' and MeasureDT='''+cast(@starttime as varchar(19))+''''
	exec sp_executesql @sql4,N'@count int output',@count output
	if (@count > 0)
	begin
	SET @sql4 = N''
	SET @sql4 = @sql4 +'update TB_FORMULAPARM set ParmValue =('+@value+') where  MpointName = '''+@scadacode+''' and ParmName = '''+@cc+''' and MeasureDT ='''+cast(@starttime as varchar(19))+''''
    	--set @sql4='update TB_MP_'+@scadacode+'_day set ParmValue =('+CONVERT(money,@sqlstr)+') where MeasureDT ='''+@starttime+''''
	end
	else	
	begin
	SET @sql4 = N''
	SET @sql4 = @sql4 +'insert into TB_FORMULAPARM (MpointName,ParmName,ParmValue,MeasureDT) values ('''+@scadacode+''','''+@cc+''','+@value+','''+cast(@starttime as varchar(19))+''')'
	--set @sql4='insert into TB_MP_'+@scadacode+'_day (ParmValue,MeasureDT) values ('+Cast(@sqlstr as money)+','''+@starttime+''')'
	end
    	--print @sql4
    	exec sp_executesql @sql4

     end
     fetch next from c1 into @cc
    end
    close c1
    deallocate c1

if(@sqlstr is null)
begin
set @sqlstr = '0.00'
end

	
	declare @sql5 nvarchar(2000)
	SET @sql5 = N''

	set @sql5 = 'select @count = count(*) from TB_MP_'+@scadacode+' where MeasureDT='''+cast(@starttime as varchar(19))+''''
	exec sp_executesql @sql5,N'@count int output',@count output
	if (@count > 0)
	begin
	SET @sql5 = N''
	SET @sql5 = @sql5 + 'update TB_MP_'+@scadacode+' set ParmValue =('+@sqlstr+') where MeasureDT ='''+cast(@starttime as varchar(19))+''''
    	--set @sql5='update TB_MP_'+@scadacode+'_day set ParmValue =('+CONVERT(money,@sqlstr)+') where MeasureDT ='''+@starttime+''''
	end
	else	
	begin
	SET @sql5 = N''
	SET @sql5 = @sql5 + 'insert into TB_MP_'+@scadacode+' (ParmValue,MeasureDT) values ('+@sqlstr+','''+cast(@starttime as varchar(19))+''')'
	--set @sql5='insert into TB_MP_'+@scadacode+'_day (ParmValue,MeasureDT) values ('+Cast(@sqlstr as money)+','''+@starttime+''')'
	end
    	--print @sql5
    	exec sp_executesql @sql5

	FETCH next FROM rptfieldcs INTO @id,@name,@exp,@intv,@starthour


end
CLOSE rptfieldcs
DEALLOCATE rptfieldcs


select * from tb_mp_temp


GO

-- ----------------------------
-- Procedure structure for sp_genformuladayX
-- ----------------------------
DROP PROCEDURE [dbo].[sp_genformuladayX]
GO



CREATE PROCEDURE [dbo].[sp_genformuladayX]  
@sdatein datetime,@edatein datetime
AS

declare @id varchar(50)
declare @name varchar(50)
declare @exp varchar(500)
declare @intv varchar(50)
declare @starthour int

declare @sqlstr varchar(1000)
declare @strsqldel nvarchar(1000)
declare @tmpstr varchar(20)
declare @numstr varchar(20)
declare @tid int
declare @value varchar(50)
declare @mp varchar(50)
declare @type varchar(50)
declare @scadacode varchar(500)
declare @count int


Declare @starttime datetime
Declare @endtime datetime
Declare @tempdate datetime



set @tmpstr='()+-*/'


DECLARE rptfieldcs CURSOR FOR SELECT id,name, exp,intv,starthour FROM TB_FORMULA  where active = '启用' and intv = 'day' and  len(ltrim(rtrim(exp)))>0  order by morder


open rptfieldcs
FETCH next FROM rptfieldcs INTO @id,@name,@exp,@intv,@starthour

WHILE (@@FETCH_STATUS=0)
BEGIN



set @endtime = @edatein
set @endtime = dateadd(hh,-datepart(hh,@endtime),@endtime)
set @endtime = dateadd(ss,-datepart(ss,@endtime),@endtime)
set @endtime = dateadd(mi,-datepart(mi,@endtime),@endtime)
set @endtime = dateadd(ms,-datepart(ms,@endtime),@endtime)
set @endtime = dateadd(hh,8,@endtime)

set @starttime = @sdatein
set @starttime = dateadd(hh,-datepart(hh,@starttime),@starttime)
set @starttime = dateadd(ss,-datepart(ss,@starttime),@starttime)
set @starttime = dateadd(mi,-datepart(mi,@starttime),@starttime)
set @starttime = dateadd(ms,-datepart(ms,@starttime),@starttime)
set @starttime = dateadd(hh,8,@starttime)

SET @strsqldel = N''
SET @strsqldel = @strsqldel + N'delete from tb_mp_' +  @name + N' where MeasureDT between'''+cast(@starttime as varchar)+N''' and '''+cast(@endtime as varchar)+N'''';
print @strsqldel
exec sp_executesql @strsqldel

SET @strsqldel = N''
SET @strsqldel = @strsqldel + N'delete from TB_FORMULAPARM where MeasureDT between'''+cast(@starttime as varchar)+N''' and '''+cast(@endtime as varchar)+N'''';
print @strsqldel
exec sp_executesql @strsqldel

if EXISTS (select * from TB_MeasurePoint where exp = @id)
	begin
	declare @sql1 nvarchar(1000)
	SET @sql1 = 'select @scadacode = MPointCode  from TB_MeasurePoint  where exp = '''+@id+''' '
	--print @sql1	
	exec sp_executesql @sql1,N'@scadacode varchar(50) output',@scadacode output
	end
	else
	begin
	set @scadacode = @name
	end

while(@starttime<=@endtime)

begin

set @tempdate = dateadd(day,1,@starttime)
	
    set @sqlstr=''
    declare @cc varchar(20)
	set @exp = ltrim(rtrim(@exp))
    declare c1 cursor for
	select a from dbo.f_get_exp_split(@exp,@tmpstr)
    open c1
    fetch next from c1 into @cc
    while @@fetch_status=0
    begin
     if (charindex(@cc,@tmpstr)>0)
     begin
      set @sqlstr=@sqlstr+@cc
     end
     --else if (charindex(substring(@cc,1,1),@numstr)>0)
	else if (ISNUMERIC(@cc)=1)
    begin
     set @sqlstr=@sqlstr+cast(@cc as varchar(30))
     end
     else
     begin

	declare @sql2 nvarchar(1000)
	SET @sql2 = 'select  @mp = mp, @type = type from TB_FORMULADETAIL where pid = '''+@id+''' and name='''+@cc+''''
	--print @sql2	
	exec sp_executesql @sql2,N'@mp varchar(50) output,@type varchar(50) output',@mp output,@type output

	delete from tb_mp_temp_rpt

	exec sp_getdata @starttime,@tempdate,@type,@mp
	
	declare @sql3 nvarchar(1000)
	SET @sql3 = 'select top 1 @value = ParmValue from TB_MP_TEMP_rpt order by MeasureDT asc '
	--print @sql3	
	exec sp_executesql @sql3,N'@value varchar(50) output',@value output

	--set @sqlstr=@sqlstr+cast(@value as varchar(50))

	--print @value
	if (@value='-')
	begin
	--set @sqlstr=@sqlstr+'0.0'
	set @value=0.0
	set @sqlstr=null
	end
	else
	begin
	set @sqlstr=@sqlstr+cast(@value as varchar(50))
	end
	--print @sqlstr

	declare @sql4 nvarchar(2000)
	SET @sql4 = N''

	SET @sql4 = 'insert into TB_FORMULAPARM (MpointName,ParmName,ParmValue,MeasureDT) values ('''+@scadacode+''','''+@cc+''','+@value+','''+cast(@starttime as varchar(19))+''')'
    	print @sql4
    	exec sp_executesql @sql4

     end
     fetch next from c1 into @cc
    end
    close c1
    deallocate c1

	
if(@sqlstr is null)
begin
set @sqlstr = '0.00'
end
	
	declare @sql5 nvarchar(2000)
	SET @sql5 = N''

	set @sql5='insert into TB_MP_'+@scadacode+' (ParmValue,MeasureDT) values ('+@sqlstr+','''+cast(@starttime as varchar(19))+''')'
	
    	print @sql5
    	exec sp_executesql @sql5

	set @starttime = @tempdate

end

	FETCH next FROM rptfieldcs INTO @id,@name,@exp,@intv,@starthour

end
CLOSE rptfieldcs
DEALLOCATE rptfieldcs




select * from tb_mp_temp


GO

-- ----------------------------
-- Procedure structure for sp_genformulaexp
-- ----------------------------
DROP PROCEDURE [dbo].[sp_genformulaexp]
GO



CREATE PROCEDURE [dbo].[sp_genformulaexp]  @stime datetime AS

declare @id varchar(50)
declare @name varchar(50)
declare @exp varchar(500)
declare @intv varchar(50)
declare @starthour int

declare @sqlstr varchar(1000)
declare @tmpstr varchar(20)
declare @numstr varchar(20)
declare @tid int
declare @value varchar(50)
declare @mp varchar(50)
declare @type varchar(50)
declare @scadacode varchar(500)
declare @count int



Declare @starttime datetime
Declare @endtime datetime
Declare @tempdate datetime



set @tmpstr='()+-*/'


DECLARE rptfieldcs CURSOR FOR SELECT id,name, exp,intv,starthour FROM TB_FORMULA  where active = '启用' and intv = 'day' and  len(ltrim(rtrim(exp)))>0  order by morder


open rptfieldcs
FETCH next FROM rptfieldcs INTO @id,@name,@exp,@intv,@starthour

WHILE (@@FETCH_STATUS=0)
BEGIN

set @starttime = @stime;
set @starttime = dateadd(hh,-datepart(hh,@starttime),@starttime)
set @starttime = dateadd(ss,-datepart(ss,@starttime),@starttime)
set @starttime = dateadd(mi,-datepart(mi,@starttime),@starttime)
set @starttime = dateadd(ms,-datepart(ms,@starttime),@starttime)
set @starttime = dateadd(hh,8,@starttime)

set @endtime = dateadd(day,1,@starttime)

	if EXISTS (select * from TB_MeasurePoint where exp = @id)
	begin
	declare @sql1 nvarchar(1000)
	SET @sql1 = 'select @scadacode = MPointCode  from TB_MeasurePoint  where exp = '''+@id+''' '
	--print @sql1	
	exec sp_executesql @sql1,N'@scadacode varchar(50) output',@scadacode output
	end
	else
	begin
	set @scadacode = @name
	end

	
    set @sqlstr=''
    declare @cc varchar(20)
	set @exp = ltrim(rtrim(@exp))
    declare c1 cursor for
	select a from dbo.f_get_exp_split(@exp,@tmpstr)
    open c1
    fetch next from c1 into @cc
    while @@fetch_status=0
    begin
     if (charindex(@cc,@tmpstr)>0)
     begin
      set @sqlstr=@sqlstr+@cc
     end
     --else if (charindex(substring(@cc,1,1),@numstr)>0)
	else if (ISNUMERIC(@cc)=1)
    begin
     set @sqlstr=@sqlstr+cast(@cc as varchar(30))
     end
     else
     begin

	declare @sql2 nvarchar(1000)
	SET @sql2 = 'select  @mp = mp, @type = type from TB_FORMULADETAIL where pid = '''+@id+''' and name='''+@cc+''''
	--print @sql2	
	exec sp_executesql @sql2,N'@mp varchar(50) output,@type varchar(50) output',@mp output,@type output

	delete from tb_mp_temp_rpt

	while(@starttime<@endtime)
	begin
	set @tempdate = dateadd(day,1,@starttime)
	exec sp_getdata @starttime,@tempdate,@type,@mp
	set @starttime = @tempdate
	end
	
	set @starttime = dateadd(day,-1,@endtime)

	declare @sql3 nvarchar(1000)
	SET @sql3 = 'select top 1 @value = ParmValue from TB_MP_TEMP_rpt order by MeasureDT asc '
	--print @sql3	
	exec sp_executesql @sql3,N'@value varchar(50) output',@value output

	--print @value
	if (@value='-')
	begin
	set @sqlstr=@sqlstr+'0.0' --A A和B两选一
	set @value=0.0
	--set @sqlstr=null        --B A和B两选一
	end
	else
	begin
	set @sqlstr=@sqlstr+cast(@value as varchar(50))
	end
	--print @sqlstr

	declare @sql4 nvarchar(2000)
	SET @sql4 = N''

	set @sql4 = 'select @count = count(*) from TB_FORMULAPARM where MpointName = '''+@scadacode+''' and ParmName = '''+@cc+''' and MeasureDT='''+cast(@starttime as varchar(19))+''''
	exec sp_executesql @sql4,N'@count int output',@count output
	if (@count > 0)
	begin
	SET @sql4 = N''
	SET @sql4 = @sql4 +'update TB_FORMULAPARM set ParmValue =('+@value+') where  MpointName = '''+@scadacode+''' and ParmName = '''+@cc+''' and MeasureDT ='''+cast(@starttime as varchar(19))+''''
    	--set @sql4='update TB_MP_'+@scadacode+'_day set ParmValue =('+CONVERT(money,@sqlstr)+') where MeasureDT ='''+@starttime+''''
	end
	else	
	begin
	SET @sql4 = N''
	SET @sql4 = @sql4 +'insert into TB_FORMULAPARM (MpointName,ParmName,ParmValue,MeasureDT) values ('''+@scadacode+''','''+@cc+''','+@value+','''+cast(@starttime as varchar(19))+''')'
	--set @sql4='insert into TB_MP_'+@scadacode+'_day (ParmValue,MeasureDT) values ('+Cast(@sqlstr as money)+','''+@starttime+''')'
	end
    	--print @sql4
    	exec sp_executesql @sql4

     end
     fetch next from c1 into @cc
    end
    close c1
    deallocate c1
	
if(@sqlstr is null)
begin
set @sqlstr = '0.00'
end
	declare @sql5 nvarchar(2000)
	SET @sql5 = N''

	set @sql5 = 'select @count = count(*) from TB_MP_'+@scadacode+' where MeasureDT='''+cast(@starttime as varchar(19))+''''
	exec sp_executesql @sql5,N'@count int output',@count output
	if (@count > 0)
	begin
	SET @sql5 = N''
	SET @sql5 = @sql5 + 'update TB_MP_'+@scadacode+' set ParmValue =('+@sqlstr+') where MeasureDT ='''+cast(@starttime as varchar(19))+''''
    	--set @sql5='update TB_MP_'+@scadacode+'_day set ParmValue =('+CONVERT(money,@sqlstr)+') where MeasureDT ='''+@starttime+''''
	end
	else	
	begin
	SET @sql5 = N''
	SET @sql5 = @sql5 + 'insert into TB_MP_'+@scadacode+' (ParmValue,MeasureDT) values ('+@sqlstr+','''+cast(@starttime as varchar(19))+''')'
	--set @sql5='insert into TB_MP_'+@scadacode+'_day (ParmValue,MeasureDT) values ('+Cast(@sqlstr as money)+','''+@starttime+''')'
	end
    	--print @sql5
    	exec sp_executesql @sql5

	FETCH next FROM rptfieldcs INTO @id,@name,@exp,@intv,@starthour

end
CLOSE rptfieldcs
DEALLOCATE rptfieldcs


select * from tb_mp_temp_rpt  order by itemid,insdt


GO

-- ----------------------------
-- Procedure structure for sp_genformulamthrpt
-- ----------------------------
DROP PROCEDURE [dbo].[sp_genformulamthrpt]
GO



CREATE PROCEDURE [dbo].[sp_genformulamthrpt]
@stime varchar(10),  @starthour int,  @intval int 
 AS
declare @starttime datetime
declare @endtime datetime
declare @tempdate datetime

declare @name varchar(50)

Select @starttime=cast(@stime as datetime)
if(DATEPART(day,@starttime))>1
begin
	set @starttime = cast (cast(DATEPART(yyyy,@starttime)  as varchar)+'-'+cast(DATEPART(mm,@starttime)  as varchar)+'-01' as datetime)
	Select @starttime=DateAdd(hour,@starthour,@starttime) 
end
else
begin
	set @starttime = dateadd(month,-1,@starttime)
	set @starttime = cast (cast(DATEPART(yyyy,@starttime)  as varchar)+'-'+cast(DATEPART(mm,@starttime)  as varchar)+'-01' as datetime)
	Select @starttime=DateAdd(hour,@starthour,@starttime) 
end 
	Select @endtime=DateAdd(month,1,@starttime) 

print @starttime

delete from tb_mp_temp_rpt

while(@starttime<@endtime)
begin
	set @tempdate = dateadd(hour,@intval,@starttime)

	insert into TB_MP_TEMP_RPT(ParmValue,MeasureDT) values(SUBSTRING(convert(varchar(100),@starttime,120),0,11),@starttime)

DECLARE rptfieldcs CURSOR FOR SELECT name FROM TB_FORMULA  where active = '启用' and intv = 'day' and  len(ltrim(rtrim(exp)))>0  order by morder


open rptfieldcs
FETCH next FROM rptfieldcs INTO @name

WHILE (@@FETCH_STATUS=0)
BEGIN

 exec sp_getdata @starttime,@tempdate,'first',@name


FETCH next FROM rptfieldcs INTO @name

END
CLOSE rptfieldcs
DEALLOCATE rptfieldcs

	set @starttime = @tempdate
end

Select * from TB_MP_TEMP_RPT order by itemid,insdt


GO

-- ----------------------------
-- Procedure structure for sp_genformulaparm
-- ----------------------------
DROP PROCEDURE [dbo].[sp_genformulaparm]
GO



CREATE PROCEDURE [dbo].[sp_genformulaparm]  @stime datetime AS

declare @id varchar(50)
declare @name varchar(50)
declare @memo varchar(50)
declare @exp varchar(500)
declare @rate money
declare @intv varchar(50)
declare @starthour int

declare @sqlstr varchar(1000)
declare @tmpstr varchar(20)
declare @numstr varchar(20)
declare @tid int
declare @av1 money
declare @av2 money
declare @mp varchar(50)
declare @mpname varchar(50)
declare @type varchar(50)
declare @scadacode varchar(500)
declare @count int


Declare @starttime datetime
Declare @endtime datetime
Declare @tempdate datetime



set @tmpstr='()+-*/'

delete from tb_mp_temp_rpt

DECLARE rptfieldcs CURSOR FOR SELECT id,name,memo,exp,rate,intv,starthour FROM TB_FORMULA  where active = '启用' and intv = 'day' and  len(ltrim(rtrim(exp)))>0 order by morder


open rptfieldcs
FETCH next FROM rptfieldcs INTO @id,@name,@memo,@exp,@rate,@intv,@starthour

WHILE (@@FETCH_STATUS=0)
BEGIN

set @starttime = @stime;
set @starttime = dateadd(hh,-datepart(hh,@starttime),@starttime)
set @starttime = dateadd(ss,-datepart(ss,@starttime),@starttime)
set @starttime = dateadd(mi,-datepart(mi,@starttime),@starttime)
set @starttime = dateadd(ms,-datepart(ms,@starttime),@starttime)
set @starttime = dateadd(hh,8,@starttime)


insert into TB_MP_TEMP_RPT(ParmValue,MeasureDT) values(@memo+':',@starttime)

if EXISTS (select * from TB_MeasurePoint where exp = @id)
	begin
	declare @sql1 nvarchar(1000)
	SET @sql1 = 'select @scadacode = MPointCode  from TB_MeasurePoint  where exp = '''+@id+''' '
	--print @sql1	
	exec sp_executesql @sql1,N'@scadacode varchar(50) output',@scadacode output
	end
	else
	begin
	set @scadacode = @name
	end

declare @sql5 nvarchar(2000)
	SET @sql5 = N''

	set @sql5 = 'select  top 1 @av2=ParmValue from TB_MP_'+@scadacode+' where MeasureDT='''+cast(@starttime as varchar(19))+''''
	exec sp_executesql @sql5,N'@av2 money output',@av2 output
	
	if @av2 IS NULL
		begin
			--insert into TB_MP_TEMP_RPT(ParmValue,MeasureDT) values('参数值:',@starttime)
			insert into TB_MP_TEMP_RPT(ParmValue,MeasureDT) values('-',@starttime)
		end
		else 
		begin
			--insert into TB_MP_TEMP_RPT(ParmValue,MeasureDT) values('参数值:',@starttime)
			insert into TB_MP_TEMP_RPT(ParmValue,MeasureDT) values(cast((@av2*@rate) as varchar),@starttime)
		end

insert into TB_MP_TEMP_RPT(ParmValue,MeasureDT) values('br',@starttime)
insert into TB_MP_TEMP_RPT(ParmValue,MeasureDT) values('str',@starttime)
insert into TB_MP_TEMP_RPT(ParmValue,MeasureDT) values(@exp,@starttime)
insert into TB_MP_TEMP_RPT(ParmValue,MeasureDT) values('br',@starttime)
insert into TB_MP_TEMP_RPT(ParmValue,MeasureDT) values('str',@starttime)

	


	

	
    set @sqlstr=''
    declare @cc varchar(20)
	set @exp = ltrim(rtrim(@exp))
    declare c1 cursor for
	select a from dbo.f_get_exp_split(@exp,@tmpstr)
    open c1
    fetch next from c1 into @cc
    while @@fetch_status=0
    begin
     if (charindex(@cc,@tmpstr)>0)
	fetch next from c1 into @cc
    
	else if (ISNUMERIC(@cc)=1)
	fetch next from c1 into @cc
     else
     begin

	declare @sql2 nvarchar(1000)
	--SET @sql2 = 'select  @mp = mp  from TB_FORMULADETAIL where pid = '''+@id+''' and name='''+@cc+''''
	SET @sql2 = 'select @mpname = memo, @mp = mp  from TB_FORMULADETAIL where pid = '''+@id+''' and name='''+@cc+''''
	--print @sql2
	--exec sp_executesql @sql2,N'@mp varchar(50) output',@mp output
	exec sp_executesql @sql2,N'@mpname varchar(50) output,@mp varchar(50) output',@mpname output,@mp output

	--declare @sql3 nvarchar(1000)
	--SET @sql3 = 'select  @mpname = ParmName  from TB_MeasurePoint where MPointCode = '''+@mp+''''
	--print @sql3
	--exec sp_executesql @sql3,N'@mpname varchar(50) output',@mpname output


	declare @sql4 nvarchar(2000)
	SET @sql4 = N''

	set @sql4 = 'select top 1 @av1=ParmValue from TB_FORMULAPARM where MpointName = '''+@scadacode+''' and ParmName = '''+@cc+''' and MeasureDT='''+cast(@starttime as varchar(19))+''''
	exec sp_executesql @sql4,N'@av1 money output',@av1 output

	if @av1 IS NULL
		begin
			insert into TB_MP_TEMP_RPT(ParmValue,MeasureDT) values(@cc+'['+@mpname+']:',@starttime)
			--insert into TB_MP_TEMP_RPT(ParmValue,MeasureDT) values('-',@starttime)
		end
		else 
		begin
			insert into TB_MP_TEMP_RPT(ParmValue,MeasureDT) values(@cc+'['+@mpname+']:'+cast(@av1 as varchar),@starttime)
			--insert into TB_MP_TEMP_RPT(ParmValue,MeasureDT) values(cast(@av1 as varchar),@starttime)
		end
insert into TB_MP_TEMP_RPT(ParmValue,MeasureDT) values('br',@starttime)
insert into TB_MP_TEMP_RPT(ParmValue,MeasureDT) values('str',@starttime)

fetch next from c1 into @cc

     end
     
    end
    close c1
    deallocate c1

	

FETCH next FROM rptfieldcs INTO @id,@name,@memo,@exp,@rate,@intv,@starthour
insert into TB_MP_TEMP_RPT(ParmValue,MeasureDT) values('br',@starttime)
	
end
CLOSE rptfieldcs
DEALLOCATE rptfieldcs


Select * from TB_MP_TEMP_RPT order by itemid,insdt


GO

-- ----------------------------
-- Procedure structure for sp_getdata
-- ----------------------------
DROP PROCEDURE [dbo].[sp_getdata]
GO



CREATE PROCEDURE [dbo].[sp_getdata]
@starttime varchar(20),@endtime varchar(20),@type varchar(80),@mp varchar(80)
as
Declare @strSQL nvarchar(4000)
declare @v money
declare @s money
declare @v1 money
declare @v2 money

	if(@mp!='-')
	begin
		if(@type='first') 
		begin
			SET @strSQL = N''
			SET @strSQL = @strSQL + N'select top 1 @av=ParmValue  from tb_mp_' + @mp+N' ' +char(10)
			SET @strSQL = @strSQL + N'where measuredt >= ''' +cast(@starttime as varchar(20)) +N''' and measuredt < '''+cast(@endtime as varchar(20))+N''' order by MeasureDT asc'+char(10)
			print @strsql
			exec sp_executesql @strsql,N'@av money output',@v output
		end
		if(@type='last') 
		begin
			SET @strSQL = N''
			SET @strSQL = @strSQL + N'select top 1 @av=ParmValue from tb_mp_' + @mp+N' ' +char(10)
			SET @strSQL = @strSQL + N'where measuredt >= ''' +cast(@starttime as varchar(20)) +N''' and measuredt < '''+cast(@endtime as varchar(20))+N''' order by MeasureDT desc'+char(10)
			print @strsql
			exec sp_executesql @strsql,N'@av money output',@v output
		end
		if(@type='avg') 
		begin
			SET @strSQL = N''
			SET @strSQL = @strSQL + N'select  @av=avg(ParmValue) from tb_mp_' + @mp+N' ' +char(10)
			SET @strSQL = @strSQL + N'where measuredt between ''' +cast(@starttime as varchar(20)) +N''' and '''+cast(@endtime as varchar(20))+N''' and parmvalue <> 0 '+char(10)
			print @strsql
			exec sp_executesql @strsql,N'@av money output',@v output
		end
		if(@type='min') 
		begin
			SET @strSQL = N''
			SET @strSQL = @strSQL + N'select  @av=min(ParmValue) from tb_mp_' + @mp+N' ' +char(10)
			SET @strSQL = @strSQL + N'where measuredt between ''' +cast(@starttime as varchar(20)) +N''' and '''+cast(@endtime as varchar(20))+N''''+char(10)
			print @strsql
			exec sp_executesql @strsql,N'@av money output',@v output
		end
		if(@type='max') 
		begin
			SET @strSQL = N''
			SET @strSQL = @strSQL + N'select  @av=max(ParmValue) from tb_mp_' + @mp+N' ' +char(10)
			SET @strSQL = @strSQL + N'where measuredt between ''' +cast(dateadd(minute,5,@starttime) as varchar(20)) +N''' and '''+cast(@endtime as varchar(20))+N''''+char(10)
			print @strsql
			exec sp_executesql @strsql,N'@av money output',@v output
		end
		if(@type='diff') 
		begin
			SET @strSQL = N''
			SET @strSQL = @strSQL + N'select top 1 @av=parmvalue from tb_mp_' + @mp+N' ' +char(10)
			SET @strSQL = @strSQL + N'where measuredt >= ''' +cast(@starttime as varchar(20)) +N''' and measuredt < '''+cast(@endtime as varchar(20))+N''' order by measuredt asc'+char(10)
			print @strsql
			exec sp_executesql @strsql,N'@av money output',@v1 output
			print cast(@v1 as varchar(20))

			SET @strSQL = N''
			SET @strSQL = @strSQL + N'select top 1 @av=parmvalue from tb_mp_' + @mp+N' ' +char(10)
			SET @strSQL = @strSQL + N'where measuredt >= ''' +cast(dateadd(day,1,@starttime) as varchar(20)) +N''' and measuredt < '''+cast(dateadd(day,1,@endtime) as varchar(20))+N''' order by measuredt asc'+char(10)
			print @strsql
			exec sp_executesql @strsql,N'@av money output',@v2 output
			print cast(@v2 as varchar(20))
			
			set @s = @v2 - @v1
			if(@s is not null)
			begin
				if(@s<0)
				begin
					set @v = null
				end
				else
				begin
					set @v = @s
				end
			end
			else
				set @v = null
			
		end
		if(@type='yes_last_hour_diff') 
		begin
			SET @strSQL = N''
			SET @strSQL = @strSQL + N'select top 1 @av=parmvalue from tb_mp_' + @mp+N' ' +char(10)
			SET @strSQL = @strSQL + N'where measuredt >= ''' +cast(dateadd(hour,-1,@starttime) as varchar(20)) +N''' and measuredt < '''+cast(dateadd(hour,-1,@endtime) as varchar(20))+N''' order by measuredt asc'+char(10)
			print @strsql
			exec sp_executesql @strsql,N'@av money output',@v1 output
			print cast(@v1 as varchar(20))

			SET @strSQL = N''
			SET @strSQL = @strSQL + N'select top 1 @av=parmvalue from tb_mp_' + @mp+N' ' +char(10)
			SET @strSQL = @strSQL + N'where measuredt >= ''' +cast(@starttime as varchar(20)) +N''' and measuredt < '''+cast(@endtime as varchar(20))+N''' order by measuredt asc'+char(10)
			print @strsql
			exec sp_executesql @strsql,N'@av money output',@v2 output
			print cast(@v2 as varchar(20))
			
			set @s = @v2 - @v1
			if(@s is not null)
			begin
				if(@s<0)
				begin
					set @v = null
				end
				else
				begin
					set @v = @s
				end
			end
			else
				set @v = null
			
		end
		if(@type='diff1') 
		begin
			SET @strSQL = N''
			SET @strSQL = @strSQL + N'select top 1 @av=parmvalue from tb_mp_' + @mp+N' ' +char(10)
			SET @strSQL = @strSQL + N'where measuredt >= ''' +cast(@starttime as varchar(20)) +N''' and measuredt < '''+cast(@endtime as varchar(20))+N''' order by measuredt asc'+char(10)
			print @strsql
			exec sp_executesql @strsql,N'@av money output',@v1 output
			print cast(@v1 as varchar(20))

			SET @strSQL = N''
			SET @strSQL = @strSQL + N'select top 1 @av=parmvalue from tb_mp_' + @mp+N' ' +char(10)
			SET @strSQL = @strSQL + N'where measuredt >= ''' +cast(dateadd(day,1,@starttime) as varchar(20)) +N''' and measuredt < '''+cast(dateadd(day,1,@endtime) as varchar(20))+N''' order by measuredt asc'+char(10)
			print @strsql
			exec sp_executesql @strsql,N'@av money output',@v2 output
			print cast(@v2 as varchar(20))
			
			set @s = @v2 - @v1
			--if(@s is not null)
			--begin
			--	if(@s<0)
			--	begin
			--		set @v = null
			--	end
			--	else
			--	begin
					set @v = @s
			--	end
			--end
			--else
			--	set @v = null
			
		end
		if(@type='diff24') 
		begin
			SET @strSQL = N''
			SET @strSQL = @strSQL + N'select top 1 @av=parmvalue from tb_mp_' + @mp+N' ' +char(10)
			SET @strSQL = @strSQL + N'where measuredt >= ''' +cast(@starttime as varchar(20)) +N''' and measuredt < '''+cast(@endtime as varchar(20))+N''' order by measuredt asc'+char(10)
			print @strsql
			exec sp_executesql @strsql,N'@av money output',@v1 output
			print cast(@v1 as varchar(20))

			SET @strSQL = N''
			SET @strSQL = @strSQL + N'select top 1 @av=parmvalue from tb_mp_' + @mp+N' ' +char(10)
			SET @strSQL = @strSQL + N'where measuredt >= ''' +cast(dateadd(day,1,@starttime) as varchar(20)) +N''' and measuredt < '''+cast(dateadd(day,1,@endtime) as varchar(20))+N''' order by measuredt asc'+char(10)
			print @strsql
			exec sp_executesql @strsql,N'@av money output',@v2 output
			print cast(@v2 as varchar(20))
			
			set @s = @v2 - @v1
			if(@s is not null)
			begin
				if(@s<0)
				begin
					set @v = null
				end
			    else if (@s > 24)
				begin
				   set @s = 24 
				end
				
				set @v = @s
				
			end
			else
				set @v = null
			
		end
		if(@type='diff60') 
		begin
			SET @strSQL = N''
			SET @strSQL = @strSQL + N'select top 1 @av=parmvalue from tb_mp_' + @mp+N' ' +char(10)
			SET @strSQL = @strSQL + N'where measuredt >= ''' +cast(@starttime as varchar(20)) +N''' and measuredt < '''+cast(@endtime as varchar(20))+N''' order by measuredt asc'+char(10)
			print @strsql
			exec sp_executesql @strsql,N'@av money output',@v1 output
			print cast(@v1 as varchar(20))

			SET @strSQL = N''
			SET @strSQL = @strSQL + N'select top 1 @av=parmvalue from tb_mp_' + @mp+N' ' +char(10)
			SET @strSQL = @strSQL + N'where measuredt >= ''' +cast(dateadd(day,1,@starttime) as varchar(20)) +N''' and measuredt < '''+cast(dateadd(day,1,@endtime) as varchar(20))+N''' order by measuredt asc'+char(10)
			print @strsql
			exec sp_executesql @strsql,N'@av money output',@v2 output
			print cast(@v2 as varchar(20))
			
			set @s = @v2 - @v1
			if(@s is not null)
			begin
				if(@s<0)
				begin
					set @v = null
				end
				else
				begin
					set @v = @s/60
				end
			end
			else
				set @v = null
			
		end
		if(@type='sum')
		begin
			SET @strSQL = N''
			SET @strSQL = @strSQL + N'select  @av=sum(ParmValue) from tb_mp_' + @mp+N' ' +char(10)
			SET @strSQL = @strSQL + N'where measuredt >= ''' +cast(@starttime as varchar(20)) +N''' and measuredt< '''+cast(@endtime as varchar(20))+N''''+char(10)
			print @strsql
			exec sp_executesql @strsql,N'@av money output',@v output
		end
		if(@type='time')
		begin
			SET @strSQL = N''
			SET @strSQL = @strSQL + N'select top 1 @av=parmvalue from tb_mp_' + @mp+N' ' +char(10)
			SET @strSQL = @strSQL + N'where measuredt >= ''' +cast(@starttime as varchar(20)) +N''' and measuredt< '''+cast(@endtime as varchar(20))+N''' order by measuredt asc'+char(10)
			print @strsql
			exec sp_executesql @strsql,N'@av money output',@v1 output
			print cast(@v1 as varchar(20))

			SET @strSQL = N''
			SET @strSQL = @strSQL + N'select top 1 @av=parmvalue from tb_mp_' + @mp+N' ' +char(10)
			SET @strSQL = @strSQL + N'where measuredt >= ''' +cast(dateadd(day,1,@starttime) as varchar(20)) +N''' and measuredt< '''+cast(dateadd(day,1,@endtime) as varchar(20))+N''' order by measuredt asc'+char(10)
			print @strsql
			exec sp_executesql @strsql,N'@av money output',@v2 output
			print cast(@v2 as varchar(20))
			
			set @s = @v2*60 - @v1*60
			if(@s is not null)
			begin
				if(@s<0)
				begin
					set @v = null
				end
				else if((@v2*60 - @v1*60) >= 1440 )
				begin
					set @v = 24
				end
				else
				begin
					set @v = (@v2*60 - @v1*60)/60.0
				end			
			end
			else
				set @v = null
		end	
		if(@type='timem') 
		begin
			SET @strSQL = N''
			SET @strSQL = @strSQL + N'select top 1 @av=parmvalue from tb_mp_' + @mp+N' ' +char(10)
			SET @strSQL = @strSQL + N'where measuredt >= ''' +cast(@starttime as varchar(20)) +N''' and measuredt< '''+cast(@endtime as varchar(20))+N''' order by measuredt asc'+char(10)
			print @strsql
			exec sp_executesql @strsql,N'@av money output',@v1 output
			print cast(@v1 as varchar(20))

			SET @strSQL = N''
			SET @strSQL = @strSQL + N'select top 1 @av=parmvalue from tb_mp_' + @mp+N' ' +char(10)
			SET @strSQL = @strSQL + N'where measuredt >= ''' +cast(dateadd(day,1,@starttime) as varchar(20)) +N''' and measuredt< '''+cast(dateadd(day,1,@endtime) as varchar(20))+N''' order by measuredt asc'+char(10)
			print @strsql
			exec sp_executesql @strsql,N'@av money output',@v2 output
			print cast(@v2 as varchar(20))
			
			set @s = @v2 - @v1
			if(@s is not null)
			begin
				if(@s<0)
				begin
					set @v = null
				end
				else
				begin
					set @v = @s/60.0
				end
			end
			else
				set @v = null
			
		end	

		if @v IS NULL
		begin
			print '-'
			insert into TB_MP_TEMP_RPT(ParmValue,MeasureDT) values('-',@starttime)
		end
		else 
		begin
			print 'insert into TB_MP_TEMP_RPT(ParmValue,MeasureDT) values('''+cast(@v as varchar)+''','''+@starttime+''')'
			insert into TB_MP_TEMP_RPT(ParmValue,MeasureDT) values(cast(@v as varchar),@starttime)
		end

	end

	else
	begin
		print '-'

		insert into TB_MP_TEMP_RPT(ParmValue,MeasureDT) values('-',@starttime)
	end


GO

-- ----------------------------
-- Procedure structure for sp_getfundata
-- ----------------------------
DROP PROCEDURE [dbo].[sp_getfundata]
GO



CREATE PROCEDURE [dbo].[sp_getfundata]
@starttime varchar(20),@endtime varchar(20),@type varchar(80),@mp varchar(200)
as
Declare @strSQL nvarchar(4000)
declare @v money
declare @s money
declare @vtemp money
declare @mptemp varchar(80)
declare @count int
declare @i int
declare @v1 money
declare @v2 money
declare @sdate datetime
declare @edate datetime
declare @tdate datetime
declare @n int

Select @sdate=cast(@starttime as datetime)
Select @edate=cast(@endtime as datetime)

select @count = count(*) from dbo.FUN_split(@mp,',') 
set @i = 1
set @s = 0
set @n = 0
	if(@mp!='-')
	begin

		DECLARE curRunStatus cursor for 
		select * from dbo.FUN_split(@mp,',') 
		open curRunStatus

		while(@i<=@count)
		begin

		FETCH curRunStatus into @mptemp
		
		if(@type='first') 
		begin
			SET @strSQL = N''
			SET @strSQL = @strSQL + N'select top 1 @av=ParmValue  from tb_mp_' + @mptemp+N' ' +char(10)
			SET @strSQL = @strSQL + N'where measuredt >= ''' +cast(@starttime as varchar(20)) +N''' and measuredt < '''+cast(@endtime as varchar(20))+N''' order by MeasureDT asc'+char(10)
			print @strsql
			exec sp_executesql @strsql,N'@av money output',@vtemp output

			if(@vtemp is not null)
			set @s = @s + @vtemp
		end
		if(@type='last') 
		begin
			SET @strSQL = N''
			SET @strSQL = @strSQL + N'select top 1 @av=ParmValue from tb_mp_' + @mptemp+N' ' +char(10)
			SET @strSQL = @strSQL + N'where measuredt >= ''' +cast(@starttime as varchar(20)) +N''' and measuredt < '''+cast(@endtime as varchar(20))+N''' order by MeasureDT desc'+char(10)
			print @strsql
			exec sp_executesql @strsql,N'@av money output',@vtemp output

			if(@vtemp is not null)
			set @s = @s + @vtemp
		end
		if(@type='avg') 
		begin
			SET @strSQL = N''
			SET @strSQL = @strSQL + N'select  @av=avg(ParmValue) from tb_mp_' + @mptemp+N' ' +char(10)
			SET @strSQL = @strSQL + N'where measuredt between ''' +cast(@starttime as varchar(20)) +N''' and '''+cast(@endtime as varchar(20))+N''' and parmvalue <> 0 '+char(10)
			print @strsql
			exec sp_executesql @strsql,N'@av money output',@vtemp output

			if(@vtemp is not null)
			set @s = @s + @vtemp
		end
		if(@type='min') 
		begin
			SET @strSQL = N''
			SET @strSQL = @strSQL + N'select  @av=min(ParmValue) from tb_mp_' + @mptemp+N' ' +char(10)
			SET @strSQL = @strSQL + N'where measuredt between ''' +cast(@starttime as varchar(20)) +N''' and '''+cast(@endtime as varchar(20))+N''''+char(10)
			print @strsql
			exec sp_executesql @strsql,N'@av money output',@vtemp output

			if(@vtemp is not null)
			set @s = @s + @vtemp
		end
		if(@type='max') 
		begin
			SET @strSQL = N''
			SET @strSQL = @strSQL + N'select  @av=max(ParmValue) from tb_mp_' + @mptemp+N' ' +char(10)
			SET @strSQL = @strSQL + N'where measuredt between ''' +cast(@starttime as varchar(20)) +N''' and '''+cast(@endtime as varchar(20))+N''''+char(10)
			print @strsql
			exec sp_executesql @strsql,N'@av money output',@vtemp output

			if(@vtemp is not null)
			set @s = @s + @vtemp
		end
		if(@type='diff') 
		begin
			SET @strSQL = N''
			SET @strSQL = @strSQL + N'select top 1 @av=parmvalue from tb_mp_' + @mptemp+N' ' +char(10)
			SET @strSQL = @strSQL + N'where measuredt >= ''' +cast(@starttime as varchar(20)) +N''' and measuredt < '''+cast(@endtime as varchar(20))+N''' order by measuredt asc'+char(10)
			print @strsql
			exec sp_executesql @strsql,N'@av money output',@v1 output
			print cast(@v1 as varchar(20))

			SET @strSQL = N''
			SET @strSQL = @strSQL + N'select top 1 @av=parmvalue from tb_mp_' + @mptemp+N' ' +char(10)
			SET @strSQL = @strSQL + N'where measuredt >= ''' +cast(dateadd(day,1,@starttime) as varchar(20)) +N''' and measuredt < '''+cast(dateadd(day,1,@endtime) as varchar(20))+N''' order by measuredt asc'+char(10)
			print @strsql
			exec sp_executesql @strsql,N'@av money output',@v2 output
			print cast(@v2 as varchar(20))
			
			set @vtemp = @v2 - @v1
			if(@vtemp is not null)
			begin
				if(@vtemp<0)
				begin
					set @s = @s
				end
				else
				begin
					set @s = @s + @vtemp
				end		
			end
			else
				set @s = @s
			
		end		
		if(@type='sum') 
		begin
			SET @strSQL = N''
			SET @strSQL = @strSQL + N'select  @av=sum(ParmValue) from tb_mp_' + @mptemp+N' ' +char(10)
			SET @strSQL = @strSQL + N'where measuredt >= ''' +cast(@starttime as varchar(20)) +N''' and measuredt< '''+cast(@endtime as varchar(20))+N''''+char(10)
			print @strsql
			exec sp_executesql @strsql,N'@av money output',@vtemp output

			if(@vtemp is not null)
			set @s = @s + @vtemp
		end
		if(@type='time')
		begin
			SET @strSQL = N''
			SET @strSQL = @strSQL + N'select top 1 @av=parmvalue from tb_mp_' + @mp+N' ' +char(10)
			SET @strSQL = @strSQL + N'where measuredt >= ''' +cast(@starttime as varchar(20)) +N''' and measuredt< '''+cast(@endtime as varchar(20))+N''' order by measuredt asc'+char(10)
			print @strsql
			exec sp_executesql @strsql,N'@av money output',@v1 output
			print cast(@v1 as varchar(20))

			SET @strSQL = N''
			SET @strSQL = @strSQL + N'select top 1 @av=parmvalue from tb_mp_' + @mp+N' ' +char(10)
			SET @strSQL = @strSQL + N'where measuredt >= ''' +cast(dateadd(day,1,@starttime) as varchar(20)) +N''' and measuredt< '''+cast(dateadd(day,1,@endtime) as varchar(20))+N''' order by measuredt asc'+char(10)
			print @strsql
			exec sp_executesql @strsql,N'@av money output',@v2 output
			print cast(@v2 as varchar(20))
			
			set @vtemp = @v2*60 - @v1*60
			if(@vtemp is not null)
			begin
				if(@vtemp<0)
				begin
					set @s = @s
				end
				else if((@v2*60 - @v1*60) >= 1440 )
				begin
					set @s = @s+24
				end
				else
				begin
					set @s = @s+(@v2*60 - @v1*60)/60.0
				end			
			end
			else
				set @s = @s
		end
		if(@type='timem') --传入参数是分钟，返回是小时
		begin
			SET @strSQL = N''
			SET @strSQL = @strSQL + N'select top 1 @av=parmvalue from tb_mp_' + @mptemp+N' ' +char(10)
			SET @strSQL = @strSQL + N'where measuredt >= ''' +cast(@starttime as varchar(20)) +N''' and measuredt< '''+cast(@endtime as varchar(20))+N''' order by measuredt asc'+char(10)
			print @strsql
			exec sp_executesql @strsql,N'@av money output',@v1 output
			print cast(@v1 as varchar(20))

			SET @strSQL = N''
			SET @strSQL = @strSQL + N'select top 1 @av=parmvalue from tb_mp_' + @mptemp+N' ' +char(10)
			SET @strSQL = @strSQL + N'where measuredt >= ''' +cast(dateadd(day,1,@starttime) as varchar(20)) +N''' and measuredt< '''+cast(dateadd(day,1,@endtime) as varchar(20))+N''' order by measuredt asc'+char(10)
			print @strsql
			exec sp_executesql @strsql,N'@av money output',@v2 output
			print cast(@v2 as varchar(20))
			
			set @vtemp = @v2 - @v1
			if(@vtemp is not null)
			begin
				if(@vtemp<0)
				begin
					set @s = @s
				end
				else
				begin
					set @s = @s + @vtemp/60.0
				end		
			end
			else
				set @s = @s
			
		end	
		if(@type='run') 
		begin
			SET @strSQL = N''
			SET @strSQL = @strSQL + N'select top 1 @av=ParmValue  from tb_mp_' + @mptemp+N' ' +char(10)
			SET @strSQL = @strSQL + N'where measuredt >= ''' +cast(@starttime as varchar(20)) +N''' and measuredt< '''+cast(@endtime as varchar(20))+N''' order by MeasureDT asc'+char(10)
			print @strsql
			exec sp_executesql @strsql,N'@av money output',@vtemp output

			if(@vtemp is not null)
			set @s = @s + @vtemp
		end	
		if(@type='mthavg') --只适用于求月报平均，并且与日报的平均值相等时，日报的平均值是用excel的平均函数求出的
		begin
			while(@sdate<@edate)
			begin
				set @tdate = dateadd(hour,2,@sdate)

				SET @strSQL = N''
				SET @strSQL = @strSQL + N'select top 1 @av=ParmValue  from tb_mp_' + @mptemp+N' ' +char(10)
				SET @strSQL = @strSQL + N'where measuredt between ''' +cast(@sdate as varchar(20)) +N''' and '''+cast(@edate as varchar(20))+N''' order by MeasureDT asc'+char(10)
				print @strsql
				exec sp_executesql @strsql,N'@av money output',@vtemp output

				if(@vtemp is not null)
				begin
					set @s = @s + @vtemp
					set @n = @n+1
				end
				else
					set @s = @s

				set @sdate = @tdate
			end

			if(@n>0)
				set @s = @s/@n
			else
				set @s = @s

		end
		if(@type='diffmax') --根据总累计，求出时间段内日的最高累计值
		begin
			while(@sdate<@edate)
			begin
				set @tdate = dateadd(day,1,@sdate)

				SET @strSQL = N''
				SET @strSQL = @strSQL + N'select top 1 @av=ParmValue  from tb_mp_' + @mptemp+N' ' +char(10)
				SET @strSQL = @strSQL + N'where measuredt >= ''' +cast(@sdate as varchar(20)) +N''' and measuredt< '''+cast(@edate as varchar(20))+N''' order by MeasureDT asc'+char(10)
				print @strsql
				exec sp_executesql @strsql,N'@av money output',@v1 output
				print cast(@v1 as varchar(20))

				SET @strSQL = N''
				SET @strSQL = @strSQL + N'select top 1 @av=parmvalue from tb_mp_' + @mptemp+N' ' +char(10)
				SET @strSQL = @strSQL + N'where measuredt >= ''' +cast(dateadd(day,1,@sdate) as varchar(20)) +N''' and measuredt< '''+cast(dateadd(day,1,@edate) as varchar(20))+N''' order by measuredt asc'+char(10)
				print @strsql
				exec sp_executesql @strsql,N'@av money output',@v2 output
				print cast(@v2 as varchar(20))

				set @vtemp = @v2 - @v1
				if(@vtemp is not null)
				begin
					if(@vtemp<0)
					begin
						set @s = @s
					end
					else
					begin
						if(@s is null or @s<@vtemp)
						set @s=@vtemp
					end		
				end
				else
					set @s = @s

				set @sdate = @tdate
			end

		end
		if(@type='firstavg') 
		begin
			SET @strSQL = N''
			SET @strSQL = @strSQL + N'select top 1 @av=ParmValue  from tb_mp_' + @mptemp+N' ' +char(10)
			SET @strSQL = @strSQL + N'where measuredt between ''' +cast(@starttime as varchar(20)) +N''' and '''+cast(@endtime as varchar(20))+N''' order by MeasureDT asc'+char(10)
			print @strsql
			exec sp_executesql @strsql,N'@av money output',@vtemp output

			if(@vtemp is not null)
			set @s = @s + @vtemp
		end
		if(@type='avgavg') 
		begin
			SET @strSQL = N''
			SET @strSQL = @strSQL + N'select  @av=avg(ParmValue) from tb_mp_' + @mptemp+N' ' +char(10)
			SET @strSQL = @strSQL + N'where measuredt between ''' +cast(@starttime as varchar(20)) +N''' and '''+cast(@endtime as varchar(20))+N''' and parmvalue <> 0 '+char(10)
			print @strsql
			exec sp_executesql @strsql,N'@av money output',@vtemp output

			if(@vtemp is not null)
			set @s = @s + @vtemp
		end
		
		set @i=@i+1

		end

		close curRunStatus
		DEALLOCATE curRunStatus

		if(@s>=0)
			set @v = @s
		else
			set @v = null

		if @v IS NULL
		begin
			print '-'
			insert into TB_MP_TEMP_RPT(ParmValue,MeasureDT) values('-',@starttime)
		end
		else 
		begin
			if(@type='run')
			begin
				if(@v = 0)
				begin
				print '停止'
				insert into TB_MP_TEMP_RPT(ParmValue,MeasureDT) values('停止',@starttime)
				end
				if(@v = 1)
				begin
				print '运行'
				insert into TB_MP_TEMP_RPT(ParmValue,MeasureDT) values('运行',@starttime)
				end
				if(@v > 1)
				begin
				print '故障'
				insert into TB_MP_TEMP_RPT(ParmValue,MeasureDT) values('故障',@starttime)
				end
			end
			else if(@type='firstavg' or @type='avgavg')
			begin
				print 'insert into TB_MP_TEMP_RPT(ParmValue,MeasureDT) values('''+cast(@v/@count as varchar)+''','''+@starttime+''')'
				insert into TB_MP_TEMP_RPT(ParmValue,MeasureDT) values(cast(@v/@count as varchar),@starttime)
			end
			else
			begin
				print 'insert into TB_MP_TEMP_RPT(ParmValue,MeasureDT) values('''+cast(@v as varchar)+''','''+@starttime+''')'
				insert into TB_MP_TEMP_RPT(ParmValue,MeasureDT) values(cast(@v as varchar),@starttime)
			end
		end

		

	end
	else
	begin
		print '-'
		insert into TB_MP_TEMP_RPT(ParmValue,MeasureDT) values('-',@starttime)
	end


GO

-- ----------------------------
-- Procedure structure for sp_getheadvalue
-- ----------------------------
DROP PROCEDURE [dbo].[sp_getheadvalue]
GO



CREATE PROCEDURE [dbo].[sp_getheadvalue]
@stime varchar(10),
@svalue varchar(500),
@returnvalue nvarchar(500) output
as
Declare @nowvalue nvarchar(4000) 
Declare @strSQL nvarchar(4000)
Declare @totianqi varchar(500)
Declare @towendu varchar(500)
Declare @outtianqi varchar(500)
declare @mptemp varchar(80)
declare @starttime varchar(10)
Select @starttime=CONVERT(varchar(10),DateAdd(day,-1,cast(@stime as datetime)),20)  -- 昨天
	
	set @nowvalue=''
	DECLARE curRunStatus cursor for 
	select * from dbo.FUN_split(@svalue,',') 
	open curRunStatus
	FETCH curRunStatus into @mptemp
	while (@@fetch_status=0)
	begin
	

		SET @strSQL = N''	
		SET @strSQL = @strSQL + N'select top 1 @av=inputvalue  from UV_report_head  where headdate= '''+@starttime+''' and headsetname='''+@mptemp+ ''' order by headsetid asc '	
		exec sp_executesql @strsql,N'@av varchar(500) output',@outtianqi output
		set @nowvalue=@nowvalue+@mptemp+': '+@outtianqi+'    '
	
		FETCH curRunStatus into @mptemp
	end
	
	close curRunStatus
	DEALLOCATE curRunStatus
set @returnvalue=@nowvalue


GO

-- ----------------------------
-- Procedure structure for sp_getscadadata_auto
-- ----------------------------
DROP PROCEDURE [dbo].[sp_getscadadata_auto]
GO



CREATE PROCEDURE [dbo].[sp_getscadadata_auto]  AS

declare @id varchar(50)
declare @name varchar(50)
declare @exp varchar(500)
declare @intv varchar(50)
declare @starthour int

declare @sqlstr varchar(1000)
declare @tmpstr varchar(20)
declare @numstr varchar(20)
declare @tid int
declare @value varchar(50)
declare @mp varchar(50)
declare @type varchar(50)
declare @scadacode varchar(500)
declare @count int


Declare @starttime datetime
Declare @endtime datetime
Declare @tempdate datetime

declare @createtime datetime
declare @GFJ1_FENGLIANG money
declare @GFJ2_FENGLIANG money
declare @GFJ3_FENGLIANG money
declare @GFJ_ZGFL money

declare @LT2201 money
declare @YZC_LVL money

declare @2GUFENG_U2 money
declare @GFJ1_GONGLV money
declare @GFJ2_GONGLV money
declare @GFJ3_GONGLV money
declare @GFJ1_DL money
declare @GFJ2_DL money
declare @GFJ3_DL money


SELECT top 1 @createtime=MeasureDT,@GFJ1_FENGLIANG=ParmValue FROM TB_MP_GFJ1_FENGLIANG  order by MeasureDT desc

SELECT top 1 @createtime=MeasureDT,@GFJ2_FENGLIANG=ParmValue FROM TB_MP_GFJ2_FENGLIANG  order by MeasureDT desc

SELECT top 1 @createtime=MeasureDT,@GFJ3_FENGLIANG=ParmValue FROM TB_MP_GFJ3_FENGLIANG  order by MeasureDT desc

set @GFJ_ZGFL=@GFJ1_FENGLIANG+@GFJ2_FENGLIANG+@GFJ3_FENGLIANG

if EXISTS (select * from TB_MP_GFJ_ZGFL where MeasureDT = @createtime)
update TB_MP_GFJ_ZGFL set ParmValue=@GFJ_ZGFL where MeasureDT = @createtime
else
begin
insert into TB_MP_GFJ_ZGFL(ParmValue,MeasureDT) values(@GFJ_ZGFL,@createtime)
update TB_MeasurePoint set ParmValue=@GFJ_ZGFL,MeasureDT=@createtime where MPointCode='GFJ_ZGFL'
end




SELECT top 1 @createtime=MeasureDT,@LT2201=ParmValue FROM TB_MP_LT2201  order by MeasureDT desc

set @YZC_LVL=@LT2201*0.01

if EXISTS (select * from TB_MP_YZC_LVL where MeasureDT = @createtime)
update TB_MP_YZC_LVL set ParmValue=@YZC_LVL where MeasureDT = @createtime
else
begin
insert into TB_MP_YZC_LVL(ParmValue,MeasureDT) values(@YZC_LVL,@createtime)
update TB_MeasurePoint set ParmValue=@YZC_LVL,MeasureDT=@createtime where MPointCode='YZC_LVL'
end




SELECT top 1 @createtime=MeasureDT,@2GUFENG_U2=ParmValue FROM TB_MP_2GUFENG_U2  order by MeasureDT desc

SELECT top 1 @createtime=MeasureDT,@GFJ1_GONGLV=ParmValue FROM TB_MP_GFJ1_GONGLV  order by MeasureDT desc

SELECT top 1 @createtime=MeasureDT,@GFJ2_GONGLV=ParmValue FROM TB_MP_GFJ2_GONGLV  order by MeasureDT desc

SELECT top 1 @createtime=MeasureDT,@GFJ3_GONGLV=ParmValue FROM TB_MP_GFJ3_GONGLV  order by MeasureDT desc

if(@2GUFENG_U2=0)
begin
set @GFJ1_DL=0
set @GFJ2_DL=0
set @GFJ3_DL=0
end
else
begin
set @GFJ1_DL=@GFJ1_GONGLV/@2GUFENG_U2/1.732/0.9*1000
set @GFJ2_DL=@GFJ2_GONGLV/@2GUFENG_U2/1.732/0.9*1000
set @GFJ3_DL=@GFJ3_GONGLV/@2GUFENG_U2/1.732/0.9*1000
end

if EXISTS (select * from TB_MP_GFJ1_DL where MeasureDT = @createtime)
update TB_MP_GFJ1_DL set ParmValue=@GFJ1_DL where MeasureDT = @createtime
else
begin
insert into TB_MP_GFJ1_DL(ParmValue,MeasureDT) values(@GFJ1_DL,@createtime)
update TB_MeasurePoint set ParmValue=@GFJ1_DL,MeasureDT=@createtime where MPointCode='GFJ1_DL'
end

if EXISTS (select * from TB_MP_GFJ2_DL where MeasureDT = @createtime)
update TB_MP_GFJ2_DL set ParmValue=@GFJ2_DL where MeasureDT = @createtime
else
begin
insert into TB_MP_GFJ2_DL(ParmValue,MeasureDT) values(@GFJ2_DL,@createtime)
update TB_MeasurePoint set ParmValue=@GFJ2_DL,MeasureDT=@createtime where MPointCode='GFJ2_DL'
end

if EXISTS (select * from TB_MP_GFJ3_DL where MeasureDT = @createtime)
update TB_MP_GFJ3_DL set ParmValue=@GFJ3_DL where MeasureDT = @createtime
else
begin
insert into TB_MP_GFJ3_DL(ParmValue,MeasureDT) values(@GFJ3_DL,@createtime)
update TB_MeasurePoint set ParmValue=@GFJ3_DL,MeasureDT=@createtime where MPointCode='GFJ3_DL'
end


GO

-- ----------------------------
-- Procedure structure for sp_gettimedata
-- ----------------------------
DROP PROCEDURE [dbo].[sp_gettimedata]
GO



CREATE PROCEDURE [dbo].[sp_gettimedata]
@starttime varchar(20),@endtime varchar(20),@type varchar(80),@mp varchar(200)
as
Declare @strSQL nvarchar(4000)
declare @v money
declare @s money
declare @vtemp money
declare @mptemp varchar(80)
declare @count int
declare @i int
declare @v1 money
declare @v2 money

select @count = count(*) from dbo.FUN_split(@mp,',') 
set @i = 1
set @s = 0
	if(@mp!='-')
	begin

		DECLARE curRunStatus cursor for 
		select * from dbo.FUN_split(@mp,',') 
		open curRunStatus

		while(@i<=@count)
		begin

		FETCH curRunStatus into @mptemp
		
		if(@type='first') 
		begin
			SET @strSQL = N''
			SET @strSQL = @strSQL + N'select top 1 @av=ParmValue*60  from tb_mp_' + @mptemp+N' ' +char(10)
			SET @strSQL = @strSQL + N'where measuredt between ''' +cast(@starttime as varchar(20)) +N''' and '''+cast(@endtime as varchar(20))+N''' order by MeasureDT asc'+char(10)
			print @strsql
			exec sp_executesql @strsql,N'@av money output',@vtemp output

			if(@vtemp is not null)
			set @s = @s + @vtemp
		end
		if(@type='last') 
		begin
			SET @strSQL = N''
			SET @strSQL = @strSQL + N'select top 1 @av=ParmValue*60 from tb_mp_' + @mptemp+N' ' +char(10)
			SET @strSQL = @strSQL + N'where measuredt between ''' +cast(@starttime as varchar(20)) +N''' and '''+cast(@endtime as varchar(20))+N''' order by MeasureDT desc'+char(10)
			print @strsql
			exec sp_executesql @strsql,N'@av money output',@vtemp output

			if(@vtemp is not null)
			set @s = @s + @vtemp
		end
		if(@type='avg') 
		begin
			SET @strSQL = N''
			SET @strSQL = @strSQL + N'select  @av=avg(ParmValue*60) from tb_mp_' + @mptemp+N' ' +char(10)
			SET @strSQL = @strSQL + N'where measuredt between ''' +cast(@starttime as varchar(20)) +N''' and '''+cast(@endtime as varchar(20))+N''' and parmvalue <> 0 '+char(10)
			print @strsql
			exec sp_executesql @strsql,N'@av money output',@vtemp output

			if(@vtemp is not null)
			set @s = @s + @vtemp
		end
		if(@type='min') 
		begin
			SET @strSQL = N''
			SET @strSQL = @strSQL + N'select  @av=min(ParmValue*60) from tb_mp_' + @mptemp+N' ' +char(10)
			SET @strSQL = @strSQL + N'where measuredt between ''' +cast(@starttime as varchar(20)) +N''' and '''+cast(@endtime as varchar(20))+N''''+char(10)
			print @strsql
			exec sp_executesql @strsql,N'@av money output',@vtemp output

			if(@vtemp is not null)
			set @s = @s + @vtemp
		end
		if(@type='max') 
		begin
			SET @strSQL = N''
			SET @strSQL = @strSQL + N'select  @av=max(ParmValue*60) from tb_mp_' + @mptemp+N' ' +char(10)
			SET @strSQL = @strSQL + N'where measuredt between ''' +cast(@starttime as varchar(20)) +N''' and '''+cast(@endtime as varchar(20))+N''''+char(10)
			print @strsql
			exec sp_executesql @strsql,N'@av money output',@vtemp output

			if(@vtemp is not null)
			set @s = @s + @vtemp
		end
		
		if(@type='sum') 
		begin
			SET @strSQL = N''
			SET @strSQL = @strSQL + N'select  @av=sum(ParmValue) from tb_mp_' + @mptemp+N' ' +char(10)
			SET @strSQL = @strSQL + N'where measuredt >= ''' +cast(@starttime as varchar(20)) +N''' and measuredt < '''+cast(@endtime as varchar(20))+N''''+char(10)
			print @strsql
			exec sp_executesql @strsql,N'@av money output',@vtemp output

			if(@vtemp is not null)
			set @s = @s + @vtemp
		end
		set @i=@i+1

		end

		close curRunStatus
		DEALLOCATE curRunStatus

		if(@s>0)
		begin
			set @v = dbo.FUN_mtohTime(@s)
		end
		else if(@s=0)
		set @v = @s
		else
		set @v = null

		if @v IS NULL
		begin
			print '-'
			insert into TB_MP_TEMP_RPT(ParmValue,MeasureDT) values('-',@starttime)
		end
		else 
		begin
			print 'insert into TB_MP_TEMP_RPT(ParmValue,MeasureDT) values('''+cast(@v as varchar)+''','''+@starttime+''')'
			insert into TB_MP_TEMP_RPT(ParmValue,MeasureDT) values(cast(@v as varchar),@starttime)
		end

		

	end
	else
	begin
		print '-'
		insert into TB_MP_TEMP_RPT(ParmValue,MeasureDT) values('-',@starttime)
	end


GO

-- ----------------------------
-- Procedure structure for sp_insertday
-- ----------------------------
DROP PROCEDURE [dbo].[sp_insertday]
GO



CREATE PROCEDURE [dbo].[sp_insertday]
as


Declare @starttime datetime
Declare @endtime datetime
Declare @tempdate datetime


set @endtime = getdate();
set @endtime = dateadd(hh,-datepart(hh,@endtime),@endtime)
set @endtime = dateadd(ss,-datepart(ss,@endtime),@endtime)
set @endtime = dateadd(mi,-datepart(mi,@endtime),@endtime)
--set @endtime = dateadd(hh,8,@endtime) --当日8点到明日8点

set @starttime = dateadd(day,-1,@endtime)


while(@starttime<@endtime)
begin

set @tempdate = dateadd(day,1,@starttime)

Declare @bql money 
Declare @jsl money 
Declare @whll money
Declare @jscod money
Declare @cscod money
Declare @qsb money 
Declare @hlb money
Declare @codxjl money

set @bql = 0 
set @jsl = 0  
set @whll = 0 
set @jscod = 0 
set @cscod = 0 
set @qsb = 0  
set @hlb = 0 
set @codxjl = 0 

Declare @s1 money 
Declare @s2 money
Declare @s money
Declare @v money
SET @s1 = 0
SET @s2 = 0
SET @s = 0
SET @v = 0

--鼓风机曝气量				
if exists (select * from TB_MP_GFJ1_FL_day where measuredt between @starttime and @tempdate)
select top 1 @s1 =  ParmValue from TB_MP_GFJ1_FL_day where measuredt between @starttime and @tempdate order by measuredt asc
					
if exists (select * from TB_MP_GFJ1_RUNTIME_C_day where measuredt between @starttime and @tempdate)
select  top 1 @s2 =  ParmValue from TB_MP_GFJ1_RUNTIME_C_day where measuredt between @starttime and @tempdate order by measuredt asc

if(@s2 >0)
begin
	if(dbo.FUN_mtohTime(@s2) >= 24 )
	begin
		set @v = 24*@s1
		set @s = @s + @v
	end		
	else
	begin
		set @v = dbo.FUN_mtohTime(@s2)*@s1
		set @s = @s + @v
	end
end
else
begin
	set @v = 0
	set @s = @s + @v
end

SET @s1 = 0
SET @s2 = 0
SET @v = 0

if exists (select * from TB_MP_GFJ2_FL_day where measuredt between @starttime and @tempdate)
select top 1 @s1 =  ParmValue from TB_MP_GFJ2_FL_day where measuredt between @starttime and @tempdate order by measuredt asc
					
if exists (select * from TB_MP_GFJ2_RUNTIME_C_day where measuredt between @starttime and @tempdate)
select  top 1 @s2 =  ParmValue from TB_MP_GFJ2_RUNTIME_C_day where measuredt between @starttime and @tempdate order by measuredt asc

if(@s2 >0)
begin
	if(dbo.FUN_mtohTime(@s2) >= 24 )
	begin
		set @v = 24*@s1
		set @s = @s + @v
	end		
	else
	begin
		set @v = dbo.FUN_mtohTime(@s2)*@s1
		set @s = @s + @v
	end
	end
else
begin
	set @v = 0
	set @s = @s + @v
end

SET @s1 = 0
SET @s2 = 0
SET @v = 0

if exists (select * from TB_MP_GFJ3_FL_day where measuredt between @starttime and @tempdate)
select  top 1 @s1 =  ParmValue from TB_MP_GFJ3_FL_day where measuredt between @starttime and @tempdate order by measuredt asc
					
if exists (select * from TB_MP_GFJ3_RUNTIME_C_day where measuredt between @starttime and @tempdate)
select  top 1 @s2 =  ParmValue from TB_MP_GFJ3_RUNTIME_C_day where measuredt between @starttime and @tempdate order by measuredt asc

if(@s2 >0)
begin
	if(dbo.FUN_mtohTime(@s2) >= 24 )
	begin
		set @v = 24*@s1
		set @s = @s + @v
	end		
	else
	begin
		set @v = dbo.FUN_mtohTime(@s2)*@s1
		set @s = @s + @v
	end
end
else
begin
	set @v = 0
	set @s = @s + @v
end

if(@s>=0)
begin
set @bql = @s
insert into TB_MP_鼓风机曝气量_day(ParmValue,MeasureDT) values(@s ,@starttime)
end
else
begin
insert into TB_MP_鼓风机曝气量_day(ParmValue,MeasureDT) values(0,@starttime)
end

--进水量
if exists (select * from TB_MP_JS_FLOW_C_day where measuredt between @starttime and @tempdate)
select  top 1  @jsl =  ParmValue from TB_MP_JS_FLOW_C_day where measuredt between @starttime and @tempdate order by measuredt asc

--外回流量
SET @s1 = 0
SET @s = 0
SET @v = 0

if exists (select * from TB_MP_WHL_FLW1_day where measuredt between @starttime and @tempdate)
begin
select  top 1 @s1 =  ParmValue from TB_MP_WHL_FLW1_day where measuredt between @starttime and @tempdate	order by measuredt asc		
set @v = 24*@s1
set @s = @s + @v
end

SET @s1 = 0
SET @v = 0

if exists (select * from TB_MP_WHL_FLW2_day where measuredt between @starttime and @tempdate)
begin
select  top 1 @s1 =  ParmValue from TB_MP_WHL_FLW2_day where measuredt between @starttime and @tempdate	order by measuredt asc		
set @v = 24*@s1
set @s = @s + @v
end

if(@s>=0)
begin
set @whll = @s
insert into TB_MP_外回流量_day(ParmValue,MeasureDT) values(@s ,@starttime)
end
else
begin
insert into TB_MP_外回流量_day(ParmValue,MeasureDT) values(0,@starttime)
end

--进水COD
if exists (select * from TB_MP_JS_COD where measuredt between @starttime and @tempdate)
select  top 1 @jscod =  ParmValue from TB_MP_JS_COD where measuredt between @starttime and @tempdate order by measuredt asc

--出水COD
if exists (select * from TB_MP_CS_COD where measuredt between @starttime and @tempdate)
select  top 1 @cscod =  ParmValue from TB_MP_CS_COD where measuredt between @starttime and @tempdate order by measuredt asc

--气水比
if(@jsl>0)
begin
set @qsb = @bql/@jsl
insert into TB_MP_气水比_day(ParmValue,MeasureDT) values(@qsb ,@starttime)
end
else
begin
insert into TB_MP_气水比_day(ParmValue,MeasureDT) values(0 ,@starttime)
end


--回流比
if(@jsl>0)
begin
set @hlb = @whll/@jsl*100
insert into TB_MP_回流比_day(ParmValue,MeasureDT) values(@hlb ,@starttime)
end
else
begin
insert into TB_MP_回流比_day(ParmValue,MeasureDT) values(0 ,@starttime)
end


--COD削减量
if(@jsl*(@jscod-@cscod)>0)
begin
set @codxjl = @jsl*(@jscod-@cscod)/1000000
insert into TB_MP_COD削减量_day(ParmValue,MeasureDT) values(@codxjl ,@starttime)
end
else
begin
insert into TB_MP_COD削减量_day(ParmValue,MeasureDT) values(0 ,@starttime)
end


set @starttime = @tempdate
end


GO

-- ----------------------------
-- Procedure structure for sp_insertdayX
-- ----------------------------
DROP PROCEDURE [dbo].[sp_insertdayX]
GO



CREATE PROCEDURE [dbo].[sp_insertdayX]
@sdatein datetime,@edatein datetime
as


Declare @starttime datetime
Declare @endtime datetime
Declare @tempdate datetime


set @endtime = @edatein
set @endtime = dateadd(hh,-datepart(hh,@endtime),@endtime)
set @endtime = dateadd(ss,-datepart(ss,@endtime),@endtime)
set @endtime = dateadd(mi,-datepart(mi,@endtime),@endtime)
--set @endtime = dateadd(hh,8,@endtime)--当日8点到明日8点

set @starttime = @sdatein
set @starttime = dateadd(hh,-datepart(hh,@starttime),@starttime)
set @starttime = dateadd(ss,-datepart(ss,@starttime),@starttime)
set @starttime = dateadd(mi,-datepart(mi,@starttime),@starttime)
--set @starttime = dateadd(hh,8,@starttime)--当日8点到明日8点

delete from TB_MP_鼓风机曝气量_day where MeasureDT between @starttime and @endtime
delete from TB_MP_外回流量_day where MeasureDT between @starttime and @endtime
delete from TB_MP_气水比_day where MeasureDT between @starttime and @endtime
delete from TB_MP_回流比_day where MeasureDT between @starttime and @endtime
delete from TB_MP_COD削减量_day where MeasureDT between @starttime and @endtime

while(@starttime<@endtime)
begin

set @tempdate = dateadd(day,1,@starttime)

Declare @bql money 
Declare @jsl money 
Declare @whll money
Declare @jscod money
Declare @cscod money
Declare @qsb money 
Declare @hlb money
Declare @codxjl money

set @bql = 0 
set @jsl = 0  
set @whll = 0 
set @jscod = 0 
set @cscod = 0 
set @qsb = 0  
set @hlb = 0 
set @codxjl = 0 

Declare @s1 money 
Declare @s2 money
Declare @s money
Declare @v money
SET @s1 = 0
SET @s2 = 0
SET @s = 0
SET @v = 0

--鼓风机曝气量				
if exists (select * from TB_MP_GFJ1_FL_day where measuredt between @starttime and @tempdate)
select top 1 @s1 =  ParmValue from TB_MP_GFJ1_FL_day where measuredt between @starttime and @tempdate order by measuredt asc
					
if exists (select * from TB_MP_GFJ1_RUNTIME_C_day where measuredt between @starttime and @tempdate)
select  top 1 @s2 =  ParmValue from TB_MP_GFJ1_RUNTIME_C_day where measuredt between @starttime and @tempdate order by measuredt asc

if(@s2 >0)
begin
	if(dbo.FUN_mtohTime(@s2) >= 24 )
	begin
		set @v = 24*@s1
		set @s = @s + @v
	end		
	else
	begin
		set @v = dbo.FUN_mtohTime(@s2)*@s1
		set @s = @s + @v
	end
end
else
begin
	set @v = 0
	set @s = @s + @v
end

SET @s1 = 0
SET @s2 = 0
SET @v = 0

if exists (select * from TB_MP_GFJ2_FL_day where measuredt between @starttime and @tempdate)
select top 1 @s1 =  ParmValue from TB_MP_GFJ2_FL_day where measuredt between @starttime and @tempdate order by measuredt asc
					
if exists (select * from TB_MP_GFJ2_RUNTIME_C_day where measuredt between @starttime and @tempdate)
select  top 1 @s2 =  ParmValue from TB_MP_GFJ2_RUNTIME_C_day where measuredt between @starttime and @tempdate order by measuredt asc

if(@s2 >0)
begin
	if(dbo.FUN_mtohTime(@s2) >= 24 )
	begin
		set @v = 24*@s1
		set @s = @s + @v
	end		
	else
	begin
		set @v = dbo.FUN_mtohTime(@s2)*@s1
		set @s = @s + @v
	end
	end
else
begin
	set @v = 0
	set @s = @s + @v
end

SET @s1 = 0
SET @s2 = 0
SET @v = 0

if exists (select * from TB_MP_GFJ3_FL_day where measuredt between @starttime and @tempdate)
select  top 1 @s1 =  ParmValue from TB_MP_GFJ3_FL_day where measuredt between @starttime and @tempdate order by measuredt asc
					
if exists (select * from TB_MP_GFJ3_RUNTIME_C_day where measuredt between @starttime and @tempdate)
select  top 1 @s2 =  ParmValue from TB_MP_GFJ3_RUNTIME_C_day where measuredt between @starttime and @tempdate order by measuredt asc

if(@s2 >0)
begin
	if(dbo.FUN_mtohTime(@s2) >= 24 )
	begin
		set @v = 24*@s1
		set @s = @s + @v
	end		
	else
	begin
		set @v = dbo.FUN_mtohTime(@s2)*@s1
		set @s = @s + @v
	end
end
else
begin
	set @v = 0
	set @s = @s + @v
end

if(@s>=0)
begin
set @bql = @s
insert into TB_MP_鼓风机曝气量_day(ParmValue,MeasureDT) values(@s ,@starttime)
end
else
begin
insert into TB_MP_鼓风机曝气量_day(ParmValue,MeasureDT) values(0,@starttime)
end

--进水量
if exists (select * from TB_MP_JS_FLOW_C_day where measuredt between @starttime and @tempdate)
select  top 1  @jsl =  ParmValue from TB_MP_JS_FLOW_C_day where measuredt between @starttime and @tempdate order by measuredt asc

--外回流量
SET @s1 = 0
SET @s = 0
SET @v = 0

if exists (select * from TB_MP_WHL_FLW1_day where measuredt between @starttime and @tempdate)
begin
select  top 1 @s1 =  ParmValue from TB_MP_WHL_FLW1_day where measuredt between @starttime and @tempdate	order by measuredt asc		
set @v = 24*@s1
set @s = @s + @v
end

SET @s1 = 0
SET @v = 0

if exists (select * from TB_MP_WHL_FLW2_day where measuredt between @starttime and @tempdate)
begin
select  top 1 @s1 =  ParmValue from TB_MP_WHL_FLW2_day where measuredt between @starttime and @tempdate	order by measuredt asc		
set @v = 24*@s1
set @s = @s + @v
end

if(@s>=0)
begin
set @whll = @s
insert into TB_MP_外回流量_day(ParmValue,MeasureDT) values(@s ,@starttime)
end
else
begin
insert into TB_MP_外回流量_day(ParmValue,MeasureDT) values(0,@starttime)
end

--进水COD
if exists (select * from TB_MP_JS_COD where measuredt between @starttime and @tempdate)
select  top 1 @jscod =  ParmValue from TB_MP_JS_COD where measuredt between @starttime and @tempdate order by measuredt asc

--出水COD
if exists (select * from TB_MP_CS_COD where measuredt between @starttime and @tempdate)
select  top 1 @cscod =  ParmValue from TB_MP_CS_COD where measuredt between @starttime and @tempdate order by measuredt asc

--气水比
if(@jsl>0)
begin
set @qsb = @bql/@jsl
insert into TB_MP_气水比_day(ParmValue,MeasureDT) values(@qsb ,@starttime)
end
else
begin
insert into TB_MP_气水比_day(ParmValue,MeasureDT) values(0 ,@starttime)
end


--回流比
if(@jsl>0)
begin
set @hlb = @whll/@jsl*100
insert into TB_MP_回流比_day(ParmValue,MeasureDT) values(@hlb ,@starttime)
end
else
begin
insert into TB_MP_回流比_day(ParmValue,MeasureDT) values(0 ,@starttime)
end


--COD削减量
if(@jsl*(@jscod-@cscod)>0)
begin
set @codxjl = @jsl*(@jscod-@cscod)/1000000
insert into TB_MP_COD削减量_day(ParmValue,MeasureDT) values(@codxjl ,@starttime)
end
else
begin
insert into TB_MP_COD削减量_day(ParmValue,MeasureDT) values(0 ,@starttime)
end


set @starttime = @tempdate
end


GO

-- ----------------------------
-- Procedure structure for sp_inserthour
-- ----------------------------
DROP PROCEDURE [dbo].[sp_inserthour]
GO



CREATE PROCEDURE [dbo].[sp_inserthour]
as


Declare @starttime datetime
Declare @endtime datetime
Declare @tempdate datetime


set @endtime = getdate();
set @endtime = dateadd(ss,-datepart(ss,@endtime),@endtime)
set @endtime = dateadd(mi,-datepart(mi,@endtime),@endtime)

set @starttime = dateadd(hh,-1,@endtime)


while(@starttime<@endtime)
begin

set @tempdate = dateadd(hour,1,@starttime)

Declare @bql money 
Declare @jsl money 
Declare @whll money
Declare @jscod money
Declare @cscod money
Declare @qsb money 
Declare @hlb money
Declare @codxjl money

set @bql = 0 
set @jsl = 0  
set @whll = 0 
set @jscod = 0 
set @cscod = 0 
set @qsb = 0  
set @hlb = 0 
set @codxjl = 0 

Declare @s1 money 
Declare @s2 money
Declare @s money
Declare @v money
SET @s1 = 0
SET @s2 = 0
SET @s = 0
SET @v = 0

--鼓风机曝气量				
if exists (select * from TB_MP_GFJ1_FL where measuredt between @starttime and @tempdate and ParmValue <> 0)
select  @s1 =  avg(ParmValue) from TB_MP_GFJ1_FL where measuredt between @starttime and @tempdate and ParmValue <> 0
					
if exists (select * from TB_MP_GFJ1_RUNTIME_C_hour where measuredt between @starttime and @tempdate)
select  top 1 @s2 =  ParmValue from TB_MP_GFJ1_RUNTIME_C_hour where measuredt between @starttime and @tempdate

if(@s2 >0)
begin
	if(dbo.FUN_mtohTime(@s2) >= 1 )
	begin
		set @v = 1*@s1
		set @s = @s + @v
	end		
	else
	begin
		set @v = dbo.FUN_mtohTime(@s2)*@s1
		set @s = @s + @v
	end
end
else
begin
	set @v = 0
	set @s = @s + @v
end

SET @s1 = 0
SET @s2 = 0
SET @v = 0

if exists (select * from TB_MP_GFJ2_FL where measuredt between @starttime and @tempdate and ParmValue <> 0)
select  @s1 =  avg(ParmValue) from TB_MP_GFJ2_FL where measuredt between @starttime and @tempdate and ParmValue <> 0
					
if exists (select * from TB_MP_GFJ2_RUNTIME_C_hour where measuredt between @starttime and @tempdate)
select  top 1 @s2 =  ParmValue from TB_MP_GFJ2_RUNTIME_C_hour where measuredt between @starttime and @tempdate

if(@s2 >0)
begin
	if(dbo.FUN_mtohTime(@s2) >= 1 )
	begin
		set @v = 1*@s1
		set @s = @s + @v
	end		
	else
	begin
		set @v = dbo.FUN_mtohTime(@s2)*@s1
		set @s = @s + @v
	end
end
else
begin
	set @v = 0
	set @s = @s + @v
end

SET @s1 = 0
SET @s2 = 0
SET @v = 0

if exists (select * from TB_MP_GFJ3_FL where measuredt between @starttime and @tempdate and ParmValue <> 0)
select  @s1 =  avg(ParmValue) from TB_MP_GFJ3_FL where measuredt between @starttime and @tempdate and ParmValue <> 0
				
if exists (select * from TB_MP_GFJ3_RUNTIME_C_hour where measuredt between @starttime and @tempdate)
select  top 1 @s2 =  ParmValue from TB_MP_GFJ3_RUNTIME_C_hour where measuredt between @starttime and @tempdate

if(@s2 >0)
begin
	if(dbo.FUN_mtohTime(@s2) >= 1 )
	begin
		set @v = 1*@s1
		set @s = @s + @v
	end		
	else
	begin
		set @v = dbo.FUN_mtohTime(@s2)*@s1
		set @s = @s + @v
	end
end
else
begin
	set @v = 0
	set @s = @s + @v
end

if(@s>=0)
begin
set @bql = @s
insert into TB_MP_鼓风机曝气量_hour(ParmValue,MeasureDT) values(@s ,@starttime)
end
else
begin
insert into TB_MP_鼓风机曝气量_hour(ParmValue,MeasureDT) values(0,@starttime)
end

--进水量
if exists (select * from TB_MP_JS_FLOW_C_hour where measuredt between @starttime and @tempdate)
select  top 1  @jsl =  ParmValue from TB_MP_JS_FLOW_C_hour where measuredt between @starttime and @tempdate

--外回流量
SET @s1 = 0
SET @s = 0
SET @v = 0

if exists (select * from TB_MP_WHL_FLW1 where measuredt between @starttime and @tempdate and ParmValue <> 0)
begin
select  @s1 =  avg(ParmValue) from TB_MP_WHL_FLW1 where measuredt between @starttime and @tempdate and ParmValue <> 0					
set @v = 1*@s1
set @s = @s + @v
end

SET @s1 = 0
SET @v = 0

if exists (select * from TB_MP_WHL_FLW2 where measuredt between @starttime and @tempdate and ParmValue <> 0)
begin
select  @s1 =  avg(ParmValue) from TB_MP_WHL_FLW2 where measuredt between @starttime and @tempdate and ParmValue <> 0					
set @v = 1*@s1
set @s = @s + @v
end

if(@s>=0)
begin
set @whll = @s
insert into TB_MP_外回流量_hour(ParmValue,MeasureDT) values(@s ,@starttime)
end
else
begin
insert into TB_MP_外回流量_hour(ParmValue,MeasureDT) values(0,@starttime)
end

--进水COD
if exists (select * from TB_MP_JS_COD where measuredt between @starttime and @tempdate and ParmValue <> 0)
select  @jscod =  avg(ParmValue) from TB_MP_JS_COD where measuredt between @starttime and @tempdate and ParmValue <> 0

--出水COD
if exists (select * from TB_MP_CS_COD where measuredt between @starttime and @tempdate and ParmValue <> 0)
select  @cscod =  avg(ParmValue) from TB_MP_CS_COD where measuredt between @starttime and @tempdate and ParmValue <> 0

--气水比
if(@jsl>0)
begin
set @qsb = @bql/@jsl
insert into TB_MP_气水比_hour(ParmValue,MeasureDT) values(@qsb ,@starttime)
end
else
begin
insert into TB_MP_气水比_hour(ParmValue,MeasureDT) values(0 ,@starttime)
end


--回流比
if(@jsl>0)
begin
set @hlb = @whll/@jsl*100
insert into TB_MP_回流比_hour(ParmValue,MeasureDT) values(@hlb ,@starttime)
end
else
begin
insert into TB_MP_回流比_hour(ParmValue,MeasureDT) values(0 ,@starttime)
end


--COD削减量
if(@jsl*(@jscod-@cscod)>0)
begin
set @codxjl = @jsl*(@jscod-@cscod)/1000000
insert into TB_MP_COD削减量_hour(ParmValue,MeasureDT) values(@codxjl ,@starttime)
end
else
begin
insert into TB_MP_COD削减量_hour(ParmValue,MeasureDT) values(0 ,@starttime)
end


set @starttime = @tempdate
end


GO

-- ----------------------------
-- Procedure structure for sp_inserthourX
-- ----------------------------
DROP PROCEDURE [dbo].[sp_inserthourX]
GO



CREATE PROCEDURE [dbo].[sp_inserthourX]
@sdatein datetime,@edatein datetime
as


Declare @starttime datetime
Declare @endtime datetime
Declare @tempdate datetime


set @endtime = @edatein
set @endtime = dateadd(ss,-datepart(ss,@endtime),@endtime)
set @endtime = dateadd(mi,-datepart(mi,@endtime),@endtime)

set @starttime = @sdatein
set @starttime = dateadd(ss,-datepart(ss,@starttime),@starttime)
set @starttime = dateadd(mi,-datepart(mi,@starttime),@starttime)

delete from TB_MP_鼓风机曝气量_hour where MeasureDT between @starttime and @endtime
delete from TB_MP_外回流量_hour where MeasureDT between @starttime and @endtime
delete from TB_MP_气水比_hour where MeasureDT between @starttime and @endtime
delete from TB_MP_回流比_hour where MeasureDT between @starttime and @endtime
delete from TB_MP_COD削减量_hour where MeasureDT between @starttime and @endtime


while(@starttime<@endtime)
begin

set @tempdate = dateadd(hour,1,@starttime)

Declare @bql money 
Declare @jsl money 
Declare @whll money
Declare @jscod money
Declare @cscod money
Declare @qsb money 
Declare @hlb money
Declare @codxjl money

set @bql = 0 
set @jsl = 0  
set @whll = 0 
set @jscod = 0 
set @cscod = 0 
set @qsb = 0  
set @hlb = 0 
set @codxjl = 0 

Declare @s1 money 
Declare @s2 money
Declare @s money
Declare @v money
SET @s1 = 0
SET @s2 = 0
SET @s = 0
SET @v = 0

--鼓风机曝气量				
if exists (select * from TB_MP_GFJ1_FL where measuredt between @starttime and @tempdate and ParmValue <> 0)
select  @s1 =  avg(ParmValue) from TB_MP_GFJ1_FL where measuredt between @starttime and @tempdate and ParmValue <> 0
					
if exists (select * from TB_MP_GFJ1_RUNTIME_C_hour where measuredt between @starttime and @tempdate)
select  top 1 @s2 =  ParmValue from TB_MP_GFJ1_RUNTIME_C_hour where measuredt between @starttime and @tempdate

if(@s2 >0)
begin
	if(dbo.FUN_mtohTime(@s2) >= 1 )
	begin
		set @v = 1*@s1
		set @s = @s + @v
	end		
	else
	begin
		set @v = dbo.FUN_mtohTime(@s2)*@s1
		set @s = @s + @v
	end
end
else
begin
	set @v = 0
	set @s = @s + @v
end

SET @s1 = 0
SET @s2 = 0
SET @v = 0

if exists (select * from TB_MP_GFJ2_FL where measuredt between @starttime and @tempdate and ParmValue <> 0)
select  @s1 =  avg(ParmValue) from TB_MP_GFJ2_FL where measuredt between @starttime and @tempdate and ParmValue <> 0
					
if exists (select * from TB_MP_GFJ2_RUNTIME_C_hour where measuredt between @starttime and @tempdate)
select  top 1 @s2 =  ParmValue from TB_MP_GFJ2_RUNTIME_C_hour where measuredt between @starttime and @tempdate

if(@s2 >0)
begin
	if(dbo.FUN_mtohTime(@s2) >= 1 )
	begin
		set @v = 1*@s1
		set @s = @s + @v
	end		
	else
	begin
		set @v = dbo.FUN_mtohTime(@s2)*@s1
		set @s = @s + @v
	end
end
else
begin
	set @v = 0
	set @s = @s + @v
end

SET @s1 = 0
SET @s2 = 0
SET @v = 0

if exists (select * from TB_MP_GFJ3_FL where measuredt between @starttime and @tempdate and ParmValue <> 0)
select  @s1 =  avg(ParmValue) from TB_MP_GFJ3_FL where measuredt between @starttime and @tempdate and ParmValue <> 0
				
if exists (select * from TB_MP_GFJ3_RUNTIME_C_hour where measuredt between @starttime and @tempdate)
select  top 1 @s2 =  ParmValue from TB_MP_GFJ3_RUNTIME_C_hour where measuredt between @starttime and @tempdate

if(@s2 >0)
begin
	if(dbo.FUN_mtohTime(@s2) >= 1 )
	begin
		set @v = 1*@s1
		set @s = @s + @v
	end		
	else
	begin
		set @v = dbo.FUN_mtohTime(@s2)*@s1
		set @s = @s + @v
	end
end
else
begin
	set @v = 0
	set @s = @s + @v
end

if(@s>=0)
begin
set @bql = @s
insert into TB_MP_鼓风机曝气量_hour(ParmValue,MeasureDT) values(@s ,@starttime)
end
else
begin
insert into TB_MP_鼓风机曝气量_hour(ParmValue,MeasureDT) values(0,@starttime)
end

--进水量
if exists (select * from TB_MP_JS_FLOW_C_hour where measuredt between @starttime and @tempdate)
select  top 1  @jsl =  ParmValue from TB_MP_JS_FLOW_C_hour where measuredt between @starttime and @tempdate

--外回流量
SET @s1 = 0
SET @s = 0
SET @v = 0

if exists (select * from TB_MP_WHL_FLW1 where measuredt between @starttime and @tempdate and ParmValue <> 0)
begin
select  @s1 =  avg(ParmValue) from TB_MP_WHL_FLW1 where measuredt between @starttime and @tempdate and ParmValue <> 0					
set @v = 1*@s1
set @s = @s + @v
end

SET @s1 = 0
SET @v = 0

if exists (select * from TB_MP_WHL_FLW2 where measuredt between @starttime and @tempdate and ParmValue <> 0)
begin
select  @s1 =  avg(ParmValue) from TB_MP_WHL_FLW2 where measuredt between @starttime and @tempdate and ParmValue <> 0					
set @v = 1*@s1
set @s = @s + @v
end

if(@s>=0)
begin
set @whll = @s
insert into TB_MP_外回流量_hour(ParmValue,MeasureDT) values(@s ,@starttime)
end
else
begin
insert into TB_MP_外回流量_hour(ParmValue,MeasureDT) values(0,@starttime)
end

--进水COD
if exists (select * from TB_MP_JS_COD where measuredt between @starttime and @tempdate and ParmValue <> 0)
select  @jscod =  avg(ParmValue) from TB_MP_JS_COD where measuredt between @starttime and @tempdate and ParmValue <> 0

--出水COD
if exists (select * from TB_MP_CS_COD where measuredt between @starttime and @tempdate and ParmValue <> 0)
select  @cscod =  avg(ParmValue) from TB_MP_CS_COD where measuredt between @starttime and @tempdate and ParmValue <> 0

--气水比
if(@jsl>0)
begin
set @qsb = @bql/@jsl
insert into TB_MP_气水比_hour(ParmValue,MeasureDT) values(@qsb ,@starttime)
end
else
begin
insert into TB_MP_气水比_hour(ParmValue,MeasureDT) values(0 ,@starttime)
end


--回流比
if(@jsl>0)
begin
set @hlb = @whll/@jsl*100
insert into TB_MP_回流比_hour(ParmValue,MeasureDT) values(@hlb ,@starttime)
end
else
begin
insert into TB_MP_回流比_hour(ParmValue,MeasureDT) values(0 ,@starttime)
end


--COD削减量
if(@jsl*(@jscod-@cscod)>0)
begin
set @codxjl = @jsl*(@jscod-@cscod)/1000000
insert into TB_MP_COD削减量_hour(ParmValue,MeasureDT) values(@codxjl ,@starttime)
end
else
begin
insert into TB_MP_COD削减量_hour(ParmValue,MeasureDT) values(0 ,@starttime)
end

set @starttime = @tempdate
end


GO

-- ----------------------------
-- Procedure structure for sp_mptalarm
-- ----------------------------
DROP PROCEDURE [dbo].[sp_mptalarm]
GO



CREATE PROCEDURE [dbo].[sp_mptalarm]
as


Declare @starttime datetime
Declare @endtime datetime
Declare @MPointCode varchar(50)
Declare @ParmName varchar(50)
DECLARE @avgmax MONEY
DECLARE @avgmin MONEY
Declare @strSQL nvarchar(4000)
Declare @strSQL1 nvarchar(4000)
Declare @strtemp nvarchar(4000)
Declare @stralarm nvarchar(4000)
Declare @stralarm1 nvarchar(4000)
Declare @msg varchar(255)


set @endtime = getdate();
set @endtime = dateadd(hh,-datepart(hh,@endtime),@endtime)
set @endtime = dateadd(ss,-datepart(ss,@endtime),@endtime)
set @endtime = dateadd(mi,-datepart(mi,@endtime),@endtime)
set @endtime = dateadd(ms,-datepart(ms,@endtime),@endtime)
set @endtime = dateadd(hh,8,@endtime) --当日8点到明日8点

set @starttime = dateadd(day,-1,@endtime)

--set @ParmValue = 0
--set @MeasureDT = @starttime

--BEGIN TRANSACTION

SET @strSQL1 = N''
SET @strSQL1 = @strSQL1 + N'delete from tb_mptalarm where alarmdt between'''+cast(@starttime as varchar)+N''' and '''+cast(@endtime as varchar)+N'''';
print @strSQL1
exec sp_executesql @strSQL1

DECLARE curRunStatus cursor for 
select MPointCode,ParmName,avgmax,avgmin from TB_MeasurePoint where avgmin is not null or avgmax is not null
open curRunStatus

FETCH curRunStatus into @MPointCode,@ParmName,@avgmax,@avgmin

if(@ParmName is null)
set @ParmName=@MPointCode

if(@avgmax is null)
set @avgmax=99999

if(@avgmin is null)
set @avgmin=0

set @stralarm = ''
set @stralarm1 = ''

while (@@fetch_status=0)
begin
set @strtemp = ''
SET @strSQL = N''
SET @strSQL = @strSQL + N'declare @ParmValue money'+char(10)
SET @strSQL = @strSQL + N'declare @MeasureDT datetime'+char(10)
SET @strSQL = @strSQL + N'declare @str nvarchar(4000)'+char(10)
SET @strSQL = @strSQL + N'SET @str = N'''''+char(10)
SET @strSQL = @strSQL + N'DECLARE cur cursor for select ParmValue,MeasureDT from TB_MP_' + @MPointCode+N' ' +char(10)
SET @strSQL = @strSQL + N'where (measuredt between ''' +cast(@starttime as varchar(20))+N''' and '''+cast(@endtime as varchar(20))+N''') '+char(10)
SET @strSQL = @strSQL + N'and (ParmValue > ' +cast(@avgmax as varchar) +N' or ParmValue < '+cast(@avgmin as varchar)+N') order by MeasureDT asc'+char(10)
SET @strSQL = @strSQL + N'open cur'+char(10)
SET @strSQL = @strSQL + N'FETCH cur into @ParmValue,@MeasureDT'+char(10)
SET @strSQL = @strSQL + N'while (@@fetch_status=0)'+char(10)
SET @strSQL = @strSQL + N'begin'+char(10)
SET @strSQL = @strSQL + N'set @str = @str + N''测量点:'+@ParmName+'['+@MPointCode+'],超限值:''+cast(@ParmValue as varchar)+'',时间:''+CONVERT(varchar(20), @MeasureDT,20)+'';'''+char(10)
SET @strSQL = @strSQL + N'FETCH cur into @ParmValue,@MeasureDT'+char(10)
SET @strSQL = @strSQL + N'end'+char(10)
SET @strSQL = @strSQL + N'close cur'+char(10)
SET @strSQL = @strSQL + N'DEALLOCATE cur'+char(10)
SET @strSQL = @strSQL + N'select @strtp = @str' +char(10)
--SET @strSQL = @strSQL + N'insert into tb_mptalarm(alarmdt,comment) values('''+cast(@starttime as varchar)+''',@str)'+char(10)

print @strSQL
exec sp_executesql @strSQL,N'@strtp nvarchar(4000) output',@strtemp output

if(@strtemp is not null)
begin

if(len(@stralarm+@strtemp)<4000)
begin
set @stralarm=@stralarm+@strtemp
end
else
begin
set @stralarm1=@stralarm1+@strtemp
end

end

FETCH curRunStatus into @MPointCode,@ParmName,@avgmax,@avgmin

if(@ParmName is null)
set @ParmName=@MPointCode

if(@avgmax is null)
set @avgmax=99999

if(@avgmin is null)
set @avgmin=0

end

close curRunStatus
DEALLOCATE curRunStatus

print @stralarm
print RTRIM(LTRIM(@stralarm))
print len(RTRIM(LTRIM(@stralarm)))
print @stralarm1
print RTRIM(LTRIM(@stralarm1))
print len(RTRIM(LTRIM(@stralarm1)))


if(len(RTRIM(LTRIM(@stralarm)))>0)
begin
insert into tb_mptalarm(alarmdt,comment) values(@starttime,@stralarm+@stralarm1)
insert into CHSW.dbo.TB_Mail_MessageSend(iMessageid,cSendUserId,CSendUserEmail,cToUserId,CToUserEmail,cForUserId,cForUserEmail,cSecrecyUserId,cSecrecyUserEmail,cMessageTitle,cMessageCotent,dSendTime,cMyListTree,cMailcheck,cReadTime,cAdjunct,cEmailEstate,cEmailType,cExigence,cEmailConSize,iMessageMailid,cAdjuncturl,cAdjunctnumber) 
values(newid(),'emp000029','','超级管理员','','','','','','生产数据超限报警',@stralarm+@stralarm1,getdate(),'ALL','0','0','0','collectmail','Inmail','none','0 B','none','',0)
insert into CHSW.dbo.TB_Mail_MessageSend(iMessageid,cSendUserId,CSendUserEmail,cToUserId,CToUserEmail,cForUserId,cForUserEmail,cSecrecyUserId,cSecrecyUserEmail,cMessageTitle,cMessageCotent,dSendTime,cMyListTree,cMailcheck,cReadTime,cAdjunct,cEmailEstate,cEmailType,cExigence,cEmailConSize,iMessageMailid,cAdjuncturl,cAdjunctnumber) 
values(newid(),'xxxxx','','超级管理员','','','','','','生产数据超限报警',@stralarm+@stralarm1,getdate(),'ALL','0','0','0','collectmail','Inmail','none','0 B','none','',0)
end

	
	/*If @@ERROR <> 0
	Begin
	Set @Msg = '数据插入出错！'
	Goto ErrorHandle
	End
	 

--Select * from tb_mptalarm

ErrorHandle:

IF @msg IS null
    Commit transaction
ELSE
    BEGIN
        Rollback transaction
        RAISERROR (@Msg, 16, 1)
    END*/


GO

-- ----------------------------
-- Procedure structure for sp_mptalarmold
-- ----------------------------
DROP PROCEDURE [dbo].[sp_mptalarmold]
GO



CREATE PROCEDURE [dbo].[sp_mptalarmold]
as


Declare @starttime datetime
Declare @endtime datetime
Declare @MPointCode varchar(50)
Declare @ParmName varchar(50)
DECLARE @avgmax MONEY
DECLARE @avgmin MONEY
Declare @strSQL nvarchar(4000)
Declare @strSQL1 nvarchar(4000)
Declare @msg varchar(255)


set @endtime = getdate();
set @endtime = dateadd(hh,-datepart(hh,@endtime),@endtime)
set @endtime = dateadd(ss,-datepart(ss,@endtime),@endtime)
set @endtime = dateadd(mi,-datepart(mi,@endtime),@endtime)
set @endtime = dateadd(ms,-datepart(ms,@endtime),@endtime)
set @endtime = dateadd(hh,8,@endtime) --当日8点到明日8点

set @starttime = dateadd(day,-1,@endtime)

--set @ParmValue = 0
--set @MeasureDT = @starttime

BEGIN TRANSACTION

SET @strSQL1 = N''
SET @strSQL1 = @strSQL1 + N'delete from tb_mptalarm where alarmdt between'''+cast(@starttime as varchar)+N''' and '''+cast(@endtime as varchar)+N'''';
print @strSQL1
exec sp_executesql @strSQL1

DECLARE curRunStatus cursor for 
select MPointCode,ParmName,avgmax,avgmin from TB_MeasurePoint where avgmin is not null and avgmax is not null
open curRunStatus

FETCH curRunStatus into @MPointCode,@ParmName,@avgmax,@avgmin

while (@@fetch_status=0)
begin

SET @strSQL = N''
SET @strSQL = @strSQL + N'declare @ParmValue money'+char(10)
SET @strSQL = @strSQL + N'declare @MeasureDT datetime'+char(10)
SET @strSQL = @strSQL + N'declare @str nvarchar(4000)'+char(10)
SET @strSQL = @strSQL + N'DECLARE cur cursor for select ParmValue,MeasureDT from TB_MP_' + @MPointCode+N' ' +char(10)
SET @strSQL = @strSQL + N'where (measuredt between ''' +cast(@starttime as varchar(20))+N''' and '''+cast(@endtime as varchar(20))+N''') '+char(10)
SET @strSQL = @strSQL + N'and (ParmValue not between ' +cast(@avgmin as varchar) +N' and '+cast(@avgmax as varchar)+N') order by MeasureDT asc'+char(10)
SET @strSQL = @strSQL + N'open cur'+char(10)
SET @strSQL = @strSQL + N'FETCH cur into @ParmValue,@MeasureDT'+char(10)
SET @strSQL = @strSQL + N'while (@@fetch_status=0)'+char(10)
SET @strSQL = @strSQL + N'begin'+char(10)
SET @strSQL = @strSQL + N'SET @str = N'''''+char(10)
SET @strSQL = @strSQL + N'set @str = @str + N''测量点:'+@ParmName+'['+@MPointCode+'],超限值:''+cast(@ParmValue as varchar)+'',时间:''+CONVERT(varchar(20), @MeasureDT,20)+'';'''+char(10)
SET @strSQL = @strSQL + N'insert into tb_mptalarm(alarmdt,comment) values('''+cast(@starttime as varchar)+''',@str)'+char(10)
SET @strSQL = @strSQL + N'FETCH cur into @ParmValue,@MeasureDT'+char(10)
SET @strSQL = @strSQL + N'end'+char(10)

SET @strSQL = @strSQL + N'close cur'+char(10)
SET @strSQL = @strSQL + N'DEALLOCATE cur'+char(10)


print @strSQL
exec sp_executesql @strSQL

FETCH curRunStatus into @MPointCode,@ParmName,@avgmax,@avgmin

end

close curRunStatus
DEALLOCATE curRunStatus


			

	
	If @@ERROR <> 0
	Begin
	Set @Msg = '数据插入出错！'
	Goto ErrorHandle
	End
	 

--Select * from tb_mptalarm

ErrorHandle:

IF @msg IS null
    Commit transaction
ELSE
    BEGIN
        Rollback transaction
        RAISERROR (@Msg, 16, 1)
    END


GO

-- ----------------------------
-- Procedure structure for sp_mth_01
-- ----------------------------
DROP PROCEDURE [dbo].[sp_mth_01]
GO



CREATE PROCEDURE [dbo].[sp_mth_01]
@stime varchar(10),  @starthour int,  @intval int, @mthsdt int  
 AS
declare @starttime datetime
declare @endtime datetime
declare @starttimeadd datetime
declare @tempdateadd datetime
declare @tempdate datetime
declare @starttime0 datetime
declare @tempdate0 datetime
--Select @endtime=cast(@stime as datetime)
--print @endtime
--Select @endtime=DateAdd(hour,@starthour,@endtime) 
--Select @endtime=dateadd(minute,30,@endtime)  --增加半小时

Select @starttime=cast(@stime as datetime)
if(DATEPART(day,@starttime)>@mthsdt)
begin

	set @starttime = cast (cast(DATEPART(yyyy,@starttime)  as varchar)+'-'+cast(DATEPART(mm,@starttime)  as varchar)+'-'+cast(@mthsdt as varchar) as datetime)
	set @starttime = dateadd(month,-2,@starttime)
	Select @starttime=DateAdd(hour,@starthour,@starttime) 
	select @endtime=dateadd(MONTH,1,@starttime)
end
else
begin

	set @starttime = cast (cast(DATEPART(yyyy,@starttime)  as varchar)+'-'+cast(DATEPART(mm,@starttime)  as varchar)+'-'+cast(@mthsdt as varchar) as datetime)
	set @starttime = dateadd(month,-2,@starttime)
	Select @starttime=DateAdd(hour,@starthour,@starttime) 
	select @endtime=dateadd(MONTH,1,@starttime)
end 
--Select @starttime=dateadd(minute,30,@starttime)  --增加半小时


delete from tb_mp_temp_rpt

while(@starttime<@endtime)
begin
	print @starttime
	print @endtime
	set @starttimeadd=dateadd(DAY,1,@starttime)
	set @starttimeadd=dateadd(MINUTE,5,@starttimeadd)
	set @tempdate = dateadd(DAY,@intval,@starttime)
	set @tempdateadd=dateadd(DAY,1,@tempdate)
	SET @starttime0=DATEADD(HOUR,-DATEPART(HOUR,@starttime),@starttime)
	SET @tempdate0=DATEADD(HOUR,-DATEPART(HOUR,@tempdate),@tempdate)
	print @starttime0
	print @tempdate0
	print @starttimeadd
	insert into TB_MP_TEMP_RPT(ParmValue,MeasureDT) values (DATEPART(day,@starttime),@starttime)
	exec sp_getdata @starttimeadd,@tempdateadd,'first','JS_FL_TOT1_Y'
	exec sp_getdata @starttime0,@tempdate0,'first','hqws_zks_mrydl'
	exec sp_getdata @starttime0,@tempdate0,'first','-'
	exec sp_getdata @starttime0,@tempdate0,'first','hqws_hys_mrsw'
	exec sp_getdata @starttime0,@tempdate0,'first','hqws_hys_jsbod5'
	exec sp_getdata @starttime0,@tempdate0,'first','hqws_hys_csbod5'
	exec sp_getdata @starttime0,@tempdate0,'first','-'
	exec sp_getdata @starttime0,@tempdate0,'first','hqws_hys_jscodcr'
	exec sp_getdata @starttime0,@tempdate0,'first','hqws_hys_cscodcr'
	exec sp_getdata @starttime0,@tempdate0,'first','-'
	exec sp_getdata @starttime0,@tempdate0,'first','hqws_hys_jsss'
	exec sp_getdata @starttime0,@tempdate0,'first','hqws_hys_csss'
	exec sp_getdata @starttime0,@tempdate0,'first','-'
	exec sp_getdata @starttime0,@tempdate0,'first','hqws_hys_jsnh3n'
	exec sp_getdata @starttime0,@tempdate0,'first','hqws_hys_csnh3n'
	exec sp_getdata @starttime0,@tempdate0,'first','-'
	exec sp_getdata @starttime0,@tempdate0,'first','hqws_hys_jstn'
	exec sp_getdata @starttime0,@tempdate0,'first','hqws_hys_cstn'
	exec sp_getdata @starttime0,@tempdate0,'first','-'
	exec sp_getdata @starttime0,@tempdate0,'first','hqws_hys_jstp'
	exec sp_getdata @starttime0,@tempdate0,'first','hqws_hys_cstp'
	exec sp_getdata @starttime0,@tempdate0,'first','-'
	exec sp_getdata @starttime0,@tempdate0,'first','hqws_hys_jsph'
	exec sp_getdata @starttime0,@tempdate0,'first','hqws_hys_csph'
	exec sp_getdata @starttime0,@tempdate0,'first','hqws_hys_jssd'
	exec sp_getdata @starttime0,@tempdate0,'first','hqws_hys_cssd'
	exec sp_getdata @starttime0,@tempdate0,'first','hqws_hys_csno3n'
	exec sp_getdata @starttime0,@tempdate0,'first','hqws_hys_csno2n'


	set @starttime = @tempdate
end

Select * from TB_MP_TEMP_RPT order by itemid


GO

-- ----------------------------
-- Procedure structure for sp_mth_02
-- ----------------------------
DROP PROCEDURE [dbo].[sp_mth_02]
GO



CREATE PROCEDURE [dbo].[sp_mth_02]
@stime varchar(10),  @starthour int,  @intval int, @mthsdt int  
 AS
declare @starttime datetime
declare @endtime datetime
declare @tempdate datetime
declare @starttime0 datetime
declare @tempdate0 datetime
--Select @endtime=cast(@stime as datetime)
--Select @endtime=DateAdd(hour,@starthour,@endtime) 
--Select @endtime=dateadd(minute,30,@endtime)  --增加半小时

Select @starttime=cast(@stime as datetime)
if(DATEPART(day,@starttime)>@mthsdt)
begin
	set @starttime = cast (cast(DATEPART(yyyy,@starttime)  as varchar)+'-'+cast(DATEPART(mm,@starttime)  as varchar)+'-'+cast(@mthsdt as varchar) as datetime)
	set @starttime = dateadd(month,-2,@starttime)
	Select @starttime=DateAdd(hour,@starthour,@starttime) 
	select @endtime=dateadd(MONTH,1,@starttime)
end
else
begin

	set @starttime = cast (cast(DATEPART(yyyy,@starttime)  as varchar)+'-'+cast(DATEPART(mm,@starttime)  as varchar)+'-'+cast(@mthsdt as varchar) as datetime)
	set @starttime = dateadd(month,-2,@starttime)
	Select @starttime=DateAdd(hour,@starthour,@starttime) 
	select @endtime=dateadd(MONTH,1,@starttime)
end 
--Select @starttime=dateadd(minute,30,@starttime)  --增加半小时


delete from tb_mp_temp_rpt

while(@starttime<@endtime)
begin
	set @tempdate = dateadd(DAY,@intval,@starttime)
	SET @starttime0=DATEADD(HOUR,-DATEPART(HOUR,@starttime),@starttime)
	SET @tempdate0=DATEADD(HOUR,-DATEPART(HOUR,@tempdate),@tempdate)
	--insert into TB_MP_TEMP_RPT(ParmValue,MeasureDT) values (DATEPART(day,@starttime),@starttime)
	exec sp_getdata @starttime,@tempdate,'first','-'
	exec sp_getdata @starttime,@tempdate,'first','hqws_hys_sdclpam'
	exec sp_getdata @starttime,@tempdate,'first','-'
	exec sp_getdata @starttime,@tempdate,'first','-'
	exec sp_getdata @starttime,@tempdate,'first','-'
	exec sp_getdata @starttime,@tempdate,'first','-'
	exec sp_getdata @starttime,@tempdate,'first','hqws_zks_trqmr'
	exec sp_getdata @starttime,@tempdate,'first','-'
	exec sp_getdata @starttime,@tempdate,'first','-'
	exec sp_getdata @starttime,@tempdate,'first','-'
	exec sp_getdata @starttime0,@tempdate0,'first','hqws_hys_sdtswnl'
	exec sp_getdata @starttime,@tempdate,'first','-'
	exec sp_getdata @starttime0,@tempdate0,'first','hqws_hys_sdtswyl'
	exec sp_getdata @starttime,@tempdate,'first','-'



	set @starttime = @tempdate
end

Select * from TB_MP_TEMP_RPT order by itemid


GO

-- ----------------------------
-- Procedure structure for sp_mth_03
-- ----------------------------
DROP PROCEDURE [dbo].[sp_mth_03]
GO



CREATE PROCEDURE [dbo].[sp_mth_03]
@stime varchar(10),  @starthour int,  @intval int, @mthsdt int  
 AS
declare @starttime datetime
declare @endtime datetime
declare @tempdate datetime
declare @starttime0 datetime
declare @tempdate0 datetime
Select @endtime=cast(@stime as datetime)
Select @endtime=DateAdd(hour,@starthour,@endtime) 
--Select @endtime=dateadd(minute,30,@endtime)  --增加半小时

Select @starttime=cast(@stime as datetime)
if(DATEPART(day,@starttime)>@mthsdt)
begin
	set @starttime = cast (cast(DATEPART(yyyy,@starttime)  as varchar)+'-'+cast(DATEPART(mm,@starttime)  as varchar)+'-'+cast(@mthsdt as varchar) as datetime)
		set @starttime = dateadd(month,-2,@starttime)
	Select @starttime=DateAdd(hour,@starthour,@starttime) 
	select @endtime=dateadd(MONTH,1,@starttime)
end
else
begin
	
	set @starttime = cast (cast(DATEPART(yyyy,@starttime)  as varchar)+'-'+cast(DATEPART(mm,@starttime)  as varchar)+'-'+cast(@mthsdt as varchar) as datetime)
	set @starttime = dateadd(month,-2,@starttime)
	Select @starttime=DateAdd(hour,@starthour,@starttime) 
	select @endtime=dateadd(MONTH,1,@starttime)
end 
--Select @starttime=dateadd(minute,30,@starttime)  --增加半小时


delete from tb_mp_temp_rpt

while(@starttime<@endtime)
begin
	set @tempdate = dateadd(DAY,@intval,@starttime)
	SET @starttime0=DATEADD(HOUR,-DATEPART(HOUR,@starttime),@starttime)
	SET @tempdate0=DATEADD(HOUR,-DATEPART(HOUR,@tempdate),@tempdate)
	insert into TB_MP_TEMP_RPT(ParmValue,MeasureDT) values (DATEPART(day,@starttime),@starttime)
	exec sp_getdata @starttime0,@tempdate0,'first','hqws_hys_1do'
	exec sp_getdata @starttime0,@tempdate0,'first','hqws_hys_2do'
	exec sp_getdata @starttime0,@tempdate0,'first','hqws_hys_3do'
	exec sp_getdata @starttime0,@tempdate0,'first','hqws_hys_4do'
	exec sp_getdata @starttime0,@tempdate0,'first','hqws_hys_1mlss'
	exec sp_getdata @starttime0,@tempdate0,'first','hqws_hys_2mlss'
	exec sp_getdata @starttime0,@tempdate0,'first','hqws_hys_3mlss'
	exec sp_getdata @starttime0,@tempdate0,'first','hqws_hys_4mlss'
	exec sp_getdata @starttime0,@tempdate0,'first','hqws_hys_1svi'
	exec sp_getdata @starttime0,@tempdate0,'first','hqws_hys_2svi'
	exec sp_getdata @starttime0,@tempdate0,'first','hqws_hys_3svi'
	exec sp_getdata @starttime0,@tempdate0,'first','hqws_hys_4svi'
	exec sp_getdata @starttime0,@tempdate0,'first','hqws_hys_jszgt'
	exec sp_getdata @starttime0,@tempdate0,'first','hqws_hys_cszgt'
	exec sp_getdata @starttime0,@tempdate0,'first','hqws_hys_lhw'
	exec sp_getdata @starttime0,@tempdate0,'first','hqws_hys_cslhw'
	exec sp_getdata @starttime0,@tempdate0,'first','hqws_hys_1hhyhf'
	exec sp_getdata @starttime0,@tempdate0,'first','hqws_hys_2hhyhf'
	exec sp_getdata @starttime0,@tempdate0,'first','hqws_hys_3hhyhf'
	exec sp_getdata @starttime0,@tempdate0,'first','hqws_hys_hhyhf'
	exec sp_getdata @starttime0,@tempdate0,'first','hqws_hys_jnhsl'
	exec sp_getdata @starttime0,@tempdate0,'first','hqws_hys_jnhf'
	exec sp_getdata @starttime0,@tempdate0,'first','hqws_hys_sdtshsl'
	exec sp_getdata @starttime0,@tempdate0,'first','hqws_hys_nswnhf'
	exec sp_getdata @starttime0,@tempdate0,'first','hqws_hys_nbhsl'
	exec sp_getdata @starttime0,@tempdate0,'first','hqws_hys_jnhf'


	set @starttime = @tempdate
end

Select * from TB_MP_TEMP_RPT order by itemid


GO

-- ----------------------------
-- Procedure structure for sp_mth_04
-- ----------------------------
DROP PROCEDURE [dbo].[sp_mth_04]
GO



CREATE PROCEDURE [dbo].[sp_mth_04]
@stime varchar(10),  @starthour int,  @intval int, @mthsdt int  
 AS
declare @starttime datetime
declare @starttime1 datetime
declare @starttime2 datetime
declare @endtime datetime
declare @tempdate datetime

Select @endtime=cast(@stime as datetime)
Select @endtime=DateAdd(hour,@starthour,@endtime) 
--Select @endtime=dateadd(minute,30,@endtime)  --增加半小时

Select @starttime=cast(@stime as datetime)
if(DATEPART(day,@starttime)>@mthsdt)
begin
	set @starttime = cast (cast(DATEPART(yyyy,@starttime)  as varchar)+'-'+cast(DATEPART(mm,@starttime)  as varchar)+'-'+cast(@mthsdt as varchar) as datetime)
	set @starttime = dateadd(month,-2,@starttime)
	Select @starttime=DateAdd(hour,@starthour,@starttime) 
	select @endtime=dateadd(MONTH,1,@starttime) 
end
else
begin
	
	set @starttime = cast (cast(DATEPART(yyyy,@starttime)  as varchar)+'-'+cast(DATEPART(mm,@starttime)  as varchar)+'-'+cast(@mthsdt as varchar) as datetime)
	set @starttime = dateadd(month,-2,@starttime)
	Select @starttime=DateAdd(hour,@starthour,@starttime) 
	select @endtime=dateadd(MONTH,1,@starttime)
end 
--Select @starttime=dateadd(minute,30,@starttime)  --增加半小时
select @starttime1=dateadd(MONTH,-1,@starttime)
select @starttime2=dateadd(MONTH,1,@starttime1)
delete from tb_mp_temp_rpt

while(@starttime<@endtime)
begin
	set @tempdate = dateadd(DAY,@intval,@starttime)
	insert into TB_MP_TEMP_RPT(ParmValue,MeasureDT) values (DATEPART(day,@starttime),@starttime)
	exec sp_getdata @starttime,@tempdate,'first','JS_FL1_TOT1'
	exec sp_getdata @starttime,@tempdate,'first','JS_FL2_TOT1'
	exec sp_getdata @starttime,@tempdate,'first','JS_FL3_TOT1'
	exec sp_getdata @starttime,@tempdate,'first','JS_FL4_TOT1'
	exec sp_getdata @starttime,@tempdate,'first','JS_FL5_TOT1'
	exec sp_getdata @starttime,@tempdate,'first','JS_FL6_TOT1'
	exec sp_getdata @starttime1,@starttime2,'first','JS_FL1_TOT1'
	exec sp_getdata @starttime1,@starttime2,'first','JS_FL2_TOT1'
	exec sp_getdata @starttime1,@starttime2,'first','JS_FL3_TOT1'
	exec sp_getdata @starttime1,@starttime2,'first','JS_FL4_TOT1'
	exec sp_getdata @starttime1,@starttime2,'first','JS_FL5_TOT1'
	exec sp_getdata @starttime1,@starttime2,'first','JS_FL6_TOT1'


	set @starttime = @tempdate
end

Select * from TB_MP_TEMP_RPT order by itemid


GO

-- ----------------------------
-- Procedure structure for sp_mth_05
-- ----------------------------
DROP PROCEDURE [dbo].[sp_mth_05]
GO



CREATE PROCEDURE [dbo].[sp_mth_05]
@stime varchar(10),  @starthour int,  @intval int, @mthsdt int  
 AS
declare @starttime datetime
declare @starttime1 datetime
declare @endtime datetime
declare @tempdate datetime
declare @starttimeadd datetime
declare @tempdateadd datetime
declare @starttime0 datetime
declare @tempdate0 datetime
Select @endtime=cast(@stime as datetime)
Select @endtime=DateAdd(hour,@starthour,@endtime) 
--Select @endtime=dateadd(minute,30,@endtime)  --增加半小时

Select @starttime=cast(@stime as datetime)
if(DATEPART(day,@starttime)>@mthsdt)
begin
	set @starttime = cast (cast(DATEPART(yyyy,@starttime)  as varchar)+'-'+cast(DATEPART(mm,@starttime)  as varchar)+'-'+cast(@mthsdt as varchar) as datetime)
	set @starttime = dateadd(month,-2,@starttime)
	Select @starttime=DateAdd(hour,@starthour,@starttime) 
	select @endtime=dateadd(MONTH,1,@starttime) 
end
else
begin
	
	set @starttime = cast (cast(DATEPART(yyyy,@starttime)  as varchar)+'-'+cast(DATEPART(mm,@starttime)  as varchar)+'-'+cast(@mthsdt as varchar) as datetime)
	set @starttime = dateadd(month,-2,@starttime)
	Select @starttime=DateAdd(hour,@starthour,@starttime) 
	select @endtime=dateadd(MONTH,1,@starttime)
end 
--Select @starttime=dateadd(minute,30,@starttime)  --增加半小时
select @starttime1=dateadd(MONTH,-1,@starttime)

delete from tb_mp_temp_rpt

while(@starttime<@endtime)
begin
	set @tempdate = dateadd(DAY,@intval,@starttime)
	set @starttimeadd=dateadd(DAY,1,@starttime)
	set @starttimeadd=dateadd(MINUTE,5,@starttimeadd)
	set @tempdateadd=dateadd(DAY,1,@tempdate)
	SET @starttime0=DATEADD(HOUR,-DATEPART(HOUR,@starttime),@starttime)
	SET @tempdate0=DATEADD(HOUR,-DATEPART(HOUR,@tempdate),@tempdate)
	insert into TB_MP_TEMP_RPT(ParmValue,MeasureDT) values (DATEPART(day,@starttime),@starttime)
	exec sp_getdata @starttime,@tempdate,'first','-'
	exec sp_getdata @starttime0,@tempdate0,'first','hqws_zks_1dbzlj'
	exec sp_getdata @starttime,@tempdate,'first','-'
	exec sp_getdata @starttime0,@tempdate0,'first','hqws_zks_2dbzlj'
	exec sp_getdata @starttime,@tempdate,'first','-'
	exec sp_getdata @starttime0,@tempdate0,'first','hqws_zks_gldbzlj'
	exec sp_getdata @starttime,@tempdate,'first','-'
	exec sp_getdata @starttime0,@tempdate0,'first','hqws_zks_zlszlj'
	exec sp_getdata @starttime,@tempdate,'diff','FT_tot_comm'
	exec sp_getdata @starttime,@tempdate,'diff','-'
	exec sp_getdata @starttime,@tempdate,'diff','-'
	exec sp_getdata @starttime,@tempdate,'max','zuizhong55'
	exec sp_getdata @starttime,@tempdate,'max','zuizhong56'
	exec sp_getdata @starttime,@tempdate,'diff','-'
	exec sp_getdata @starttimeadd,@tempdateadd,'first','zuizhong60'
	exec sp_getdata @starttimeadd,@tempdateadd,'first','ZS_FL_TOT1_Y'
	exec sp_getdata @starttime0,@tempdate0,'first','hqws_zks_wnclpam'
	exec sp_getdata @starttimeadd,@tempdateadd,'first','CS_FL_TOT1_Y'
	exec sp_getdata @starttimeadd,@tempdateadd,'first','GFJF_AIR_TOT1_C_Y'
	exec sp_getdata @starttimeadd,@tempdateadd,'first','GFJF_AIR_TOT2_C_Y'
	exec sp_getdata @starttime,@tempdate,'max','zizhan11_real17'
	exec sp_getdata @starttime,@tempdate,'max','zizhan12_real17'
	exec sp_getdata @starttime,@tempdate,'max','zizhan13_real17'
	exec sp_getdata @starttime,@tempdate,'max','zizhan14_real17'
	exec sp_getdata @starttime,@tempdate,'max','zizhan15_real17'
	exec sp_getdata @starttime,@tempdate,'max','zizhan16_real17'

	set @starttime = @tempdate
end

Select * from TB_MP_TEMP_RPT order by itemid


GO

-- ----------------------------
-- Procedure structure for sp_mth_06
-- ----------------------------
DROP PROCEDURE [dbo].[sp_mth_06]
GO



CREATE PROCEDURE [dbo].[sp_mth_06]
@stime varchar(10),  @starthour int,  @intval int, @mthsdt int  
 AS
declare @starttime datetime
declare @starttime1 datetime
declare @endtime datetime
declare @tempdate datetime

Select @endtime=cast(@stime as datetime)
Select @endtime=DateAdd(hour,@starthour,@endtime) 
--Select @endtime=dateadd(minute,30,@endtime)  --增加半小时

Select @starttime=cast(@stime as datetime)
if(DATEPART(day,@starttime)>@mthsdt)
begin
	set @starttime = cast (cast(DATEPART(yyyy,@starttime)  as varchar)+'-'+cast(DATEPART(mm,@starttime)  as varchar)+'-'+cast(@mthsdt as varchar) as datetime)
	set @starttime = dateadd(month,-2,@starttime)
	Select @starttime=DateAdd(hour,@starthour,@starttime) 
	select @endtime=dateadd(MONTH,1,@starttime) 
end
else
begin
	
	set @starttime = cast (cast(DATEPART(yyyy,@starttime)  as varchar)+'-'+cast(DATEPART(mm,@starttime)  as varchar)+'-'+cast(@mthsdt as varchar) as datetime)
	set @starttime = dateadd(month,-2,@starttime)
	Select @starttime=DateAdd(hour,@starthour,@starttime) 
	select @endtime=dateadd(MONTH,1,@starttime)
end 
--Select @starttime=dateadd(minute,30,@starttime)  --增加半小时
select @starttime1=dateadd(MONTH,-1,@starttime)

delete from tb_mp_temp_rpt

while(@starttime<@endtime)
begin
	set @tempdate = dateadd(DAY,@intval,@starttime)
	--insert into TB_MP_TEMP_RPT(ParmValue,MeasureDT) values (DATEPART(day,@starttime),@starttime)
	exec sp_getdata @starttime,@tempdate,'DIFF','FT_tot1_comm_ccna'
	exec sp_getdata @starttime,@tempdate,'DIFF','FT_tot2_comm_ccna'
	exec sp_getdata @starttime,@tempdate,'DIFF','FT_tot3_comm_ccna'
	exec sp_getdata @starttime,@tempdate,'DIFF','FT_tot4_comm_ccna'
	exec sp_getdata @starttime,@tempdate,'diff','FT_tot1_comm'
	exec sp_getdata @starttime,@tempdate,'diff','FT_tot2_comm'
	exec sp_getdata @starttime,@tempdate,'diff','FT_tot3_comm'
	exec sp_getdata @starttime,@tempdate,'diff','FT_tot4_comm'
	exec sp_getdata @starttime,@tempdate,'max','zui_zhong3'
	exec sp_getdata @starttime,@tempdate,'max','zui_zhong4'

	
	

	set @starttime = @tempdate
end

Select * from TB_MP_TEMP_RPT order by itemid


GO

-- ----------------------------
-- Procedure structure for sp_mth_07
-- ----------------------------
DROP PROCEDURE [dbo].[sp_mth_07]
GO



CREATE PROCEDURE [dbo].[sp_mth_07]
@stime varchar(10),  @starthour int,  @intval int, @mthsdt int  
 AS
declare @starttime datetime
declare @starttime1 datetime
declare @endtime datetime
declare @tempdate datetime
declare @starttimeadd datetime
declare @tempdateadd datetime
declare @starttime0 datetime
declare @tempdate0 datetime
Select @endtime=cast(@stime as datetime)
Select @endtime=DateAdd(hour,@starthour,@endtime) 
--Select @endtime=dateadd(minute,30,@endtime)  --增加半小时

Select @starttime=cast(@stime as datetime)
if(DATEPART(day,@starttime)>@mthsdt)
begin
	set @starttime = cast (cast(DATEPART(yyyy,@starttime)  as varchar)+'-'+cast(DATEPART(mm,@starttime)  as varchar)+'-'+cast(@mthsdt as varchar) as datetime)
	set @starttime = dateadd(month,-2,@starttime)
	Select @starttime=DateAdd(hour,@starthour,@starttime) 
	select @endtime=dateadd(MONTH,1,@starttime) 
end
else
begin
	
	set @starttime = cast (cast(DATEPART(yyyy,@starttime)  as varchar)+'-'+cast(DATEPART(mm,@starttime)  as varchar)+'-'+cast(@mthsdt as varchar) as datetime)
	set @starttime = dateadd(month,-2,@starttime)
	Select @starttime=DateAdd(hour,@starthour,@starttime) 
	select @endtime=dateadd(MONTH,1,@starttime)
end 
--Select @starttime=dateadd(minute,30,@starttime)  --增加半小时
select @starttime1=dateadd(MONTH,-1,@starttime)

delete from tb_mp_temp_rpt

while(@starttime<@endtime)
begin
	set @tempdate = dateadd(DAY,@intval,@starttime)
	set @starttimeadd=dateadd(DAY,1,@starttime)
	set @starttimeadd=dateadd(MINUTE,5,@starttimeadd)
	set @tempdateadd=dateadd(DAY,1,@tempdate)
	SET @starttime0=DATEADD(HOUR,-DATEPART(HOUR,@starttime),@starttime)
	SET @tempdate0=DATEADD(HOUR,-DATEPART(HOUR,@tempdate),@tempdate)
	insert into TB_MP_TEMP_RPT(ParmValue,MeasureDT) values (DATEPART(day,@starttime),@starttime)
	
	exec sp_getdata @starttimeadd,@tempdateadd,'first','SHC_ZLB1_RT_P_RT_Y'
	exec sp_getdata @starttimeadd,@tempdateadd,'first','SHC_ZLB2_RT_P_RT_Y'
	exec sp_getdata @starttimeadd,@tempdateadd,'first','SHC_ZLB3_RT_P_RT_Y'
	exec sp_getdata @starttimeadd,@tempdateadd,'first','SHC_ZLB4_RT_P_RT_Y'


	set @starttime = @tempdate
end

Select * from TB_MP_TEMP_RPT order by itemid


GO

-- ----------------------------
-- Procedure structure for sp_mth_08
-- ----------------------------
DROP PROCEDURE [dbo].[sp_mth_08]
GO



CREATE PROCEDURE [dbo].[sp_mth_08]
@stime varchar(10),  @starthour int,  @intval int, @mthsdt int  
 AS
declare @starttime datetime
declare @endtime datetime
declare @tempdate datetime

Select @starttime=cast(@stime as datetime)
if(DATEPART(day,@starttime)>@mthsdt)
begin

	set @starttime = cast (cast(DATEPART(yyyy,@starttime)  as varchar)+'-'+cast(DATEPART(mm,@starttime)  as varchar)+'-'+cast(@mthsdt as varchar) as datetime)
	set @starttime = dateadd(month,-2,@starttime)
	Select @starttime=DateAdd(hour,@starthour,@starttime) 
	select @endtime=dateadd(MONTH,1,@starttime)
end
else
begin

	set @starttime = cast (cast(DATEPART(yyyy,@starttime)  as varchar)+'-'+cast(DATEPART(mm,@starttime)  as varchar)+'-'+cast(@mthsdt as varchar) as datetime)
	set @starttime = dateadd(month,-2,@starttime)
	Select @starttime=DateAdd(hour,@starthour,@starttime) 
	select @endtime=dateadd(MONTH,1,@starttime)
end 

--Select @starttime=cast(@stime as datetime)
--if(DATEPART(day,@starttime)>@mthsdt)
--begin
--	set @starttime = cast (cast(DATEPART(yyyy,@starttime)  as varchar)+'-'+cast(DATEPART(mm,@starttime)  as varchar)+'-'+cast(@mthsdt as varchar) as datetime)
--	Select @starttime=DateAdd(hour,@starthour,@starttime) 
--end
--else
--begin
--	set @starttime = dateadd(month,-1,@starttime)
--	set @starttime = cast (cast(DATEPART(yyyy,@starttime)  as varchar)+'-'+cast(DATEPART(mm,@starttime)  as varchar)+'-'+cast(@mthsdt as varchar) as datetime)
--	Select @starttime=DateAdd(hour,@starthour,@starttime) 
--end 



delete from tb_mp_temp_rpt

while(@starttime<@endtime)
begin
	set @tempdate = dateadd(DAY,@intval,@starttime)
	insert into TB_MP_TEMP_RPT(ParmValue,MeasureDT) values (DATEPART(day,@starttime),@starttime)

	exec sp_getdata @starttime,@tempdate,'diff','JS_P1_P_RT'
	exec sp_getdata @starttime,@tempdate,'diff','JS_P2_P_RT'
	exec sp_getdata @starttime,@tempdate,'diff','JS_P3_P_RT'
	exec sp_getdata @starttime,@tempdate,'diff','JS_P4_P_RT'
	exec sp_getdata @starttime,@tempdate,'diff','JS_P5_P_RT'
	exec sp_getdata @starttime,@tempdate,'diff','JS_P6_P_RT'

	exec sp_getdata @starttime,@tempdate,'diff','GFJ1_VFD_Running_Hours'
	exec sp_getdata @starttime,@tempdate,'diff','GFJ2_VFD_Running_Hours'
	exec sp_getdata @starttime,@tempdate,'diff','GFJ3_VFD_Running_Hours'
	exec sp_getdata @starttime,@tempdate,'diff','GFJ4_VFD_Running_Hours'
	exec sp_getdata @starttime,@tempdate,'diff','GFJ5_VFD_Running_Hours'
	exec sp_getdata @starttime,@tempdate,'diff','GFJ6_VFD_Running_Hours'

	exec sp_getdata @starttime,@tempdate,'diff','CGS_P1_P_RT'
	exec sp_getdata @starttime,@tempdate,'diff','CGS_P2_P_RT'
	exec sp_getdata @starttime,@tempdate,'diff','CGS_P3_P_RT'
	exec sp_getdata @starttime,@tempdate,'diff','CGS_P4_P_RT'

	exec sp_getdata @starttime,@tempdate,'diff','F14_1'
	exec sp_getdata @starttime,@tempdate,'diff','F14_3'
	exec sp_getdata @starttime,@tempdate,'diff','F14_5'
	exec sp_getdata @starttime,@tempdate,'diff','xgs2F14_1'
	exec sp_getdata @starttime,@tempdate,'diff','xgs2F14_3'

	exec sp_getdata @starttime,@tempdate,'diff','-'
	exec sp_getdata @starttime,@tempdate,'diff','-'
	exec sp_getdata @starttime,@tempdate,'diff','-'

	exec sp_getdata @starttime,@tempdate,'diff60','zizhan11_real'
	exec sp_getdata @starttime,@tempdate,'diff60','zizhan12_real'
	exec sp_getdata @starttime,@tempdate,'diff60','zizhan13_real'
	exec sp_getdata @starttime,@tempdate,'diff60','zizhan14_real'
	exec sp_getdata @starttime,@tempdate,'diff60','zizhan15_real'
	exec sp_getdata @starttime,@tempdate,'diff60','zizhan16_real'

	exec sp_getdata @starttime,@tempdate,'diff','time1'
	exec sp_getdata @starttime,@tempdate,'diff','time2'
	exec sp_getdata @starttime,@tempdate,'diff','time3'
	exec sp_getdata @starttime,@tempdate,'diff','time4'

	exec sp_getdata @starttime,@tempdate,'diff','CS_ZLB1_P_RT'
	exec sp_getdata @starttime,@tempdate,'diff','CS_ZLB2_P_RT'
	exec sp_getdata @starttime,@tempdate,'diff','CS_ZLB3_P_RT'
	exec sp_getdata @starttime,@tempdate,'diff','CS_ZLB4_P_RT'
	exec sp_getdata @starttime,@tempdate,'diff','CS_ZLB5_P_RT'

	exec sp_getdata @starttime,@tempdate,'diff','JS_FL_TOT1'
	exec sp_getdata @starttime,@tempdate,'diff','CS_FL_TOT1'

	set @starttime = @tempdate
end

Select * from TB_MP_TEMP_RPT order by itemid


GO

-- ----------------------------
-- Procedure structure for sp_mth_081
-- ----------------------------
DROP PROCEDURE [dbo].[sp_mth_081]
GO



CREATE PROCEDURE [dbo].[sp_mth_081]
@stime varchar(10),  @starthour int,  @intval int, @mthsdt int  
 AS
declare @starttime datetime
declare @endtime datetime
declare @tempdate datetime

Select @starttime=cast(@stime as datetime)
if(DATEPART(day,@starttime)>@mthsdt)
begin

	set @starttime = cast (cast(DATEPART(yyyy,@starttime)  as varchar)+'-'+cast(DATEPART(mm,@starttime)  as varchar)+'-'+cast(@mthsdt as varchar) as datetime)
	set @starttime = dateadd(month,-2,@starttime)
	Select @starttime=DateAdd(hour,@starthour,@starttime) 
	select @endtime=dateadd(MONTH,1,@starttime)
end
else
begin

	set @starttime = cast (cast(DATEPART(yyyy,@starttime)  as varchar)+'-'+cast(DATEPART(mm,@starttime)  as varchar)+'-'+cast(@mthsdt as varchar) as datetime)
	set @starttime = dateadd(month,-2,@starttime)
	Select @starttime=DateAdd(hour,@starthour,@starttime) 
	select @endtime=dateadd(MONTH,1,@starttime)
end 

--Select @starttime=cast(@stime as datetime)
--if(DATEPART(day,@starttime)>@mthsdt)
--begin
--	set @starttime = cast (cast(DATEPART(yyyy,@starttime)  as varchar)+'-'+cast(DATEPART(mm,@starttime)  as varchar)+'-'+cast(@mthsdt as varchar) as datetime)
--	Select @starttime=DateAdd(hour,@starthour,@starttime) 
--end
--else
--begin
--	set @starttime = dateadd(month,-1,@starttime)
--	set @starttime = cast (cast(DATEPART(yyyy,@starttime)  as varchar)+'-'+cast(DATEPART(mm,@starttime)  as varchar)+'-'+cast(@mthsdt as varchar) as datetime)
--	Select @starttime=DateAdd(hour,@starthour,@starttime) 
--end 



delete from tb_mp_temp_rpt

while(@starttime<@endtime)
begin
	set @tempdate = dateadd(DAY,@intval,@starttime)
	

	exec sp_getdata @starttime,@tempdate,'diff','SHC_SPZLB1_RT_P_RT'
	exec sp_getdata @starttime,@tempdate,'diff','SHC_ZLB1_RT_P_RT'
	exec sp_getdata @starttime,@tempdate,'diff','SHC_SPZLB2_RT_P_RT'
	exec sp_getdata @starttime,@tempdate,'diff','SHC_ZLB2_RT_P_RT'
	exec sp_getdata @starttime,@tempdate,'diff','SHC_SPZLB3_RT_P_RT'
	exec sp_getdata @starttime,@tempdate,'diff','SHC_ZLB3_RT_P_RT'
	exec sp_getdata @starttime,@tempdate,'diff','SHC_SPZLB4_RT_P_RT'
	exec sp_getdata @starttime,@tempdate,'diff','SHC_ZLB4_RT_P_RT'
	exec sp_getdata @starttime,@tempdate,'diff','SHC_SPZLB5_RT_P_RT'
	exec sp_getdata @starttime,@tempdate,'diff','SHC_ZLB5_RT_P_RT'
	exec sp_getdata @starttime,@tempdate,'diff','SHC_SPZLB6_RT_P_RT'
	exec sp_getdata @starttime,@tempdate,'diff','SHC_ZLB6_RT_P_RT'
	exec sp_getdata @starttime,@tempdate,'diff','SHC_SPZLB7_RT_P_RT'
	exec sp_getdata @starttime,@tempdate,'diff','SHC_ZLB7_RT_P_RT'
	exec sp_getdata @starttime,@tempdate,'diff','SHC_SPZLB8_RT_P_RT'
	exec sp_getdata @starttime,@tempdate,'diff','SHC_ZLB8_RT_P_RT'
	exec sp_getdata @starttime,@tempdate,'diff','SHC_SPZLB9_RT_P_RT'
	exec sp_getdata @starttime,@tempdate,'diff','-'
	exec sp_getdata @starttime,@tempdate,'diff','SHC_SPZLB10_RT_P_RT'
	exec sp_getdata @starttime,@tempdate,'diff','-'
	exec sp_getdata @starttime,@tempdate,'diff','SHC_SPZLB11_RT_P_RT'
	exec sp_getdata @starttime,@tempdate,'diff','-'
	exec sp_getdata @starttime,@tempdate,'diff','SHC_SPZLB12_RT_P_RT'
	exec sp_getdata @starttime,@tempdate,'diff','-'
	exec sp_getdata @starttime,@tempdate,'diff','SHC_SPZLB13_RT_P_RT'
	exec sp_getdata @starttime,@tempdate,'diff','-'
	exec sp_getdata @starttime,@tempdate,'diff','SHC_SPZLB14_RT_P_RT'
	exec sp_getdata @starttime,@tempdate,'diff','-'
	exec sp_getdata @starttime,@tempdate,'diff','SHC_SPZLB15_RT_P_RT'
	exec sp_getdata @starttime,@tempdate,'diff','-'
	exec sp_getdata @starttime,@tempdate,'diff','SHC_SPZLB16_RT_P_RT'
	exec sp_getdata @starttime,@tempdate,'diff','-'
	exec sp_getdata @starttime,@tempdate,'diff','-'
	exec sp_getdata @starttime,@tempdate,'diff','-'
	set @starttime = @tempdate
end

Select * from TB_MP_TEMP_RPT order by itemid


GO

-- ----------------------------
-- Procedure structure for sp_mth_082
-- ----------------------------
DROP PROCEDURE [dbo].[sp_mth_082]
GO



CREATE PROCEDURE [dbo].[sp_mth_082]
@stime varchar(10),  @starthour int,  @intval int, @mthsdt int  
 AS
declare @starttime datetime
declare @endtime datetime
declare @tempdate datetime

Select @starttime=cast(@stime as datetime)
if(DATEPART(day,@starttime)>@mthsdt)
begin

	set @starttime = cast (cast(DATEPART(yyyy,@starttime)  as varchar)+'-'+cast(DATEPART(mm,@starttime)  as varchar)+'-'+cast(@mthsdt as varchar) as datetime)
	set @starttime = dateadd(month,-2,@starttime)
	Select @starttime=DateAdd(hour,@starthour,@starttime) 
	select @endtime=dateadd(MONTH,1,@starttime)
end
else
begin

	set @starttime = cast (cast(DATEPART(yyyy,@starttime)  as varchar)+'-'+cast(DATEPART(mm,@starttime)  as varchar)+'-'+cast(@mthsdt as varchar) as datetime)
	set @starttime = dateadd(month,-2,@starttime)
	Select @starttime=DateAdd(hour,@starthour,@starttime) 
	select @endtime=dateadd(MONTH,1,@starttime)
end 

--Select @starttime=cast(@stime as datetime)
--if(DATEPART(day,@starttime)>@mthsdt)
--begin
--	set @starttime = cast (cast(DATEPART(yyyy,@starttime)  as varchar)+'-'+cast(DATEPART(mm,@starttime)  as varchar)+'-'+cast(@mthsdt as varchar) as datetime)
--	Select @starttime=DateAdd(hour,@starthour,@starttime) 
--end
--else
--begin
--	set @starttime = dateadd(month,-1,@starttime)
--	set @starttime = cast (cast(DATEPART(yyyy,@starttime)  as varchar)+'-'+cast(DATEPART(mm,@starttime)  as varchar)+'-'+cast(@mthsdt as varchar) as datetime)
--	Select @starttime=DateAdd(hour,@starthour,@starttime) 
--end 



delete from tb_mp_temp_rpt

while(@starttime<@endtime)
begin
	set @tempdate = dateadd(DAY,@intval,@starttime)
	

	exec sp_getdata @starttime,@tempdate,'diff1','TXC_LVL1_AI_EV'
	exec sp_getdata @starttime,@tempdate,'diff1','TXC_LVL2_AI_EV'
	exec sp_getdata @starttime,@tempdate,'max','TXC_LVL1_AI_EV'
	exec sp_getdata @starttime,@tempdate,'min','TXC_LVL1_AI_EV'
	exec sp_getdata @starttime,@tempdate,'max','TXC_LVL2_AI_EV'
	exec sp_getdata @starttime,@tempdate,'min','TXC_LVL2_AI_EV'

	set @starttime = @tempdate
end

Select * from TB_MP_TEMP_RPT order by itemid


GO

-- ----------------------------
-- Procedure structure for sp_mth_09
-- ----------------------------
DROP PROCEDURE [dbo].[sp_mth_09]
GO



CREATE PROCEDURE [dbo].[sp_mth_09]
@stime varchar(10),  @starthour int,  @intval int, @mthsdt int  
 AS
declare @starttime datetime
declare @endtime datetime
declare @tempdate datetime

Select @starttime=cast(@stime as datetime)
if(DATEPART(day,@starttime)>@mthsdt)
begin

	set @starttime = cast (cast(DATEPART(yyyy,@starttime)  as varchar)+'-'+cast(DATEPART(mm,@starttime)  as varchar)+'-'+cast(@mthsdt as varchar) as datetime)
	set @starttime = dateadd(month,-2,@starttime)
	Select @starttime=DateAdd(hour,@starthour,@starttime) 
	select @endtime=dateadd(MONTH,1,@starttime)
end
else
begin

	set @starttime = cast (cast(DATEPART(yyyy,@starttime)  as varchar)+'-'+cast(DATEPART(mm,@starttime)  as varchar)+'-'+cast(@mthsdt as varchar) as datetime)
	set @starttime = dateadd(month,-2,@starttime)
	Select @starttime=DateAdd(hour,@starthour,@starttime) 
	select @endtime=dateadd(MONTH,1,@starttime)
end 

--Select @starttime=cast(@stime as datetime)
--if(DATEPART(day,@starttime)>@mthsdt)
--begin
--	set @starttime = cast (cast(DATEPART(yyyy,@starttime)  as varchar)+'-'+cast(DATEPART(mm,@starttime)  as varchar)+'-'+cast(@mthsdt as varchar) as datetime)
--	Select @starttime=DateAdd(hour,@starthour,@starttime) 
--end
--else
--begin
--	set @starttime = dateadd(month,-1,@starttime)
--	set @starttime = cast (cast(DATEPART(yyyy,@starttime)  as varchar)+'-'+cast(DATEPART(mm,@starttime)  as varchar)+'-'+cast(@mthsdt as varchar) as datetime)
--	Select @starttime=DateAdd(hour,@starthour,@starttime) 
--end 



delete from tb_mp_temp_rpt

while(@starttime<@endtime)
begin
	set @tempdate = dateadd(MONTH,@intval,@starttime)
	insert into TB_MP_TEMP_RPT(ParmValue,MeasureDT) values (DATEPART(day,@starttime),@starttime)

	exec sp_getdata @starttime,@tempdate,'last','JS_P1_P_RT'
	exec sp_getdata @starttime,@tempdate,'last','JS_P2_P_RT'
	exec sp_getdata @starttime,@tempdate,'last','JS_P3_P_RT'
	exec sp_getdata @starttime,@tempdate,'last','JS_P4_P_RT'
	exec sp_getdata @starttime,@tempdate,'last','JS_P5_P_RT'
	exec sp_getdata @starttime,@tempdate,'last','JS_P6_P_RT'

	exec sp_getdata @starttime,@tempdate,'last','GFJ1_VFD_Running_Hours'
	exec sp_getdata @starttime,@tempdate,'last','GFJ2_VFD_Running_Hours'
	exec sp_getdata @starttime,@tempdate,'last','GFJ3_VFD_Running_Hours'
	exec sp_getdata @starttime,@tempdate,'last','GFJ4_VFD_Running_Hours'
	exec sp_getdata @starttime,@tempdate,'last','GFJ5_VFD_Running_Hours'
	exec sp_getdata @starttime,@tempdate,'last','GFJ6_VFD_Running_Hours'

	exec sp_getdata @starttime,@tempdate,'last','CGS_P1_P_RT'
	exec sp_getdata @starttime,@tempdate,'last','CGS_P1_P_RT'
	exec sp_getdata @starttime,@tempdate,'last','CGS_P1_P_RT'
	exec sp_getdata @starttime,@tempdate,'last','CGS_P1_P_RT'

	exec sp_getdata @starttime,@tempdate,'last','F14_1'
	exec sp_getdata @starttime,@tempdate,'last','F14_3'
	exec sp_getdata @starttime,@tempdate,'last','F14_5'
	exec sp_getdata @starttime,@tempdate,'last','xgs2F14_1'
	exec sp_getdata @starttime,@tempdate,'last','xgs2F14_3'

	exec sp_getdata @starttime,@tempdate,'last','-'
	exec sp_getdata @starttime,@tempdate,'last','-'
	exec sp_getdata @starttime,@tempdate,'last','-'

	exec sp_getdata @starttime,@tempdate,'last','zizhan11_real'
	exec sp_getdata @starttime,@tempdate,'last','zizhan12_real'
	exec sp_getdata @starttime,@tempdate,'last','zizhan13_real'
	exec sp_getdata @starttime,@tempdate,'last','zizhan14_real'
	exec sp_getdata @starttime,@tempdate,'last','zizhan15_real'
	exec sp_getdata @starttime,@tempdate,'last','zizhan16_real'

	exec sp_getdata @starttime,@tempdate,'last','time1'
	exec sp_getdata @starttime,@tempdate,'last','time2'
	exec sp_getdata @starttime,@tempdate,'last','time3'
	exec sp_getdata @starttime,@tempdate,'last','time4'

	exec sp_getdata @starttime,@tempdate,'last','CS_ZLB1_P_RT'
	exec sp_getdata @starttime,@tempdate,'last','CS_ZLB2_P_RT'
	exec sp_getdata @starttime,@tempdate,'last','CS_ZLB3_P_RT'
	exec sp_getdata @starttime,@tempdate,'last','CS_ZLB4_P_RT'
	exec sp_getdata @starttime,@tempdate,'last','CS_ZLB5_P_RT'

	exec sp_getdata @starttime,@tempdate,'last','JS_FL_TOT1'

	exec sp_getdata @starttime,@tempdate,'last','CS_FL_TOT1'

	set @starttime = @tempdate
end

Select * from TB_MP_TEMP_RPT order by itemid


GO

-- ----------------------------
-- Procedure structure for sp_mth_091
-- ----------------------------
DROP PROCEDURE [dbo].[sp_mth_091]
GO



CREATE PROCEDURE [dbo].[sp_mth_091]
@stime varchar(10),  @starthour int,  @intval int, @mthsdt int  
 AS
declare @starttime datetime
declare @endtime datetime
declare @tempdate datetime

Select @starttime=cast(@stime as datetime)
if(DATEPART(day,@starttime)>@mthsdt)
begin

	set @starttime = cast (cast(DATEPART(yyyy,@starttime)  as varchar)+'-'+cast(DATEPART(mm,@starttime)  as varchar)+'-'+cast(@mthsdt as varchar) as datetime)
	set @starttime = dateadd(month,-2,@starttime)
	Select @starttime=DateAdd(hour,@starthour,@starttime) 
	select @endtime=dateadd(MONTH,1,@starttime)
end
else
begin

	set @starttime = cast (cast(DATEPART(yyyy,@starttime)  as varchar)+'-'+cast(DATEPART(mm,@starttime)  as varchar)+'-'+cast(@mthsdt as varchar) as datetime)
	set @starttime = dateadd(month,-2,@starttime)
	Select @starttime=DateAdd(hour,@starthour,@starttime) 
	select @endtime=dateadd(MONTH,1,@starttime)
end 

--Select @starttime=cast(@stime as datetime)
--if(DATEPART(day,@starttime)>@mthsdt)
--begin
--	set @starttime = cast (cast(DATEPART(yyyy,@starttime)  as varchar)+'-'+cast(DATEPART(mm,@starttime)  as varchar)+'-'+cast(@mthsdt as varchar) as datetime)
--	Select @starttime=DateAdd(hour,@starthour,@starttime) 
--end
--else
--begin
--	set @starttime = dateadd(month,-1,@starttime)
--	set @starttime = cast (cast(DATEPART(yyyy,@starttime)  as varchar)+'-'+cast(DATEPART(mm,@starttime)  as varchar)+'-'+cast(@mthsdt as varchar) as datetime)
--	Select @starttime=DateAdd(hour,@starthour,@starttime) 
--end 



delete from tb_mp_temp_rpt

while(@starttime<@endtime)
begin
	set @tempdate = dateadd(MONTH,@intval,@starttime)
	

	exec sp_getdata @starttime,@tempdate,'last','SHC_SPZLB1_RT_P_RT'
	exec sp_getdata @starttime,@tempdate,'last','SHC_ZLB1_RT_P_RT'
	exec sp_getdata @starttime,@tempdate,'last','SHC_SPZLB2_RT_P_RT'
	exec sp_getdata @starttime,@tempdate,'last','SHC_ZLB2_RT_P_RT'
	exec sp_getdata @starttime,@tempdate,'last','SHC_SPZLB3_RT_P_RT'
	exec sp_getdata @starttime,@tempdate,'last','SHC_ZLB3_RT_P_RT'
	exec sp_getdata @starttime,@tempdate,'last','SHC_SPZLB4_RT_P_RT'
	exec sp_getdata @starttime,@tempdate,'last','SHC_ZLB4_RT_P_RT'
	exec sp_getdata @starttime,@tempdate,'last','SHC_SPZLB5_RT_P_RT'
	exec sp_getdata @starttime,@tempdate,'last','SHC_ZLB5_RT_P_RT'
	exec sp_getdata @starttime,@tempdate,'last','SHC_SPZLB6_RT_P_RT'
	exec sp_getdata @starttime,@tempdate,'last','SHC_ZLB6_RT_P_RT'
	exec sp_getdata @starttime,@tempdate,'last','SHC_SPZLB7_RT_P_RT'
	exec sp_getdata @starttime,@tempdate,'last','SHC_ZLB7_RT_P_RT'
	exec sp_getdata @starttime,@tempdate,'last','SHC_SPZLB8_RT_P_RT'
	exec sp_getdata @starttime,@tempdate,'last','SHC_ZLB8_RT_P_RT'
	exec sp_getdata @starttime,@tempdate,'last','SHC_SPZLB9_RT_P_RT'
	exec sp_getdata @starttime,@tempdate,'last','-'
	exec sp_getdata @starttime,@tempdate,'last','SHC_SPZLB10_RT_P_RT'
	exec sp_getdata @starttime,@tempdate,'last','-'
	exec sp_getdata @starttime,@tempdate,'last','SHC_SPZLB11_RT_P_RT'
	exec sp_getdata @starttime,@tempdate,'last','-'
	exec sp_getdata @starttime,@tempdate,'last','SHC_SPZLB12_RT_P_RT'
	exec sp_getdata @starttime,@tempdate,'last','-'
	exec sp_getdata @starttime,@tempdate,'last','SHC_SPZLB13_RT_P_RT'
	exec sp_getdata @starttime,@tempdate,'last','-'
	exec sp_getdata @starttime,@tempdate,'last','SHC_SPZLB14_RT_P_RT'
	exec sp_getdata @starttime,@tempdate,'last','-'
	exec sp_getdata @starttime,@tempdate,'last','SHC_SPZLB15_RT_P_RT'
	exec sp_getdata @starttime,@tempdate,'last','-'
	exec sp_getdata @starttime,@tempdate,'last','SHC_SPZLB16_RT_P_RT'
	exec sp_getdata @starttime,@tempdate,'last','-'
	exec sp_getdata @starttime,@tempdate,'last','-'
	exec sp_getdata @starttime,@tempdate,'last','-'


	set @starttime = @tempdate
end

Select * from TB_MP_TEMP_RPT order by itemid


GO

-- ----------------------------
-- Procedure structure for sp_mth_10
-- ----------------------------
DROP PROCEDURE [dbo].[sp_mth_10]
GO



CREATE PROCEDURE [dbo].[sp_mth_10]
@stime varchar(10),  @starthour int,  @intval int, @mthsdt int  
 AS
declare @starttime datetime
declare @endtime datetime
declare @tempdate datetime

Select @starttime=cast(@stime as datetime)
if(DATEPART(day,@starttime)>@mthsdt)
begin

	set @starttime = cast (cast(DATEPART(yyyy,@starttime)  as varchar)+'-'+cast(DATEPART(mm,@starttime)  as varchar)+'-'+cast(@mthsdt as varchar) as datetime)
	set @starttime = dateadd(month,-2,@starttime)
	Select @starttime=DateAdd(hour,@starthour,@starttime) 
	select @endtime=dateadd(MONTH,1,@starttime)
end
else
begin

	set @starttime = cast (cast(DATEPART(yyyy,@starttime)  as varchar)+'-'+cast(DATEPART(mm,@starttime)  as varchar)+'-'+cast(@mthsdt as varchar) as datetime)
	set @starttime = dateadd(month,-2,@starttime)
	Select @starttime=DateAdd(hour,@starthour,@starttime) 
	select @endtime=dateadd(MONTH,1,@starttime)
end 

--Select @starttime=cast(@stime as datetime)
--if(DATEPART(day,@starttime)>@mthsdt)
--begin
--	set @starttime = cast (cast(DATEPART(yyyy,@starttime)  as varchar)+'-'+cast(DATEPART(mm,@starttime)  as varchar)+'-'+cast(@mthsdt as varchar) as datetime)
--	Select @starttime=DateAdd(hour,@starthour,@starttime) 
--end
--else
--begin
--	set @starttime = dateadd(month,-1,@starttime)
--	set @starttime = cast (cast(DATEPART(yyyy,@starttime)  as varchar)+'-'+cast(DATEPART(mm,@starttime)  as varchar)+'-'+cast(@mthsdt as varchar) as datetime)
--	Select @starttime=DateAdd(hour,@starthour,@starttime) 
--end 



delete from tb_mp_temp_rpt

while(@starttime<@endtime)
begin
	set @tempdate = dateadd(DAY,@intval,@starttime)
	insert into TB_MP_TEMP_RPT(ParmValue,MeasureDT) values (DATEPART(day,@starttime),@starttime)

	exec sp_getdata @starttime,@tempdate,'diff24','TXC_YM2_RT_P_RT_M'
	exec sp_getdata @starttime,@tempdate,'diff24','TXC_YM1_RT_P_RT_M'
	exec sp_getdata @starttime,@tempdate,'diff24','-'
	exec sp_getdata @starttime,@tempdate,'diff24','-'
	exec sp_getdata @starttime,@tempdate,'diff24','TXC_P1_RT_P_RT'
	exec sp_getdata @starttime,@tempdate,'diff24','TXC_P2_RT_P_RT'
	exec sp_getdata @starttime,@tempdate,'diff24','TXC_P5_RT_P_RT'

	exec sp_getdata @starttime,@tempdate,'diff24','-'

	exec sp_getdata @starttime,@tempdate,'diff24','TXC_P3_RT_P_RT'
	exec sp_getdata @starttime,@tempdate,'diff24','TXC_P4_RT_P_RT'
	exec sp_getdata @starttime,@tempdate,'diff24','TXC_P6_RT_P_RT'

	exec sp_getdata @starttime,@tempdate,'diff24','-'
	exec sp_getdata @starttime,@tempdate,'avg','TXC_LVL1_AI_EV'
	exec sp_getdata @starttime,@tempdate,'avg','TXC_LVL2_AI_EV'

	set @starttime = @tempdate
end

Select * from TB_MP_TEMP_RPT order by itemid


GO

-- ----------------------------
-- Procedure structure for sp_mth_11
-- ----------------------------
DROP PROCEDURE [dbo].[sp_mth_11]
GO



CREATE PROCEDURE [dbo].[sp_mth_11]
@stime varchar(10),  @starthour int,  @intval int, @mthsdt int  
 AS
declare @starttime datetime
declare @endtime datetime
declare @tempdate datetime

Select @starttime=cast(@stime as datetime)
if(DATEPART(day,@starttime)>@mthsdt)
begin

	set @starttime = cast (cast(DATEPART(yyyy,@starttime)  as varchar)+'-'+cast(DATEPART(mm,@starttime)  as varchar)+'-'+cast(@mthsdt as varchar) as datetime)
	set @starttime = dateadd(month,-1,@starttime)
	Select @starttime=DateAdd(hour,@starthour,@starttime) 
	select @endtime=dateadd(MONTH,1,@starttime)
end
else
begin

	set @starttime = cast (cast(DATEPART(yyyy,@starttime)  as varchar)+'-'+cast(DATEPART(mm,@starttime)  as varchar)+'-'+cast(@mthsdt as varchar) as datetime)
	set @starttime = dateadd(month,-1,@starttime)
	Select @starttime=DateAdd(hour,@starthour,@starttime) 
	select @endtime=dateadd(MONTH,1,@starttime)
end 

--Select @starttime=cast(@stime as datetime)
--if(DATEPART(day,@starttime)>@mthsdt)
--begin
--	set @starttime = cast (cast(DATEPART(yyyy,@starttime)  as varchar)+'-'+cast(DATEPART(mm,@starttime)  as varchar)+'-'+cast(@mthsdt as varchar) as datetime)
--	Select @starttime=DateAdd(hour,@starthour,@starttime) 
--end
--else
--begin
--	set @starttime = dateadd(month,-1,@starttime)
--	set @starttime = cast (cast(DATEPART(yyyy,@starttime)  as varchar)+'-'+cast(DATEPART(mm,@starttime)  as varchar)+'-'+cast(@mthsdt as varchar) as datetime)
--	Select @starttime=DateAdd(hour,@starthour,@starttime) 
--end 



delete from tb_mp_temp_rpt

while(@starttime<@endtime)
begin
	set @tempdate = dateadd(MONTH,@intval,@starttime)
	insert into TB_MP_TEMP_RPT(ParmValue,MeasureDT) values (DATEPART(day,@starttime),@starttime)


	exec sp_getdata @starttime,@tempdate,'diff','-'
	exec sp_getdata @starttime,@tempdate,'diff','-'
	exec sp_getdata @starttime,@tempdate,'diff','-'
	exec sp_getdata @starttime,@tempdate,'diff','-'
	exec sp_getdata @starttime,@tempdate,'diff','-'

	exec sp_getdata @starttime,@tempdate,'diff','-'

	exec sp_getdata @starttime,@tempdate,'diff','-'
	exec sp_getdata @starttime,@tempdate,'diff','-'
	exec sp_getdata @starttime,@tempdate,'diff','-'

	exec sp_getdata @starttime,@tempdate,'diff','-'


	set @starttime = @tempdate
end

Select * from TB_MP_TEMP_RPT order by itemid


GO

-- ----------------------------
-- Procedure structure for Sp_Production_Formula
-- ----------------------------
DROP PROCEDURE [dbo].[Sp_Production_Formula]
GO



CREATE PROCEDURE [dbo].[Sp_Production_Formula] 
 @execTime varchar(19),
 @execType varchar(20)
 as

 declare @mainMpCode varchar(50)--有公式的测量点
 declare @exp varchar(500)
 declare @nextTimes varchar(10)--第二天日期
 declare @lastTimes varchar(10)--昨天日期
  declare @nextMonth varchar(10)--下月
 set @nextTimes=CONVERT(varchar(10),dateadd(day,1,@execTime), 120 )
 set @lastTimes=CONVERT(varchar(10),dateadd(day,-1,@execTime), 120 )
 set @nextMonth=CONVERT(varchar(10),dateadd(month,1,@execTime), 120 )
  declare @execSt varchar(2)--记录是否执行的状态 0 执行 1 不执行  --为@execType目前只能放在附表服务
  set @execSt=0

 declare @tmpstr varchar(100)
 set @tmpstr=' ()+-*/,casewhenthenelseendpower'

	DECLARE formulaList CURSOR FOR SELECT MPointCode,exp FROM [TB_MeasurePoint]  where (exp is not null and exp!='')  order by morder
	open formulaList
	FETCH next FROM formulaList INTO @mainMpCode,@exp
	WHILE (@@FETCH_STATUS=0)
	BEGIN

	print @mainMpCode

	--建立有关动态分母的临时表
	create table #Tb_Denominator(denominatorname varchar(50),moleculename varchar(50),num int)
	declare @denominatorname varchar(50)
	declare @moleculename varchar(50)
	DECLARE DenominatorList CURSOR FOR SELECT denominatorName,moleculeName FROM [TB_MeasurePoint_Formula_Denominator]  where pmpid=''+@mainMpCode+'' order by denominatorName
	open DenominatorList
	FETCH next FROM DenominatorList INTO @denominatorname,@moleculename
	WHILE (@@FETCH_STATUS=0)
	BEGIN
		INSERT INTO #Tb_Denominator
		select @denominatorname,@moleculename,0
	FETCH next FROM DenominatorList INTO @denominatorname,@moleculename
	end
	CLOSE DenominatorList
	DEALLOCATE DenominatorList

	declare @denominatorCountNum int
	set @denominatorCountNum=(select count(*) from #Tb_Denominator)
	
	declare @sqlstr varchar(1000)
	set @sqlstr=''
    declare @cc varchar(50)--拆分后每行的数据
	set @exp = ltrim(rtrim(@exp))
	--print @exp
    declare c1 cursor for
	select a from dbo.f_get_exp_split(@exp,@tmpstr)
    open c1
    fetch next from c1 into @cc
    while @@fetch_status=0
    begin
	--print @cc
	 if (charindex(@cc,@tmpstr)>0)--是否为四则运算符
     begin
      set @sqlstr=@sqlstr+cast(@cc as varchar(50))
     end
	 else if (ISNUMERIC(@cc)=1)--是否为数字
     begin
		set @sqlstr=@sqlstr+cast(@cc as varchar(50))
     end
	 else
	 begin
	 print @cc
		declare @detailMpCode varchar(50)--公式中包含元素的测量点
		set @detailMpCode=''
		declare @mode varchar(50)--采集方式 first sum avg diff 等
		declare @starthour int--计算开始时间 0-0 8-8
		declare @datatype varchar(50)--时间类型 day month nowTime 等
		set @datatype=''

		declare @detailMpSQL nvarchar(4000)
		set @detailMpSQL=N''
		set @detailMpSQL=@detailMpSQL+N'select top 1 @detailMpCode=mpid,@mode=mode,@starthour=starthour,@datatype=datatype from [TB_MeasurePoint_Formula] where pmpid='''+@mainMpCode+''' and formulaname='''+@cc+''' and datatype='''+@execType+''' '
		EXEC sp_executesql @detailMpSQL,N'@detailMpCode varchar(50) output,@mode varchar(50) output,@starthour int output,@datatype varchar(50) output',@detailMpCode output,@mode output,@starthour output,@datatype output
		--print  @detailMpSQL
		if(@datatype='')
		begin
			set @execSt=1
		end
		else begin
			set @execSt=0
		end

		if(@starthour='' or @starthour is null)
		begin
			set @starthour=0
		end

		declare @dMpPv money--公式中包含元素的测量点
		set @dMpPv=0

		print @datatype
		declare @whereSql varchar(1000)--查询历史数据的where
		if(@datatype='day')
		begin
			set @whereSql=' where MeasureDT between '''+SUBSTRING(@execTime,0,11)+' '+cast(@starthour AS varchar(5))+':00:00'' and '''+SUBSTRING(@nextTimes,0,11)+' '+cast(@starthour AS varchar(5))+':00:00''  '
		end
		else if(@datatype='month')
		begin
			set @whereSql=' where MeasureDT between '''+SUBSTRING(@execTime,0,8)+'-01 '+cast(@starthour AS varchar(5))+':00:00'' and '''+SUBSTRING(CONVERT(varchar(10),dateadd(day,0,cast(''+dateadd(ms,-3,DATEADD(mm, DATEDIFF(m,0,''+@execTime+'')+1, 0))+'' as varchar(10))), 120 ),0,11)+' '+cast(@starthour AS varchar(5))+':00:00'' '
		end
		else if(@datatype='nowTime')
		begin
			declare @last6min varchar(19)
			set @last6min=dateadd(mi,-6,substring(@execTime,0,11)+' 00:00')
			set @whereSql=' where MeasureDT <='''+@execTime+''' and MeasureDT >='''+@last6min+''' '
		end
		else if(@datatype='hour')
		begin
		set @execTime = SUBSTRING(@execTime,0,14)+':00'
			declare @lasthour0 varchar(19)
			set @lasthour0=CONVERT(varchar(19),dateadd(HOUR,-1,@execTime), 120 )
			set @whereSql=' where MeasureDT <='''+@execTime+''' and MeasureDT >='''+@lasthour0+''' '
		end
		else if(@datatype='8hour')
		begin
			--沙口8小时班组功能  @starthour 0为0-8 1为8-16 2为16-0
			if(@starthour=0) set @whereSql=' where MeasureDT >='''+substring(@execTime,0,11)+' 00:00'' and MeasureDT <'''+substring(@execTime,0,11)+' 08:00'' '
			if(@starthour=1) set @whereSql=' where MeasureDT >='''+substring(@execTime,0,11)+' 08:00'' and MeasureDT <'''+substring(@execTime,0,11)+' 16:00'' '
			if(@starthour=2) set @whereSql=' where MeasureDT >='''+substring(@lastTimes,0,11)+' 16:00'' and MeasureDT <'''+substring(@execTime,0,11)+' 00:00'' '
			
		end
		else if(@datatype='yesterday')
		begin
			set @whereSql=' where MeasureDT between '''+SUBSTRING(@lastTimes,0,11)+' '+cast(@starthour AS varchar(5))+':00:00'' and '''+SUBSTRING(@execTime,0,11)+' '+cast(@starthour AS varchar(5))+':00:00''  '
		end
		else if(@datatype='month')
		begin
			set @whereSql=' where MeasureDT between '''+SUBSTRING(@execTime,0,11)+' '+cast(@starthour AS varchar(5))+':00:00'' and '''+SUBSTRING(@nextMonth,0,11)+' '+cast(@starthour AS varchar(5))+':00:00''  '
		end

		declare @selectSql varchar(500)--查询历史数据的select
		declare @orderSql varchar(500)--查询历史数据的order
		if(@mode='first')
		begin
			set @selectSql=' top 1 @dMpPv=ParmValue '
			set @orderSql=' order by MeasureDT desc '
		end
		else if(@mode='last')
		begin
			set @selectSql=' top 1 @dMpPv=ParmValue '
			set @orderSql=' order by MeasureDT asc '
		end
		else if(@mode='sum')
		begin
			set @selectSql=' @dMpPv=sum(ParmValue) '
			set @orderSql=''
		end
		else if(@mode='avg')
		begin
			set @selectSql=' @dMpPv=avg(ParmValue) '
			set @orderSql=''
		end


		--print @whereSql
		declare @detailMpPVSQL nvarchar(4000)
		if exists (select * from sysobjects where name='tb_mp_'+@detailMpCode+'')
		begin
		if(@mode='diff')
		begin
			if(@datatype='hour')
			begin
				declare @lasthour varchar(19)
				set @lasthour=CONVERT(varchar(19),dateadd(HOUR,-1,substring(@execTime,0,19)), 120 )

				declare @whereHourSql1 varchar(1000)
				set @whereHourSql1=' where datediff(hour,MeasureDT,'''+@execTime+''')=0 '
				declare @whereHourSql2 varchar(1000)
				set @whereHourSql2=' where datediff(hour,MeasureDT,'''+@lasthour+''')=0 '

				declare @dMpHourPv1 money--公式中包含元素的测量点
				set @dMpHourPv1=0

				declare @detailMpHourPVSQL1 nvarchar(4000)
				set @detailMpHourPVSQL1=N''
				set @detailMpHourPVSQL1=@detailMpHourPVSQL1+N'select top 1 @dMpHourPv1=ParmValue from tb_mp_'+@detailMpCode+' '+@whereHourSql1+' order by MeasureDT asc '
				--print @detailMpHourPVSQL1
				EXEC sp_executesql @detailMpHourPVSQL1,N'@dMpHourPv1 money output',@dMpHourPv1 output

				declare @dMpHourPv2 money--公式中包含元素的测量点
				set @dMpHourPv2=0
				declare @detailMpHourPVSQL2 nvarchar(4000)
				set @detailMpHourPVSQL2=N''
				set @detailMpHourPVSQL2=@detailMpHourPVSQL2+N'select top 1 @dMpHourPv2=ParmValue from tb_mp_'+@detailMpCode+' '+@whereHourSql2+' order by MeasureDT asc '
				--print @detailMpHourPVSQL2
				EXEC sp_executesql @detailMpHourPVSQL2,N'@dMpHourPv2 money output',@dMpHourPv2 output


				set @dMpPv=@dMpHourPv1-@dMpHourPv2


			end
			else if(@datatype='day')
			begin

				declare @whereDaySql1 varchar(1000)
				declare @dMpDayPv1 money--公式中包含元素的测量点
				set @dMpDayPv1=0
				declare @detailMpDayPVSQL1 nvarchar(4000)

				declare @whereDaySql2 varchar(1000)
				declare @dMpDayPv2 money--公式中包含元素的测量点
				set @dMpDayPv2=0
				declare @detailMpDayPVSQL2 nvarchar(4000)

				if(CONVERT(varchar(10),GETDATE(), 120 )>CONVERT(varchar(10),@execTime, 120 ))
				begin
					set @whereDaySql1=' where datediff(day,MeasureDT,'''+@execTime+''')=0 '
					set @whereDaySql2=' where datediff(day,MeasureDT,'''+@nextTimes+''')=0 '

					set @detailMpDayPVSQL1=N''
					set @detailMpDayPVSQL1=@detailMpDayPVSQL1+N'select top 1 @dMpDayPv1=ParmValue from tb_mp_'+@detailMpCode+' '+@whereDaySql1+' order by MeasureDT asc '
					--print @detailMpDayPVSQL1
					EXEC sp_executesql @detailMpDayPVSQL1,N'@dMpDayPv1 money output',@dMpDayPv1 output

					set @detailMpDayPVSQL2=N''
					set @detailMpDayPVSQL2=@detailMpDayPVSQL2+N'select top 1 @dMpDayPv2=ParmValue from tb_mp_'+@detailMpCode+' '+@whereDaySql2+' order by MeasureDT asc '
					--print @detailMpDayPVSQL2
					EXEC sp_executesql @detailMpDayPVSQL2,N'@dMpDayPv2 money output',@dMpDayPv2 output
				end
				else begin
					set @whereDaySql1=' where datediff(day,MeasureDT,'''+@execTime+''')=0 '
					set @whereDaySql2=' where datediff(day,MeasureDT,'''+@execTime+''')=0 '

					set @detailMpDayPVSQL1=N'' 
					set @detailMpDayPVSQL1=@detailMpDayPVSQL1+N'select top 1 @dMpDayPv1=ParmValue from tb_mp_'+@detailMpCode+' '+@whereDaySql1+' order by MeasureDT asc '
					--print @detailMpDayPVSQL1
					EXEC sp_executesql @detailMpDayPVSQL1,N'@dMpDayPv1 money output',@dMpDayPv1 output

					set @detailMpDayPVSQL2=N''
					set @detailMpDayPVSQL2=@detailMpDayPVSQL2+N'select top 1 @dMpDayPv2=ParmValue from tb_mp_'+@detailMpCode+' '+@whereDaySql2+' order by MeasureDT desc '
					--print @detailMpDayPVSQL2
					EXEC sp_executesql @detailMpDayPVSQL2,N'@dMpDayPv2 money output',@dMpDayPv2 output
				end

				set @dMpPv=@dMpDayPv2-@dMpDayPv1

			end
			else if(@datatype='month')
			begin

				declare @whereMonthSql1 varchar(1000)
				declare @dMpMonthPv1 money--公式中包含元素的测量点
				set @dMpMonthPv1=0
				declare @detailMpMonthPVSQL1 nvarchar(4000)

				declare @whereMonthSql2 varchar(1000)
				declare @dMpMonthPv2 money--公式中包含元素的测量点
				set @dMpMonthPv2=0
				declare @detailMpMonthPVSQL2 nvarchar(4000)

				if(CONVERT(varchar(10),GETDATE(), 120 )>CONVERT(varchar(10),@execTime, 120 ))
				begin
					set @whereMonthSql1=' where datediff(month,MeasureDT,'''+@execTime+''')=0 '
					set @whereMonthSql2=' where datediff(month,MeasureDT,'''+@nextMonth+''')=0 '

					set @detailMpMonthPVSQL1=N''
					set @detailMpMonthPVSQL1=@detailMpMonthPVSQL1+N'select top 1 @dMpMonthPv1=ParmValue from tb_mp_'+@detailMpCode+' '+@whereMonthSql1+' order by MeasureDT asc '
					--print @detailMpMonthPVSQL1
					EXEC sp_executesql @detailMpMonthPVSQL1,N'@dMpMonthPv1 money output',@dMpMonthPv1 output

					set @detailMpMonthPVSQL2=N''
					set @detailMpMonthPVSQL2=@detailMpMonthPVSQL2+N'select top 1 @dMpMonthPv2=ParmValue from tb_mp_'+@detailMpCode+' '+@whereMonthSql2+' order by MeasureDT asc '
					--print @detailMpMonthPVSQL2
					EXEC sp_executesql @detailMpMonthPVSQL2,N'@dMpMonthPv2 money output',@dMpMonthPv2 output
				end
				else begin
					set @whereMonthSql1=' where datediff(month,MeasureDT,'''+@execTime+''')=0 '
					set @whereMonthSql2=' where datediff(month,MeasureDT,'''+@execTime+''')=0 '

					set @detailMpMonthPVSQL1=N'' 
					set @detailMpMonthPVSQL1=@detailMpMonthPVSQL1+N'select top 1 @dMpMonthPv1=ParmValue from tb_mp_'+@detailMpCode+' '+@whereMonthSql1+' order by MeasureDT asc '
					--print @detailMpMonthPVSQL1
					EXEC sp_executesql @detailMpMonthPVSQL1,N'@dMpMonthPv1 money output',@dMpMonthPv1 output

					set @detailMpMonthPVSQL2=N''
					set @detailMpMonthPVSQL2=@detailMpMonthPVSQL2+N'select top 1 @dMpMonthPv2=ParmValue from tb_mp_'+@detailMpCode+' '+@whereMonthSql2+' order by MeasureDT desc '
					--print @detailMpMonthPVSQL2
					EXEC sp_executesql @detailMpMonthPVSQL2,N'@dMpMonthPv2 money output',@dMpMonthPv2 output
				end

				set @dMpPv=@dMpMonthPv2-@dMpMonthPv1

			end
			else if(@datatype='8hour')
			begin
			
				declare @where8HourSql1 varchar(1000)
				declare @where8HourSql2 varchar(1000)

				--沙口8小时班组功能  @starthour 0为0-8 1为8-16 2为16-0
				if(@starthour=0) begin
				set @where8HourSql1=' where MeasureDT >='''+substring(@execTime,0,11)+' 00:00'' and MeasureDT <'''+substring(@execTime,0,11)+' 01:00'' order by MeasureDT asc'
				set @where8HourSql2=' where MeasureDT >='''+substring(@execTime,0,11)+' 08:00'' and MeasureDT <'''+substring(@execTime,0,11)+' 09:00'' order by MeasureDT asc'
				end
				else if(@starthour=1) begin
				set @where8HourSql1=' where MeasureDT >='''+substring(@execTime,0,11)+' 08:00'' and MeasureDT <'''+substring(@execTime,0,11)+' 09:00'' order by MeasureDT asc'
				set @where8HourSql2=' where MeasureDT >='''+substring(@execTime,0,11)+' 16:00'' and MeasureDT <'''+substring(@execTime,0,11)+' 17:00'' order by MeasureDT asc'
				end
				else if(@starthour=2) begin
				set @where8HourSql1=' where MeasureDT >='''+substring(@lastTimes,0,11)+' 16:00'' and MeasureDT <'''+substring(@lastTimes,0,11)+' 17:00'' order by MeasureDT asc'
				set @where8HourSql2=' where MeasureDT >='''+substring(@execTime,0,11)+' 00:00'' and MeasureDT <'''+substring(@execTime,0,11)+' 01:00'' order by MeasureDT asc'
				end


				declare @dMp8HourPv1 money--公式中包含元素的测量点
				set @dMp8HourPv1=0

				declare @detailMp8HourPVSQL1 nvarchar(4000)
				set @detailMp8HourPVSQL1=N''
				set @detailMp8HourPVSQL1=@detailMp8HourPVSQL1+N'select top 1 @dMp8HourPv1=ParmValue from tb_mp_'+@detailMpCode+' '+@where8HourSql2+' '
				--print @detailMp8HourPVSQL1
				EXEC sp_executesql @detailMp8HourPVSQL1,N'@dMp8HourPv1 money output',@dMp8HourPv1 output

				declare @dMp8HourPv2 money--公式中包含元素的测量点
				set @dMp8HourPv2=0
				declare @detailMp8HourPVSQL2 nvarchar(4000)
				set @detailMp8HourPVSQL2=N''
				set @detailMp8HourPVSQL2=@detailMp8HourPVSQL2+N'select top 1 @dMp8HourPv2=ParmValue from tb_mp_'+@detailMpCode+' '+@where8HourSql1+'  '
				--print @detailMp8HourPVSQL2
				EXEC sp_executesql @detailMp8HourPVSQL2,N'@dMp8HourPv2 money output',@dMp8HourPv2 output

				
				set @dMpPv=@dMp8HourPv1-@dMp8HourPv2
				if(@dMpPv<0)begin set @dMpPv=0 end

			end
			else begin
				declare @dMpPv1 money--公式中包含元素的测量点
				set @dMpPv1=0

				declare @detailMpPVSQL1 nvarchar(4000)
				set @detailMpPVSQL1=N''
				set @detailMpPVSQL1=@detailMpPVSQL1+N'select top 1 @dMpPv1=ParmValue from tb_mp_'+@detailMpCode+' '+@whereSql+' order by MeasureDT desc '
				--print @detailMpPVSQL1
				EXEC sp_executesql @detailMpPVSQL1,N'@dMpPv1 money output',@dMpPv1 output

				declare @dMpPv2 money--公式中包含元素的测量点
				set @dMpPv2=0
				declare @detailMpPVSQL2 nvarchar(4000)
				set @detailMpPVSQL2=N''
				set @detailMpPVSQL2=@detailMpPVSQL2+N'select top 1 @dMpPv2=ParmValue from tb_mp_'+@detailMpCode+' '+@whereSql+' order by MeasureDT asc '
				--print @detailMpPVSQL2
				EXEC sp_executesql @detailMpPVSQL2,N'@dMpPv2 money output',@dMpPv2 output


				set @dMpPv=@dMpPv1-@dMpPv2
			end
		end
		else begin
			set @detailMpPVSQL=N''
			set @detailMpPVSQL=@detailMpPVSQL+N'select '+@selectSql+' from tb_mp_'+@detailMpCode+' '+@whereSql+' '+@orderSql+' '
			print @detailMpPVSQL
			EXEC sp_executesql @detailMpPVSQL,N'@dMpPv money output',@dMpPv output
		end

			if(@dMpPv is null)begin
			 set @dMpPv=0
			end

			declare @outValueChart varchar(50)

			if(@dMpPv<0) begin 
				set @outValueChart='('+convert(varchar,sum(@dMpPv))+')'
			end
			else begin
				set @outValueChart=@dMpPv
			end

			--如果包含动态分母临时表，则判定是否当前测量点是否运行
			if(@denominatorCountNum>0)begin
				if(@dMpPv>0)begin --大于0则表示在运行
					declare @lsdenominatorname varchar(50)
					declare @lsmoleculename varchar(50)
					DECLARE DenominatorLSList CURSOR FOR SELECT denominatorname,moleculename FROM #Tb_Denominator
					open DenominatorLSList
					FETCH next FROM DenominatorLSList INTO @lsdenominatorname,@lsmoleculename
					WHILE (@@FETCH_STATUS=0)
					BEGIN
						if (charindex(@cc,@lsmoleculename)>0)--是否在该组分子中
						begin
							update #Tb_Denominator set num=num+1 where denominatorname=@lsdenominatorname
						end
					FETCH next FROM DenominatorLSList INTO @lsdenominatorname,@lsmoleculename
					end
					CLOSE DenominatorLSList
					DEALLOCATE DenominatorLSList
				end
			end

			set @sqlstr=@sqlstr+@outValueChart
		end
		else begin
			set @sqlstr=@sqlstr+@cc--目前主要为了插入动态分母的识别元素，后面需要处理
		end


	 end

	fetch next from c1 into @cc
    end
    close c1
    deallocate c1

	--print @sqlstr
	declare @endsqlstr varchar(1000)
	set @endsqlstr=''

	--select * from #Tb_Denominator
	--把动态分母替换成数据
	if(@denominatorCountNum>0)begin
		declare @cc2 varchar(50)--拆分后每行的数据

		declare c2 cursor for
		select a from dbo.f_get_exp_split(@sqlstr,@tmpstr)
		open c2
		fetch next from c2 into @cc2
		while @@fetch_status=0
		begin
		--print @cc2
		 if (charindex(@cc2,@tmpstr)>0)--是否为四则运算符
		 begin
		  set @endsqlstr=@endsqlstr+cast(@cc2 as varchar(50))
		 end
		 else if (ISNUMERIC(@cc2)=1)--是否为数字
		 begin
		  set @endsqlstr=@endsqlstr+cast(@cc2 as varchar(50))
		 end
		 else
		 begin
			
			declare @lsdenominatorNum varchar(50)
			DECLARE DenominatorLSList2 CURSOR FOR SELECT num FROM #Tb_Denominator where denominatorname=@cc2
			open DenominatorLSList2
			FETCH next FROM DenominatorLSList2 INTO @lsdenominatorNum
			WHILE (@@FETCH_STATUS=0)
			BEGIN
				set @endsqlstr=@endsqlstr+cast(@lsdenominatorNum AS varchar(10))
			FETCH next FROM DenominatorLSList2 INTO @lsdenominatorNum
			end
			CLOSE DenominatorLSList2
			DEALLOCATE DenominatorLSList2

		 end

		fetch next from c2 into @cc2
		end
		close c2
		deallocate c2

	end
	else begin
		set @endsqlstr=@sqlstr
	end

	--print @execSt
	if (@execSt=0) begin
	if(@datatype=@execType)begin print @endsqlstr end

	declare @execsql nvarchar(4000)

	begin try
		set @execsql=N''
		set @execsql=@execsql+N'update TB_MeasurePoint set ParmValue='+@endsqlstr+',MeasureDT='''+@execTime+''' where MPointCode='''+@mainMpCode+''' '
		--print @execsql
		EXEC sp_executesql @execsql
	end try
	begin catch
		set @execsql=N''
		set @execsql=@execsql+N'update TB_MeasurePoint set ParmValue=0,MeasureDT='''+@execTime+''' where MPointCode='''+@mainMpCode+''' '
		--print @execsql+' --false'
		EXEC sp_executesql @execsql
	end catch
	

	if(@execType='month' or @execType='day')begin

		declare @instbsqlnum int

		declare @instbsqlnumSQL nvarchar(4000)
		set @instbsqlnumSQL=N''
		set @instbsqlnumSQL=@instbsqlnumSQL+N'select @instbsqlnum=count(*) from tb_mp_'+@mainMpCode+' where datediff('+@execType+',MeasureDT,'''+@execTime+''')=0 '
		EXEC sp_executesql @instbsqlnumSQL,N'@instbsqlnum int output',@instbsqlnum output
		--print @instbsqlnum
		if(@instbsqlnum=0)begin
			begin try
				set @execsql=N''
				set @execsql=@execsql+N'insert into tb_mp_'+@mainMpCode+'(ParmValue,MeasureDT) values('+@endsqlstr+','''+@execTime+''') '
				EXEC sp_executesql @execsql
			end try
			begin catch
				set @execsql=N''
				set @execsql=@execsql+N'insert into tb_mp_'+@mainMpCode+'(ParmValue,MeasureDT) values(0,'''+@execTime+''') '
				EXEC sp_executesql @execsql
			end catch
		end
		else begin
			begin try
				set @execsql=N''
				set @execsql=@execsql+N'update tb_mp_'+@mainMpCode+' set ParmValue='+@sqlstr+',insdt='''+CONVERT(varchar(19),GETDATE(), 120 )+'''  where datediff('+@execType+',MeasureDT,'''+@execTime+''')=0   '
				EXEC sp_executesql @execsql
			end try
			begin catch
				set @execsql=N''
				set @execsql=@execsql+N'update tb_mp_'+@mainMpCode+' set ParmValue=0,insdt='''+CONVERT(varchar(19),GETDATE(), 120 )+'''  where datediff('+@execType+',MeasureDT,'''+@execTime+''')=0   '
				EXEC sp_executesql @execsql
			end catch
		end
	end
	else if(@execType='yesterday')begin

		declare @instbsqlnumYDay int

		declare @instbsqlnumYDaySQL nvarchar(4000)
		set @instbsqlnumYDaySQL=N''
		set @instbsqlnumYDaySQL=@instbsqlnumYDaySQL+N'select @instbsqlnumYDay=count(*) from tb_mp_'+@mainMpCode+' where datediff(day,MeasureDT,'''+@execTime+''')=0 '
		EXEC sp_executesql @instbsqlnumYDaySQL,N'@instbsqlnumYDay int output',@instbsqlnumYDay output
		--print @instbsqlnumYDay
		if(@instbsqlnumYDay=0)begin
			begin try
				set @execsql=N''
				set @execsql=@execsql+N'insert into tb_mp_'+@mainMpCode+'(ParmValue,MeasureDT) values('+@endsqlstr+','''+@execTime+''') '
				EXEC sp_executesql @execsql
			end try
			begin catch
				set @execsql=N''
				set @execsql=@execsql+N'insert into tb_mp_'+@mainMpCode+'(ParmValue,MeasureDT) values(0,'''+@execTime+''') '
				EXEC sp_executesql @execsql
			end catch
		end
		else begin
			begin try
				set @execsql=N''
				set @execsql=@execsql+N'update tb_mp_'+@mainMpCode+' set ParmValue='+@sqlstr+',insdt='''+CONVERT(varchar(19),GETDATE(), 120 )+'''  where datediff(day,MeasureDT,'''+@execTime+''')=0   '
				EXEC sp_executesql @execsql
			end try
			begin catch
				set @execsql=N''
				set @execsql=@execsql+N'update tb_mp_'+@mainMpCode+' set ParmValue=0,insdt='''+CONVERT(varchar(19),GETDATE(), 120 )+'''  where datediff(day,MeasureDT,'''+@execTime+''')=0   '
				EXEC sp_executesql @execsql
			end catch
		end
	end
	else if(@execType='8hour')begin
		declare @8hourTime varchar(50)
		if(@starthour=2) begin
			set @8hourTime=CONVERT(varchar(10),dateadd(day,-1,@execTime), 120 )
		end
		else begin
			set @8hourTime=@execTime
		end

		declare @instbsqlnum8hour int

		declare @instbsqlnum8hourSQL nvarchar(4000)
		set @instbsqlnum8hourSQL=N''
		set @instbsqlnum8hourSQL=@instbsqlnum8hourSQL+N'select @instbsqlnum8hour=count(*) from tb_mp_'+@mainMpCode+' where datediff(day,MeasureDT,'''+@8hourTime+''')=0 '
		EXEC sp_executesql @instbsqlnum8hourSQL,N'@instbsqlnum8hour int output',@instbsqlnum8hour output
		--print @instbsqlnum8hour
		if(@instbsqlnum8hour=0)begin
			begin try
				set @execsql=N''
				set @execsql=@execsql+N'insert into tb_mp_'+@mainMpCode+'(ParmValue,MeasureDT) values('+@endsqlstr+','''+@8hourTime+''') '
				EXEC sp_executesql @execsql
			end try
			begin catch
				set @execsql=N''
				set @execsql=@execsql+N'insert into tb_mp_'+@mainMpCode+'(ParmValue,MeasureDT) values(0,'''+@8hourTime+''') '
				EXEC sp_executesql @execsql
			end catch
		end
		else begin
			begin try
				set @execsql=N''
				set @execsql=@execsql+N'update tb_mp_'+@mainMpCode+' set ParmValue='+@sqlstr+',insdt='''+CONVERT(varchar(19),GETDATE(), 120 )+'''  where datediff(day,MeasureDT,'''+@8hourTime+''')=0   '
				EXEC sp_executesql @execsql
			end try
			begin catch
				set @execsql=N''
				set @execsql=@execsql+N'update tb_mp_'+@mainMpCode+' set ParmValue=0,insdt='''+CONVERT(varchar(19),GETDATE(), 120 )+'''  where datediff(day,MeasureDT,'''+@8hourTime+''')=0   '
				EXEC sp_executesql @execsql
			end catch
		end
	end
	else begin
		begin try
			set @execsql=N''
			set @execsql=@execsql+N'insert into tb_mp_'+@mainMpCode+'(ParmValue,MeasureDT) values('+@endsqlstr+','''+@execTime+''') '
			EXEC sp_executesql @execsql
		end try
		begin catch
			set @execsql=N''
			set @execsql=@execsql+N'insert into tb_mp_'+@mainMpCode+'(ParmValue,MeasureDT) values(0,'''+@execTime+''') '
			EXEC sp_executesql @execsql
		end catch
	end


	end


	drop table #Tb_Denominator--删除临时表

	FETCH next FROM formulaList INTO @mainMpCode,@exp
	end
	CLOSE formulaList
	DEALLOCATE formulaList


GO

-- ----------------------------
-- Procedure structure for sp_report_create_mphistory_table
-- ----------------------------
DROP PROCEDURE [dbo].[sp_report_create_mphistory_table]
GO



CREATE PROCEDURE [dbo].[sp_report_create_mphistory_table]
@strMPointCode varchar(200)
 AS

declare @createSQL nvarchar(max),
@strSQL varchar(8000),
@msg  varchar(255)


IF (@strMPointCode <> '')
	BEGIN
		SET @strSQL = ''
		SET @strSQL = @strSQL + 'if exists (select 1 ' + char(10)
		SET @strSQL = @strSQL + '            from  sysobjects ' + char(10)
		SET @strSQL = @strSQL + '           where  id = object_id(''dbo.TB_MP_' + @strMPointCode + ''') ' + char(10)
		SET @strSQL = @strSQL + '            and   type = ''U'') ' + char(10)
		SET @strSQL = @strSQL + '   drop table dbo.[TB_MP_' + @strMPointCode + ']; ' + char(10) + char(10)
		
		SET @strSQL = @strSQL + ' create table dbo.[TB_MP_' + @strMPointCode + '] ( ' + char(10)
		SET @strSQL = @strSQL + '   ItemID               bigint               identity, ' + char(10)
		SET @strSQL = @strSQL + '   ParmValue            money                not null default 0, ' + char(10)
		SET @strSQL = @strSQL + '   MeasureDT            datetime             not null default getdate(), ' + char(10)
		SET @strSQL = @strSQL + '   memotype            varchar(100)            , ' + char(10)
		SET @strSQL = @strSQL + '   memo            varchar(500)            , ' + char(10)
		SET @strSQL = @strSQL + '   userid            varchar(50)            , ' + char(10)
		SET @strSQL = @strSQL + '   insdt            varchar(50) 	default getdate() ,          ' + char(10)
		SET @strSQL = @strSQL + '   constraint [PK_TB_MP_' + @strMPointCode + '] primary key  (ItemID) ' + char(10)
		SET @strSQL = @strSQL + ') ' + char(10) + char(10)

		EXECUTE(@strSQL)
		Print @strSQL
		IF @@ERROR <> 0
		BEGIN
			SET @msg =  '创建TB_MP_' + @strMPointCode + '表时出现错误'
			Print @msg
		END

	END


GO

-- ----------------------------
-- Procedure structure for sp_report_day_01
-- ----------------------------
DROP PROCEDURE [dbo].[sp_report_day_01]
GO



CREATE PROCEDURE [dbo].[sp_report_day_01]
@stime varchar(10), @etime varchar(10), @starthour int, @endhour int, @intval int, @mpstr varchar(5000),@rownum int,@userid varchar(50)
 AS
declare @starttime datetime
declare @endtime datetime
declare @tempdate datetime
declare @mptemp varchar(5000)
declare @collectmode varchar(50)
declare @mp varchar(5000)

-- declare @userid varchar(50)
-- set @userid=newid()

Select @starttime=cast(@stime as datetime)
Select @starttime=DateAdd(hour,@starthour,@starttime)
Select @endtime=cast(@etime as datetime)
Select @endtime=DateAdd(hour,@endhour,@endtime)

/******
if(@endtime>getdate())
begin
set @endtime=getdate()
set @endtime = dateadd(hh,-datepart(hh,@endtime),@endtime)
set @endtime = dateadd(ss,-datepart(ss,@endtime),@endtime)
set @endtime = dateadd(mi,-datepart(mi,@endtime),@endtime)
set @endtime = dateadd(ms,-datepart(ms,@endtime),@endtime)
set @endtime = dateadd(hh,@starthour,@endtime)
end
******/

if(@endtime>getdate())
begin
	set @endtime = getdate()
end

-- DROP TABLE TB_MP_TEMP_REPORT

-- CREATE TABLE [dbo].[TB_MP_TEMP_REPORT] (
-- 	[ItemID] [bigint] IDENTITY (1, 1) NOT NULL ,
-- 	[ParmValue] [varchar] (2000) COLLATE Chinese_PRC_CI_AS NOT NULL ,
-- 	[MeasureDT] [datetime] NOT NULL ,
-- 	[FormatType] [varchar] (50) COLLATE Chinese_PRC_CI_AS NOT NULL, 
-- 	[decimal] [int] NOT NULL
-- ) ON [PRIMARY]

-- delete from tb_mp_temp_rpt  where  insdt <= DateAdd(day,-1,insdt)

while(@starttime<=@endtime)
begin
	set @tempdate = dateadd(hour,@intval,@starttime)
	--print '11111'
	--insert into TB_MP_TEMP_REPORT(ParmValue,MeasureDT,FormatType) values(cast(DATEPART(HH,@starttime)  as varchar)+':'+cast(DATEPART(mm,@starttime)  as varchar),@starttime,'label')

	DECLARE CRStatus cursor for 
	select * from dbo.FUN_split(@mpstr,'|')
	open CRStatus

	FETCH  CRStatus into @mptemp

	set @mp = SUBSTRING ( @mptemp , 0 , CHARINDEX(';',@mptemp) )
	set @collectmode = SUBSTRING ( @mptemp , CHARINDEX(';',@mptemp)+1, LEN ( @mptemp )-CHARINDEX(';',@mptemp) )

	while (@@fetch_status=0)
	begin
		exec sp_report_getdata @starttime,@tempdate,@collectmode,@mp,@userid

	FETCH  CRStatus into @mptemp

	set @mp = SUBSTRING ( @mptemp , 0 , CHARINDEX(';',@mptemp) )
	set @collectmode = SUBSTRING ( @mptemp , CHARINDEX(';',@mptemp)+1, LEN ( @mptemp )-CHARINDEX(';',@mptemp) )

	end

	close CRStatus
	DEALLOCATE CRStatus
	
	set @starttime = @tempdate
end

Select * from TB_MP_TEMP_REPORT where userid=@userid order by itemid


GO

-- ----------------------------
-- Procedure structure for sp_report_day_01_test
-- ----------------------------
DROP PROCEDURE [dbo].[sp_report_day_01_test]
GO



CREATE PROCEDURE [dbo].[sp_report_day_01_test]
@tbname varchar(200), @stime varchar(10), @etime varchar(10), @starthour int,  @intval int, @mpstr varchar(800),@rownum int
 AS
declare @starttime datetime
declare @endtime datetime
declare @tempdate datetime
declare @mptemp varchar(500)
declare @collectmode varchar(50)
declare @mp varchar(500)
declare @dropSQL nvarchar(4000)
declare @createSQL nvarchar(4000)
declare @insertSQL nvarchar(4000)
declare @selectSQL nvarchar(4000)
declare @tablebname varchar(500)

Select @tablebname=@tbname
Select @starttime=cast(@stime as datetime)
Select @starttime=DateAdd(hour,@starthour,@starttime)
Select @endtime=cast(@etime as datetime)
Select @endtime=DateAdd(hour,@starthour,@endtime)
if(@endtime>getdate())
begin
--之前的sp把当前时间设置为结束时间
--set @endtime=getdate()
set @endtime = dateadd(hh,-datepart(hh,@endtime),@endtime)
set @endtime = dateadd(ss,-datepart(ss,@endtime),@endtime)
set @endtime = dateadd(mi,-datepart(mi,@endtime),@endtime)
set @endtime = dateadd(ms,-datepart(ms,@endtime),@endtime)
set @endtime = dateadd(hh,@starthour,@endtime)
end

begin
set @dropSQL = 'truncate TABLE '+@tablebname 
set @createSQL=' CREATE TABLE  [dbo].['+@tablebname+'] (
	[ItemID] [bigint] IDENTITY (1, 1) NOT NULL ,
	[ParmValue] [varchar] (2000) COLLATE Chinese_PRC_CI_AS NOT NULL ,
	[MeasureDT] [datetime] NOT NULL ,
	[mpid] [varchar] (50) COLLATE Chinese_PRC_CI_AS ,
	[ValueMeaning] [varchar] (50) COLLATE Chinese_PRC_CI_AS ,
	[alarmmax] money ,
	[alarmmin] money ,
	[numtail] [varchar] (50) COLLATE Chinese_PRC_CI_AS,
	[memotype] [varchar]  (500) COLLATE Chinese_PRC_CI_AS,
	[UUID] [bigint],
	[FormatType] [varchar] (50) COLLATE Chinese_PRC_CI_AS NOT NULL 
) ON [PRIMARY] '
end

begin
	if Exists(select top 1 * from sysObjects where Id=OBJECT_ID(@tablebname))
	begin
		EXEC sp_executesql @dropSQL
		print N'@dropSQL: '+@dropSQL
	end
	else
	begin
		EXEC sp_executesql @createSQL
	end
end

while(@starttime<@endtime)
begin
	set @tempdate = dateadd(hour,@intval,@starttime)

	--insert into TB_MP_TEMP_REPORT(ParmValue,MeasureDT,FormatType) values(cast(DATEPART(HH,@starttime)  as varchar)+':'+cast(DATEPART(mi,@starttime)  as varchar),@starttime,'label')
	set @insertSQL = N' insert into '+@tablebname+'(ParmValue,MeasureDT,FormatType) values('''+substring(cast(@starttime as varchar),11,6)+''', '''+CONVERT(varchar(16),@startTime, 120 )+''',''label'')'
	--insert into TB_MP_TEMP_REPORT(ParmValue,MeasureDT,FormatType) values(substring(cast(@starttime as varchar),11,6),@starttime,'label')
	print N'@insertSQL: '+@insertSQL
	EXEC sp_executesql @insertSQL

	DECLARE CRStatus cursor for 
	select * from dbo.FUN_split(@mpstr,'|')
	open CRStatus

	FETCH  CRStatus into @mptemp

	set @mp = SUBSTRING ( @mptemp , 0 , CHARINDEX(';',@mptemp) )
	set @collectmode = SUBSTRING ( @mptemp , CHARINDEX(';',@mptemp)+1, LEN ( @mptemp )-CHARINDEX(';',@mptemp) )

	while (@@fetch_status=0)
	begin

        exec sp_report_getdata_test @tbname,@starttime,@tempdate,@collectmode,@mp

	FETCH  CRStatus into @mptemp

	set @mp = SUBSTRING ( @mptemp , 0 , CHARINDEX(';',@mptemp) )
	set @collectmode = SUBSTRING ( @mptemp , CHARINDEX(';',@mptemp)+1, LEN ( @mptemp )-CHARINDEX(';',@mptemp) )

	end

	close CRStatus
	DEALLOCATE CRStatus
	
	set @starttime = @tempdate
end

begin
 set @selectSQL = N' Select * from '+@tablebname+' order by itemid'
 print N'@selectSQL: '+@selectSQL
 EXEC sp_executesql @selectSQL
 end
--Select * from TB_MP_TEMP_REPORT order by itemid
begin
set @dropSQL=N' DROP TABLE if exists  '+@tablebname+' '
print N'@dropSQL: '+@dropSQL
EXEC sp_executesql @dropSQL
end


GO

-- ----------------------------
-- Procedure structure for sp_report_day_02
-- ----------------------------
DROP PROCEDURE [dbo].[sp_report_day_02]
GO



CREATE PROCEDURE [dbo].[sp_report_day_02]
@stime varchar(10), @etime varchar(10), @starthour int,  @intval int, @mpstr varchar(800),@rownum int
 AS
declare @starttime datetime
declare @endtime datetime
declare @tempdate datetime
declare @mptemp varchar(500)
declare @collectmode varchar(50)
declare @mp varchar(500)


Select @starttime=cast(@stime as datetime)
Select @starttime=DateAdd(hour,@starthour,@starttime)
Select @endtime=cast(@etime as datetime)
Select @endtime=DateAdd(hour,@starthour,@endtime)
if(@endtime>getdate())
begin
set @endtime=getdate()
set @endtime = dateadd(hh,-datepart(hh,@endtime),@endtime)
set @endtime = dateadd(ss,-datepart(ss,@endtime),@endtime)
set @endtime = dateadd(mi,-datepart(mi,@endtime),@endtime)
set @endtime = dateadd(ms,-datepart(ms,@endtime),@endtime)
set @endtime = dateadd(hh,@starthour,@endtime)
end

--print @starttime
--print @endtime

DROP TABLE TB_MP_TEMP_REPORT

CREATE TABLE [dbo].[TB_MP_TEMP_REPORT] (
	[ItemID] [bigint] IDENTITY (1, 1) NOT NULL ,
	[ParmValue] [varchar] (2000) COLLATE Chinese_PRC_CI_AS NOT NULL ,
	[MeasureDT] [datetime] NOT NULL ,
	[FormatType] [varchar] (50) COLLATE Chinese_PRC_CI_AS NOT NULL,
	[Decimal] [int] NOT NULL 
) ON [PRIMARY]


	DECLARE CRStatus cursor for 
	select * from dbo.FUN_split(@mpstr,'|')
	open CRStatus

	FETCH  CRStatus into @mptemp

	set @mp = SUBSTRING ( @mptemp , 0 , CHARINDEX(';',@mptemp) )
	set @collectmode = SUBSTRING ( @mptemp , CHARINDEX(';',@mptemp)+1, LEN ( @mptemp )-CHARINDEX(';',@mptemp) )

	while (@@fetch_status=0)
	begin

        exec sp_report_runrecord @starttime,@endtime,@collectmode,@mp,@rownum

	FETCH  CRStatus into @mptemp

	set @mp = SUBSTRING ( @mptemp , 0 , CHARINDEX(';',@mptemp) )
	set @collectmode = SUBSTRING ( @mptemp , CHARINDEX(';',@mptemp)+1, LEN ( @mptemp )-CHARINDEX(';',@mptemp) )

	end

	close CRStatus
	DEALLOCATE CRStatus
	


Select * from TB_MP_TEMP_REPORT order by itemid


GO

-- ----------------------------
-- Procedure structure for sp_report_day_03
-- ----------------------------
DROP PROCEDURE [dbo].[sp_report_day_03]
GO



CREATE PROCEDURE [dbo].[sp_report_day_03]
@stime varchar(10), @etime varchar(10), @starthour int,  @intval int, @mpstr varchar(800),@rownum int
 AS
declare @starttime datetime
declare @endtime datetime
declare @tempdate datetime
declare @mptemp varchar(500)
declare @collectmode varchar(50)
declare @mp varchar(500)


Select @starttime=cast(@stime as datetime)
Select @starttime=DateAdd(hour,@starthour,@starttime)
Select @endtime=cast(@etime as datetime)
Select @endtime=DateAdd(hour,@starthour,@endtime)
if(@endtime>getdate())
begin
set @endtime=getdate()
set @endtime = dateadd(hh,-datepart(hh,@endtime),@endtime)
set @endtime = dateadd(ss,-datepart(ss,@endtime),@endtime)
set @endtime = dateadd(mi,-datepart(mi,@endtime),@endtime)
set @endtime = dateadd(ms,-datepart(ms,@endtime),@endtime)
set @endtime = dateadd(hh,@starthour,@endtime)
end

--print @starttime
--print @endtime

DROP TABLE TB_MP_TEMP_REPORT

CREATE TABLE [dbo].[TB_MP_TEMP_REPORT] (
	[ItemID] [bigint] IDENTITY (1, 1) NOT NULL ,
	[ParmValue] [varchar] (2000) COLLATE Chinese_PRC_CI_AS NOT NULL ,
	[MeasureDT] [datetime] NOT NULL ,
	[FormatType] [varchar] (50) COLLATE Chinese_PRC_CI_AS NOT NULL,
	[Decimal] [int] NOT NULL 
) ON [PRIMARY]


	set @tempdate = dateadd(hour,@intval,@starttime)

	DECLARE CRStatus cursor for 
	select * from dbo.FUN_split(@mpstr,'|')
	open CRStatus

	FETCH  CRStatus into @mptemp

	set @mp = SUBSTRING ( @mptemp , 0 , CHARINDEX(';',@mptemp) )
	set @collectmode = SUBSTRING ( @mptemp , CHARINDEX(';',@mptemp)+1, LEN ( @mptemp )-CHARINDEX(';',@mptemp) )

	while (@@fetch_status=0)
	begin

        exec sp_report_getdata @starttime,@tempdate,@collectmode,@mp

	FETCH  CRStatus into @mptemp

	set @mp = SUBSTRING ( @mptemp , 0 , CHARINDEX(';',@mptemp) )
	set @collectmode = SUBSTRING ( @mptemp , CHARINDEX(';',@mptemp)+1, LEN ( @mptemp )-CHARINDEX(';',@mptemp) )

	end

	close CRStatus
	DEALLOCATE CRStatus
	
	


Select * from TB_MP_TEMP_REPORT order by itemid


GO

-- ----------------------------
-- Procedure structure for sp_report_day_03_test
-- ----------------------------
DROP PROCEDURE [dbo].[sp_report_day_03_test]
GO



CREATE PROCEDURE [dbo].[sp_report_day_03_test]
@tbname varchar(200), @stime varchar(10), @etime varchar(10), @starthour int,  @intval int, @mpstr varchar(800),@rownum int
 AS
declare @starttime datetime
declare @endtime datetime
declare @tempdate datetime
declare @mptemp varchar(500)
declare @collectmode varchar(50)
declare @mp varchar(500)
declare @tablebname varchar(500)
declare @dropSQL nvarchar(max)
declare @createSQL nvarchar(max)
declare @insertSQL nvarchar(max)
declare @selectSQL nvarchar(max)

Select @tablebname=@tbname
Select @starttime=cast(@stime as datetime)
Select @starttime=DateAdd(hour,@starthour,@starttime)
Select @endtime=cast(@etime as datetime)
Select @endtime=DateAdd(hour,@starthour,@endtime)
if(@endtime>getdate())
begin
--之前的sp把当前时间设置为结束时间
--set @endtime=getdate()
set @endtime = dateadd(hh,-datepart(hh,@endtime),@endtime)
set @endtime = dateadd(ss,-datepart(ss,@endtime),@endtime)
set @endtime = dateadd(mi,-datepart(mi,@endtime),@endtime)
set @endtime = dateadd(ms,-datepart(ms,@endtime),@endtime)
set @endtime = dateadd(hh,@starthour,@endtime)
end

begin
set @dropSQL = 'truncate TABLE '+@tablebname 
set @createSQL=' CREATE TABLE  [dbo].['+@tablebname+'] (
	[ItemID] [bigint] IDENTITY (1, 1) NOT NULL ,
	[ParmValue] [varchar] (2000) COLLATE Chinese_PRC_CI_AS NOT NULL ,
	[MeasureDT] [datetime] NOT NULL ,
	[mpid] [varchar] (500) ,
	[ValueMeaning] [varchar] (50) COLLATE Chinese_PRC_CI_AS ,
	[alarmmax] money ,
	[alarmmin] money ,
	[numtail] [varchar] (50) COLLATE Chinese_PRC_CI_AS,
	[memotype] [varchar]  (500) COLLATE Chinese_PRC_CI_AS,
	[UUID] [bigint] ,
	[FormatType] [varchar] (50) COLLATE Chinese_PRC_CI_AS NOT NULL 
) ON [PRIMARY] '
end

begin
	if Exists(select top 1 * from sysObjects where Id=OBJECT_ID(@tablebname))
	begin
		EXEC sp_executesql @dropSQL
		print N'@dropSQL: '+@dropSQL
	end
	else
	begin
		EXEC sp_executesql @createSQL
		--print N'@@createSQL: '+@createSQL
	end
end


while(@starttime<@endtime)

begin
	set @tempdate = dateadd(hour,@intval,@starttime)
	
	DECLARE CRStatus cursor for 
	select * from dbo.FUN_split(@mpstr,'|')
	open CRStatus
	
	FETCH  CRStatus into @mptemp
	print @mptemp
	set @mp = SUBSTRING ( @mptemp , 0 , CHARINDEX(';',@mptemp) )
	print @mp
	set @collectmode = SUBSTRING ( @mptemp , CHARINDEX(';',@mptemp)+1, LEN ( @mptemp )-CHARINDEX(';',@mptemp) )
	print N'4444: '
	print @collectmode
	while (@@fetch_status=0)
	begin
	print N'5555: '
        exec sp_report_getdata_test @tbname,@starttime,@tempdate,@collectmode,@mp

	FETCH  CRStatus into @mptemp

	set @mp = SUBSTRING ( @mptemp , 0 , CHARINDEX(';',@mptemp) )
	set @collectmode = SUBSTRING ( @mptemp , CHARINDEX(';',@mptemp)+1, LEN ( @mptemp )-CHARINDEX(';',@mptemp) )

	end

	close CRStatus
	DEALLOCATE CRStatus
	print N'6666: '
	print @tempdate
		set @starttime = @tempdate
		print @starttime
end

begin
 set @selectSQL = N' Select * from '+@tablebname+' order by itemid'
 print N'@selectSQL: '+@selectSQL
 EXEC sp_executesql @selectSQL
 end
--Select * from TB_MP_TEMP_REPORT order by itemid
begin
set @dropSQL=N' DROP TABLE if exists  '+@tablebname+' '
print N'@dropSQL: '+@dropSQL
EXEC sp_executesql @dropSQL
end


GO

-- ----------------------------
-- Procedure structure for sp_report_getdata
-- ----------------------------
DROP PROCEDURE [dbo].[sp_report_getdata]
GO



CREATE PROCEDURE [dbo].[sp_report_getdata]
@starttime varchar(20),@endtime varchar(20),@type varchar(80),@mp varchar(5000),@userid varchar(100)
as
Declare @strSQL nvarchar(4000)
declare @v Numeric(20,4)
declare @s Numeric(20,4)
declare @vtemp Numeric(20,4)
declare @vtemp2 Numeric(20,4)
declare @mptemp varchar(80)
declare @count int
declare @i int
declare @v1 Numeric(20,4)
declare @v2 Numeric(20,4)
declare @tempdate datetime
declare @sdate datetime
declare @edate datetime
declare @starttime1 datetime
declare @endtime1 datetime
Declare @mainSQL nvarchar(4000)
Declare @tempSQL nvarchar(4000)
Declare @tempVAL Numeric(20,4)
Declare @tempVAL2 Numeric(20,4)
Declare @tempVAL3 Numeric(20,4)
declare @numTailSQL nvarchar(500)
Declare @numTail varchar(10)
Declare @numTailS int
declare @v3 datetime

declare @vmstr varchar(200)
declare @chararray  varchar(200)
declare @floatarray  varchar(200)
declare @charmp  varchar(50)
declare @floatmp  varchar(50)
declare @x int
declare @y int

Select @sdate=cast(@starttime as datetime)
Select @edate=cast(@endtime as datetime)

select @starttime1=DATEADD(DAY,-DATEPART(day,@starttime)+1,@starttime)
select @starttime1=DATEADD(MINUTE,-DATEPART(MINUTE,@starttime1),@starttime1)
select @starttime1=DATEADD(SECOND,-DATEPART(SECOND,@starttime1),@starttime1)
select @starttime1=DATEADD(HOUR,-DATEPART(HOUR,@starttime1),@starttime1)

select @count = count(*) from dbo.FUN_split(@mp,',') 
set @i = 1
set @s = 0
	if(@mp!='-')
	begin

		DECLARE curRunStatus cursor for 
		select * from dbo.FUN_split(@mp,',') 
		open curRunStatus

		while(@i<=@count)
		begin

		FETCH curRunStatus into @mptemp
		--print @mptemp
		
		if(@type='equal') 
		begin
			SET @strSQL = N''
			SET @strSQL = @strSQL + N'select top 1 @av=ParmValue  from tb_mp_' + @mptemp+N' ' +char(10)
			SET @strSQL = @strSQL + N'where measuredt = ''' +cast(@starttime as varchar(20)) +N'''  order by MeasureDT asc'+char(10)
			print @strsql
			exec sp_executesql @strsql,N'@av money output',@vtemp output

			--保留小数位
			set @numTailS=0
			SET @mainSQL = N''
			SET @mainSQL = @mainSQL + N'select top 1 @numTail=NumTail  from TB_MeasurePoint '
			SET @mainSQL = @mainSQL + N' where MPointCode = ''' +cast(@mptemp as varchar(50)) +N'''  '
			exec sp_executesql @mainSQL,N'@numTail varchar(10) output',@numTail output
			if((@numTail is not null) and (@numTail!='null') ) begin
				set @numTailS=CAST(@numTail as decimal(38,0))
			end

			SET @numTailSQL = N''
			SET @numTailSQL = @numTailSQL + N'set @vtemp=(SELECT CAST(' + cast(@vtemp as varchar(50)) +N' as decimal(38, ' + cast(@numTailS as varchar(50)) +N'))) '
			exec sp_executesql @numTailSQL,N'@vtemp float output',@vtemp output

			if(@vtemp is not null)
			set @s = @s + @vtemp
		end
		if(@type='timeequal')
		begin
			SET @strSQL = N''
			SET @strSQL = @strSQL + N'select top 1 @av=ParmValue  from tb_mp_' + @mptemp+N' ' +char(10)
			SET @strSQL = @strSQL + N'where measuredt = ''' +cast(@starttime as varchar(20)) +N'''  order by MeasureDT asc'+char(10)
			print @strsql
			exec sp_executesql @strsql,N'@av money output',@vtemp output

			--保留小数位
			set @numTailS=0
			SET @mainSQL = N''
			SET @mainSQL = @mainSQL + N'select top 1 @numTail=NumTail  from TB_MeasurePoint '
			SET @mainSQL = @mainSQL + N' where MPointCode = ''' +cast(@mptemp as varchar(50)) +N'''  '
			exec sp_executesql @mainSQL,N'@numTail varchar(10) output',@numTail output
			if((@numTail is not null) and (@numTail!='null') ) begin
				set @numTailS=CAST(@numTail as decimal(38,0))
			end

			SET @numTailSQL = N''
			SET @numTailSQL = @numTailSQL + N'set @vtemp=(SELECT CAST(' + cast(@vtemp as varchar(50)) +N' as decimal(38, ' + cast(@numTailS as varchar(50)) +N'))) '
			exec sp_executesql @numTailSQL,N'@vtemp float output',@vtemp output

			if(@vtemp is not null)
			set @s = @s + @vtemp
		end
		if(@type='first') 
		begin
			SET @strSQL = N''
			SET @strSQL = @strSQL + N'select top 1 @av=ParmValue  from tb_mp_' + @mptemp+N' ' +char(10)
			SET @strSQL = @strSQL + N'where measuredt >= ''' +cast(dateadd(hour,1,@starttime) as varchar(20)) +N''' and measuredt < '''+cast(dateadd(hour,1,@endtime) as varchar(20))+N''' order by MeasureDT asc'+char(10)
			print @strsql
			exec sp_executesql @strsql,N'@av money output',@vtemp output
			--print @vtemp

			--保留小数位
			set @numTailS=0
			SET @mainSQL = N''
			SET @mainSQL = @mainSQL + N'select top 1 @numTail=NumTail  from TB_MeasurePoint '
			SET @mainSQL = @mainSQL + N' where MPointCode = ''' +cast(@mptemp as varchar(50)) +N'''  '
			exec sp_executesql @mainSQL,N'@numTail varchar(10) output',@numTail output
			if((@numTail is not null) and (@numTail!='null') ) begin
				set @numTailS=CAST(@numTail as decimal(38,0))
			end

			if @vtemp is not null
			begin
			SET @numTailSQL = N''
			SET @numTailSQL = @numTailSQL + N'set @vtemp=(SELECT CAST(' + cast(@vtemp as varchar(50)) +N' as decimal(38, ' + cast(@numTailS as varchar(50)) +N'))) '
			exec sp_executesql @numTailSQL,N'@vtemp money output',@vtemp output
			end
			--print @vtemp
			if(@vtemp is not null)
			begin
			--set @s=0
			set @s = @s + @vtemp
			end
			--print @s
		end
		if(@type='first_month') 
		begin
			SET @strSQL = N''
			SET @strSQL = @strSQL + N'select top 1 @av=ParmValue  from tb_mp_' + @mptemp+N' ' +char(10)
			SET @strSQL = @strSQL + N'where measuredt >= ''' +cast(dateadd(day,1,@starttime) as varchar(20)) +N''' and measuredt < '''+cast(dateadd(day,1,@endtime) as varchar(20))+N''' order by MeasureDT asc'+char(10)
			print @strsql
			exec sp_executesql @strsql,N'@av money output',@vtemp output
			--print @vtemp

			--保留小数位
			set @numTailS=0
			SET @mainSQL = N''
			SET @mainSQL = @mainSQL + N'select top 1 @numTail=NumTail  from TB_MeasurePoint '
			SET @mainSQL = @mainSQL + N' where MPointCode = ''' +cast(@mptemp as varchar(50)) +N'''  '
			exec sp_executesql @mainSQL,N'@numTail varchar(10) output',@numTail output
			if((@numTail is not null) and (@numTail!='null') ) begin
				set @numTailS=CAST(@numTail as decimal(38,0))
			end

			if @vtemp is not null
			begin
			SET @numTailSQL = N''
			SET @numTailSQL = @numTailSQL + N'set @vtemp=(SELECT CAST(' + cast(@vtemp as varchar(50)) +N' as decimal(38, ' + cast(@numTailS as varchar(50)) +N'))) '
			exec sp_executesql @numTailSQL,N'@vtemp money output',@vtemp output
			end
			--print @vtemp
			if(@vtemp is not null)
			begin
			--set @s=0
			set @s = @s + @vtemp
			end
			--print @s
		end
		if(@type='yes_last_hour_first') 
		begin
			SET @strSQL = N''
			SET @strSQL = @strSQL + N'select top 1 @av=ParmValue  from tb_mp_' + @mptemp+N' ' +char(10)
			SET @strSQL = @strSQL + N'where measuredt >= ''' +cast(@starttime as varchar(20)) +N''' and measuredt < '''+cast(@endtime as varchar(20))+N''' order by MeasureDT asc'+char(10)
			print @strsql
			exec sp_executesql @strsql,N'@av money output',@vtemp output
			--print @vtemp

			--保留小数位
			set @numTailS=0
			SET @mainSQL = N''
			SET @mainSQL = @mainSQL + N'select top 1 @numTail=NumTail  from TB_MeasurePoint '
			SET @mainSQL = @mainSQL + N' where MPointCode = ''' +cast(@mptemp as varchar(50)) +N'''  '
			exec sp_executesql @mainSQL,N'@numTail varchar(10) output',@numTail output
			if((@numTail is not null) and (@numTail!='null') ) begin
				set @numTailS=CAST(@numTail as decimal(38,0))
			end

			if @vtemp is not null
			begin
			SET @numTailSQL = N''
			SET @numTailSQL = @numTailSQL + N'set @vtemp=(SELECT CAST(' + cast(@vtemp as varchar(50)) +N' as decimal(38, ' + cast(@numTailS as varchar(50)) +N'))) '
			exec sp_executesql @numTailSQL,N'@vtemp float output',@vtemp output
			end
			--print @vtemp
			if(@vtemp is not null)
			begin
			--set @s=0
			set @s = @s + @vtemp
			end
			--print @s
		end
		if(@type='last_month_first') 
		begin
			SET @strSQL = N''
			SET @strSQL = @strSQL + N'select top 1 @av=ParmValue  from tb_mp_' + @mptemp+N' ' +char(10)
			SET @strSQL = @strSQL + N'where measuredt < ''' +cast(@starttime as varchar(20)) +N''' order by MeasureDT desc'+char(10)
			print @strsql
			exec sp_executesql @strsql,N'@av money output',@vtemp output
			--print @vtemp

			--保留小数位
			set @numTailS=0
			SET @mainSQL = N''
			SET @mainSQL = @mainSQL + N'select top 1 @numTail=NumTail  from TB_MeasurePoint '
			SET @mainSQL = @mainSQL + N' where MPointCode = ''' +cast(@mptemp as varchar(50)) +N'''  '
			exec sp_executesql @mainSQL,N'@numTail varchar(10) output',@numTail output
			if((@numTail is not null) and (@numTail!='null') ) begin
				set @numTailS=CAST(@numTail as decimal(38,0))
			end

			if @vtemp is not null
			begin
			SET @numTailSQL = N''
			SET @numTailSQL = @numTailSQL + N'set @vtemp=(SELECT CAST(' + cast(@vtemp as varchar(50)) +N' as decimal(38, ' + cast(@numTailS as varchar(50)) +N'))) '
			exec sp_executesql @numTailSQL,N'@vtemp float output',@vtemp output
			end
			--print @vtemp
			if(@vtemp is not null)
			begin
			--set @s=0
			set @s = @s + @vtemp
			end
			--print @s
		end
		if(@type='first1') 
		begin
			SET @strSQL = N''
			SET @strSQL = @strSQL + N'select top 1 @av=ParmValue  from tb_mp_' + @mptemp+N' ' +char(10)
			SET @strSQL = @strSQL + N'where measuredt >= ''' +cast(@starttime as varchar(20)) +N''' and measuredt < '''+cast(@endtime as varchar(20))+N''' order by MeasureDT asc'+char(10)
			print @strsql
			exec sp_executesql @strsql,N'@av Numeric(20,4) output',@vtemp output
			--print @vtemp

			--保留小数位
			set @numTailS=0
			SET @mainSQL = N''
			SET @mainSQL = @mainSQL + N'select top 1 @numTail=NumTail  from TB_MeasurePoint '
			SET @mainSQL = @mainSQL + N' where MPointCode = ''' +cast(@mptemp as varchar(50)) +N'''  '
			exec sp_executesql @mainSQL,N'@numTail varchar(10) output',@numTail output
			if((@numTail is not null) and (@numTail!='null') ) begin
				set @numTailS=CAST(@numTail as decimal(38,0))
			end
			

			if @vtemp is not null
			begin
			SET @numTailSQL = N''
			SET @numTailSQL = @numTailSQL + N'set @vtemp=(SELECT CAST(' + cast(@vtemp as varchar(50)) +N' as decimal(38, ' + cast(@numTailS as varchar(50)) +N'))) '
			exec sp_executesql @numTailSQL,N'@vtemp float output',@vtemp output
			end
			--print @vtemp
			if(@vtemp is not null)
			begin
			--set @s=0
			set @s = @s + @vtemp
			end
			print @s
		end
		if(@type='first2') 
		begin
			SET @strSQL = N''
			SET @strSQL = @strSQL + N'select top 1 @av=ParmValue  from tb_mp_' + @mptemp+N' ' +char(10)
			SET @strSQL = @strSQL + N'where measuredt >= ''' +cast(dateadd(hour,8,@starttime) as varchar(20)) +N''' and measuredt < '''+cast(dateadd(hour,8,@endtime) as varchar(20))+N''' order by MeasureDT asc'+char(10)
			print @strsql
			exec sp_executesql @strsql,N'@av money output',@vtemp output
			--print @vtemp

			--保留小数位
			set @numTailS=0
			SET @mainSQL = N''
			SET @mainSQL = @mainSQL + N'select top 1 @numTail=NumTail  from TB_MeasurePoint '
			SET @mainSQL = @mainSQL + N' where MPointCode = ''' +cast(@mptemp as varchar(50)) +N'''  '
			exec sp_executesql @mainSQL,N'@numTail varchar(10) output',@numTail output
			if((@numTail is not null) and (@numTail!='null') ) begin
				set @numTailS=CAST(@numTail as decimal(38,0))
			end

			if @vtemp is not null
			begin
			SET @numTailSQL = N''
			SET @numTailSQL = @numTailSQL + N'set @vtemp=(SELECT CAST(' + cast(@vtemp as varchar(50)) +N' as decimal(38, ' + cast(@numTailS as varchar(50)) +N'))) '
			exec sp_executesql @numTailSQL,N'@vtemp money output',@vtemp output
			end
			--print @vtemp
			if(@vtemp is not null)
			begin
			--set @s=0
			set @s = @s + @vtemp
			end
			--print @s
		end
		if(@type='first3') 
		begin
			SET @strSQL = N''
			SET @strSQL = @strSQL + N'select top 1 @av=ParmValue  from tb_mp_' + @mptemp+N' ' +char(10)
			SET @strSQL = @strSQL + N'where measuredt >= ''' +cast(dateadd(hour,16,@starttime) as varchar(20)) +N''' and measuredt < '''+cast(dateadd(hour,16,@endtime) as varchar(20))+N''' order by MeasureDT asc'+char(10)
			print @strsql
			exec sp_executesql @strsql,N'@av money output',@vtemp output
			--print @vtemp

			--保留小数位
			set @numTailS=0
			SET @mainSQL = N''
			SET @mainSQL = @mainSQL + N'select top 1 @numTail=NumTail  from TB_MeasurePoint '
			SET @mainSQL = @mainSQL + N' where MPointCode = ''' +cast(@mptemp as varchar(50)) +N'''  '
			exec sp_executesql @mainSQL,N'@numTail varchar(10) output',@numTail output
			if((@numTail is not null) and (@numTail!='null') ) begin
				set @numTailS=CAST(@numTail as decimal(38,0))
			end

			if @vtemp is not null
			begin
			SET @numTailSQL = N''
			SET @numTailSQL = @numTailSQL + N'set @vtemp=(SELECT CAST(' + cast(@vtemp as varchar(50)) +N' as decimal(38, ' + cast(@numTailS as varchar(50)) +N'))) '
			exec sp_executesql @numTailSQL,N'@vtemp money output',@vtemp output
			end
			--print @vtemp
			if(@vtemp is not null)
			begin
			--set @s=0
			set @s = @s + @vtemp
			end
			--print @s
		end
		if(@type='firstzuo') 
		begin
			SET @strSQL = N''
			SET @strSQL = @strSQL + N'select top 1 @av=ParmValue  from tb_mp_' + @mptemp+N' ' +char(10)
			SET @strSQL = @strSQL + N'where measuredt >= ''' +cast(dateadd(day,1,@starttime) as varchar(20)) +N''' and measuredt < '''+cast(dateadd(day,1,@endtime) as varchar(20))+N''' order by MeasureDT asc'+char(10)
			print @strsql
			exec sp_executesql @strsql,N'@av money output',@vtemp output
			--print @vtemp

			--保留小数位
			set @numTailS=0
			SET @mainSQL = N''
			SET @mainSQL = @mainSQL + N'select top 1 @numTail=NumTail  from TB_MeasurePoint '
			SET @mainSQL = @mainSQL + N' where MPointCode = ''' +cast(@mptemp as varchar(50)) +N'''  '
			exec sp_executesql @mainSQL,N'@numTail varchar(10) output',@numTail output
			if((@numTail is not null) and (@numTail!='null') ) begin
				set @numTailS=CAST(@numTail as decimal(38,0))
			end

			if @vtemp is not null
			begin
			SET @numTailSQL = N''
			SET @numTailSQL = @numTailSQL + N'set @vtemp=(SELECT CAST(' + cast(@vtemp as varchar(50)) +N' as decimal(38, ' + cast(@numTailS as varchar(50)) +N'))) '
			exec sp_executesql @numTailSQL,N'@vtemp float output',@vtemp output
			end
			--print @vtemp
			if(@vtemp is not null)
			begin
			--set @s=0
			set @s = @s + @vtemp
			end
			--print @s
		end
		if(@type='first8') 
		begin
			SET @strSQL = N''
			SET @strSQL = @strSQL + N'select top 1 @av=ParmValue  from tb_mp_' + @mptemp+N' ' +char(10)
			SET @strSQL = @strSQL + N'where measuredt >= ''' +cast(dateadd(hour,-4,@starttime) as varchar(20)) +N''' and measuredt < '''+cast(@endtime as varchar(20))+N''' order by MeasureDT asc'+char(10)
			print @strsql
			exec sp_executesql @strsql,N'@av money output',@vtemp output
			--print @vtemp

			--保留小数位
			set @numTailS=0
			SET @mainSQL = N''
			SET @mainSQL = @mainSQL + N'select top 1 @numTail=NumTail  from TB_MeasurePoint '
			SET @mainSQL = @mainSQL + N' where MPointCode = ''' +cast(@mptemp as varchar(50)) +N'''  '
			exec sp_executesql @mainSQL,N'@numTail varchar(10) output',@numTail output
			if((@numTail is not null) and (@numTail!='null') ) begin
				set @numTailS=CAST(@numTail as decimal(38,0))
			end

			if @vtemp is not null
			begin
			SET @numTailSQL = N''
			SET @numTailSQL = @numTailSQL + N'set @vtemp=(SELECT CAST(' + cast(@vtemp as varchar(50)) +N' as decimal(38, ' + cast(@numTailS as varchar(50)) +N'))) '
			exec sp_executesql @numTailSQL,N'@vtemp float output',@vtemp output
			end
			--print @vtemp
			if(@vtemp is not null)
			begin
			--set @s=0
			set @s = @s + @vtemp
			end
			--print @s
		end
		if(@type='firsthalf') 
		begin
			SET @strSQL = N''
			SET @strSQL = @strSQL + N'select top 1 @av=ParmValue  from tb_mp_' + @mptemp+N' ' +char(10)
			SET @strSQL = @strSQL + N'where measuredt >= ''' +cast(@starttime as varchar(20)) +N''' and measuredt < '''+cast(@endtime as varchar(20))+N''' order by MeasureDT asc'+char(10)
			print @strsql
			exec sp_executesql @strsql,N'@av money output',@vtemp output

			--保留小数位
			set @numTailS=0
			SET @mainSQL = N''
			SET @mainSQL = @mainSQL + N'select top 1 @numTail=NumTail  from TB_MeasurePoint '
			SET @mainSQL = @mainSQL + N' where MPointCode = ''' +cast(@mptemp as varchar(50)) +N'''  '
			exec sp_executesql @mainSQL,N'@numTail varchar(10) output',@numTail output
			if((@numTail is not null) and (@numTail!='null') ) begin
				set @numTailS=CAST(@numTail as decimal(38,0))
				--print   @numtails
			end

			if @vtemp is not null
			begin
			SET @numTailSQL = N''
			SET @numTailSQL = @numTailSQL + N'set @vtemp=(SELECT CAST(' + cast(@vtemp as varchar(50)) +N' as decimal(38, ' + cast(@numTailS as varchar(50)) +N'))) '
			exec sp_executesql @numTailSQL,N'@vtemp float output',@vtemp output
			end
			--print @vtemp
			if(@vtemp is not null)
			begin
			--set @s=0
			set @s = @s + @vtemp
			end
			--print @s
		end
		if(@type='firstrun') 
		begin
			set @v = null
			set @s = null
		
			SET @strSQL = N''
			SET @strSQL = @strSQL + N'select top 1 @av=measuredt  from [tb_mp_' + @mp+N'_hour]' +char(10)
			SET @strSQL = @strSQL + N'where measuredt >= ''' +cast(dateadd(hour,-1,@starttime) as varchar(20)) +N''' and measuredt < '''+cast(@starttime as varchar(20))+N''' order by MeasureDT asc'+char(10)
			print @strsql
			exec sp_executesql @strsql,N'@av varchar(20) output',@v3 output
			print @v3
			SET @strSQL = N''
			SET @strSQL = @strSQL + N'select top 1 @av=ParmValue  from [tb_mp_' + @mp+N'] ' +char(10)
			SET @strSQL = @strSQL + N'where measuredt >= ''' +cast(dateadd(hour,-1,@starttime) as varchar(20)) +N''' and measuredt < '''+cast(@starttime as varchar(20))+N''' order by MeasureDT asc'+char(10)
			print @strsql
			exec sp_executesql @strsql,N'@av money output',@s output
		end
		if(@type='shushu') 
		begin
			SET @strSQL = N''
			SET @strSQL = @strSQL + N'select  @av=count(ParmValue) from tb_mp_' + @mptemp+N' ' +char(10)
			SET @strSQL = @strSQL + N'where measuredt >= ''' +cast(@starttime as varchar(20)) +N''' and measuredt < '''+cast(@endtime as varchar(20))+N''''+char(10)
			print @strsql
			exec sp_executesql @strsql,N'@av money output',@vtemp output

			--保留小数位
			set @numTailS=0
			SET @mainSQL = N''
			SET @mainSQL = @mainSQL + N'select top 1 @numTail=NumTail  from TB_MeasurePoint '
			SET @mainSQL = @mainSQL + N' where MPointCode = ''' +cast(@mptemp as varchar(50)) +N'''  '
			exec sp_executesql @mainSQL,N'@numTail varchar(10) output',@numTail output
			if((@numTail is not null) and (@numTail!='null') ) begin
				set @numTailS=CAST(@numTail as decimal(38,0))
			end

			SET @numTailSQL = N''
			SET @numTailSQL = @numTailSQL + N'set @vtemp=(SELECT CAST(' + cast(@vtemp as varchar(50)) +N' as decimal(38, ' + cast(@numTailS as varchar(50)) +N'))) '
			exec sp_executesql @numTailSQL,N'@vtemp float output',@vtemp output

			if(@vtemp is not null)
			set @s = @s + @vtemp
		end
		if(@type='shushumth') 
		begin
			SET @strSQL = N''
			SET @strSQL = @strSQL + N'select  @av=count(ParmValue) from tb_mp_' + @mptemp+N' ' +char(10)
			SET @strSQL = @strSQL + N'where measuredt >= ''' +cast(@starttime1 as varchar(20)) +N''' and measuredt < '''+cast(@endtime as varchar(20))+N''''+char(10)
			print @strsql
			exec sp_executesql @strsql,N'@av money output',@vtemp output

			--保留小数位
			set @numTailS=0
			SET @mainSQL = N''
			SET @mainSQL = @mainSQL + N'select top 1 @numTail=NumTail  from TB_MeasurePoint '
			SET @mainSQL = @mainSQL + N' where MPointCode = ''' +cast(@mptemp as varchar(50)) +N'''  '
			exec sp_executesql @mainSQL,N'@numTail varchar(10) output',@numTail output
			if((@numTail is not null) and (@numTail!='null') ) begin
				set @numTailS=CAST(@numTail as decimal(38,0))
			end

			SET @numTailSQL = N''
			SET @numTailSQL = @numTailSQL + N'set @vtemp=(SELECT CAST(' + cast(@vtemp as varchar(50)) +N' as decimal(38, ' + cast(@numTailS as varchar(50)) +N'))) '
			exec sp_executesql @numTailSQL,N'@vtemp float output',@vtemp output

			if(@vtemp is not null)
			set @s = @s + @vtemp
		end
		if(@type='last') 
		begin
			SET @strSQL = N''
			SET @strSQL = @strSQL + N'select top 1 @av=ParmValue from tb_mp_' + @mptemp+N' ' +char(10)
			SET @strSQL = @strSQL + N'where measuredt > ''' +cast(@starttime as varchar(20)) +N''' and measuredt <= '''+cast(@endtime as varchar(20))+N''' order by MeasureDT desc'+char(10)
			print @strsql
			exec sp_executesql @strsql,N'@av money output',@vtemp output

			--保留小数位
			set @numTailS=0
			SET @mainSQL = N''
			SET @mainSQL = @mainSQL + N'select top 1 @numTail=NumTail  from TB_MeasurePoint '
			SET @mainSQL = @mainSQL + N' where MPointCode = ''' +cast(@mptemp as varchar(50)) +N'''  '
			exec sp_executesql @mainSQL,N'@numTail varchar(10) output',@numTail output
			if((@numTail is not null) and (@numTail!='null') ) begin
				set @numTailS=CAST(@numTail as decimal(38,0))
			end

			SET @numTailSQL = N''
			SET @numTailSQL = @numTailSQL + N'set @vtemp=(SELECT CAST(' + cast(@vtemp as varchar(50)) +N' as decimal(38, ' + cast(@numTailS as varchar(50)) +N'))) '
			exec sp_executesql @numTailSQL,N'@vtemp float output',@vtemp output

			if(@vtemp is not null)
			--set @s = 0
			set @s = @s + @vtemp
		end
		if(@type='avg') 
		begin
			SET @strSQL = N''
			SET @strSQL = @strSQL + N'select  @av=avg(ParmValue) from tb_mp_' + @mptemp+N' ' +char(10)
			SET @strSQL = @strSQL + N'where measuredt between ''' +cast(@starttime as varchar(20)) +N''' and '''+cast(@endtime as varchar(20))+N''' and parmvalue <> 0 '+char(10)
			print @strsql
			exec sp_executesql @strsql,N'@av money output',@vtemp output

			--保留小数位
			set @numTailS=0
			SET @mainSQL = N''
			SET @mainSQL = @mainSQL + N'select top 1 @numTail=NumTail  from TB_MeasurePoint '
			SET @mainSQL = @mainSQL + N' where MPointCode = ''' +cast(@mptemp as varchar(50)) +N'''  '
			exec sp_executesql @mainSQL,N'@numTail varchar(10) output',@numTail output
			if((@numTail is not null) and (@numTail!='null') ) begin
				set @numTailS=CAST(@numTail as decimal(38,0))
			end

			SET @numTailSQL = N''
			SET @numTailSQL = @numTailSQL + N'set @vtemp=(SELECT CAST(' + cast(@vtemp as varchar(50)) +N' as decimal(38, ' + cast(@numTailS as varchar(50)) +N'))) '
			exec sp_executesql @numTailSQL,N'@vtemp float output',@vtemp output

			if(@vtemp is not null)
			--set @s = 0
			set @s = @s + @vtemp
		end
		if(@type='avgmth') 
		begin
			SET @strSQL = N''
			SET @strSQL = @strSQL + N'select  @av=avg(ParmValue) from tb_mp_' + @mptemp+N' ' +char(10)
			SET @strSQL = @strSQL + N'where measuredt between ''' +cast(@starttime1 as varchar(20)) +N''' and '''+cast(@endtime as varchar(20))+N''' and parmvalue <> 0 '+char(10)
			print @strsql
			exec sp_executesql @strsql,N'@av money output',@vtemp output

			--保留小数位
			set @numTailS=0
			SET @mainSQL = N''
			SET @mainSQL = @mainSQL + N'select top 1 @numTail=NumTail  from TB_MeasurePoint '
			SET @mainSQL = @mainSQL + N' where MPointCode = ''' +cast(@mptemp as varchar(50)) +N'''  '
			exec sp_executesql @mainSQL,N'@numTail varchar(10) output',@numTail output
			if((@numTail is not null) and (@numTail!='null') ) begin
				set @numTailS=CAST(@numTail as decimal(38,0))
			end

			SET @numTailSQL = N''
			SET @numTailSQL = @numTailSQL + N'set @vtemp=(SELECT CAST(' + cast(@vtemp as varchar(50)) +N' as decimal(38, ' + cast(@numTailS as varchar(50)) +N'))) '
			exec sp_executesql @numTailSQL,N'@vtemp float output',@vtemp output

			if(@vtemp is not null)
			--set @s = 0
			set @s = @s + @vtemp
		end
		if(@type='min') 
		begin
			SET @strSQL = N''
			SET @strSQL = @strSQL + N'select  @av=min(ParmValue) from tb_mp_' + @mptemp+N' ' +char(10)
			SET @strSQL = @strSQL + N'where measuredt between ''' +cast(@starttime as varchar(20)) +N''' and '''+cast(@endtime as varchar(20))+N''''+char(10)
			print @strsql
			exec sp_executesql @strsql,N'@av money output',@vtemp output

			--保留小数位
			set @numTailS=0
			SET @mainSQL = N''
			SET @mainSQL = @mainSQL + N'select top 1 @numTail=NumTail  from TB_MeasurePoint '
			SET @mainSQL = @mainSQL + N' where MPointCode = ''' +cast(@mptemp as varchar(50)) +N'''  '
			exec sp_executesql @mainSQL,N'@numTail varchar(10) output',@numTail output
			if((@numTail is not null) and (@numTail!='null') ) begin
				set @numTailS=CAST(@numTail as decimal(38,0))
			end

			SET @numTailSQL = N''
			SET @numTailSQL = @numTailSQL + N'set @vtemp=(SELECT CAST(' + cast(@vtemp as varchar(50)) +N' as decimal(38, ' + cast(@numTailS as varchar(50)) +N'))) '
			exec sp_executesql @numTailSQL,N'@vtemp float output',@vtemp output

			if(@vtemp is not null)
			set @s = @s + @vtemp
		end
		if(@type='max') 
		begin
			SET @strSQL = N''
			SET @strSQL = @strSQL + N'select  @av=max(ParmValue) from tb_mp_' + @mptemp+N' ' +char(10)
			SET @strSQL = @strSQL + N'where measuredt between ''' +cast(dateadd(minute,5,@starttime) as varchar(20)) +N''' and '''+cast(@endtime as varchar(20))+N''''+char(10)
			print @strsql
			exec sp_executesql @strsql,N'@av money output',@vtemp output

			--保留小数位
			set @numTailS=0
			SET @mainSQL = N''
			SET @mainSQL = @mainSQL + N'select top 1 @numTail=NumTail  from TB_MeasurePoint '
			SET @mainSQL = @mainSQL + N' where MPointCode = ''' +cast(@mptemp as varchar(50)) +N'''  '
			exec sp_executesql @mainSQL,N'@numTail varchar(10) output',@numTail output
			if((@numTail is not null) and (@numTail!='null') ) begin
				set @numTailS=CAST(@numTail as decimal(38,0))
			end

			SET @numTailSQL = N''
			SET @numTailSQL = @numTailSQL + N'set @vtemp=(SELECT CAST(' + cast(@vtemp as varchar(50)) +N' as decimal(38, ' + cast(@numTailS as varchar(50)) +N'))) '
			exec sp_executesql @numTailSQL,N'@vtemp float output',@vtemp output

			if(@vtemp is not null)
			begin
			--set @s = 0
			set @s = @s + @vtemp
			end
		end
		if(@type='diffarea') 
		begin
			SET @strSQL = N''
			SET @strSQL = @strSQL + N'select top 1 @av=parmvalue from tb_mp_' + @mptemp+N' ' +char(10)
			SET @strSQL = @strSQL + N'where measuredt >= ''' +cast(@starttime as varchar(20)) +N''' and measuredt < '''+cast(@endtime as varchar(20))+N''' order by measuredt asc'+char(10)
			print @strsql
			exec sp_executesql @strsql,N'@av money output',@v1 output
			print cast(@v1 as varchar(20))

			SET @strSQL = N''
			SET @strSQL = @strSQL + N'select top 1 @av=parmvalue from tb_mp_' + @mptemp+N' ' +char(10)
			SET @strSQL = @strSQL + N'where measuredt >= ''' +cast(@endtime as varchar(20)) +N''' and measuredt < '''+cast(dateadd(day,1,@endtime) as varchar(20))+N''' order by measuredt asc'+char(10)
			print @strsql
			exec sp_executesql @strsql,N'@av money output',@v2 output
			print cast(@v2 as varchar(20))
			
			set @vtemp = @v2 - @v1
			--print @vtemp
			--保留小数位
			set @numTailS=0
			SET @mainSQL = N''
			SET @mainSQL = @mainSQL + N'select top 1 @numTail=NumTail  from TB_MeasurePoint '
			SET @mainSQL = @mainSQL + N' where MPointCode = ''' +cast(@mptemp as varchar(50)) +N'''  '
			exec sp_executesql @mainSQL,N'@numTail varchar(10) output',@numTail output
			if((@numTail is not null) and (@numTail!='null') ) begin
				set @numTailS=CAST(@numTail as decimal(38,0))
			end

			SET @numTailSQL = N''
			SET @numTailSQL = @numTailSQL + N'set @vtemp=(SELECT CAST(' + cast(@vtemp as varchar(50)) +N' as decimal(38, ' + cast(@numTailS as varchar(50)) +N'))) '
			exec sp_executesql @numTailSQL,N'@vtemp float output',@vtemp output

			if(@vtemp is not null)
			begin
				if(@vtemp<0)
				begin
					--set @s= 0
					set @s = @s 
				end
				else
				begin
					--set @s= 0
					set @s = @s + @vtemp
				end		
			end
			--print @s
		end	
		if(@type='diff') 
		begin
			SET @strSQL = N''
			SET @strSQL = @strSQL + N'select top 1 @av=parmvalue from tb_mp_' + @mptemp+N' ' +char(10)
			SET @strSQL = @strSQL + N'where measuredt >= ''' +cast(@starttime as varchar(20)) +N''' and measuredt < '''+cast(@endtime as varchar(20))+N''' order by measuredt asc'+char(10)
			print @strsql
			exec sp_executesql @strsql,N'@av money output',@v1 output
			print cast(@v1 as varchar(20))

			SET @strSQL = N''
			SET @strSQL = @strSQL + N'select top 1 @av=parmvalue from tb_mp_' + @mptemp+N' ' +char(10)
			SET @strSQL = @strSQL + N'where measuredt >= ''' +cast(dateadd(day,1,@starttime) as varchar(20)) +N''' and measuredt < '''+cast(dateadd(day,1,@endtime) as varchar(20))+N''' order by measuredt asc'+char(10)
			print @strsql
			exec sp_executesql @strsql,N'@av money output',@v2 output
			print cast(@v2 as varchar(20))
			
			set @vtemp = @v2 - @v1
			--print @vtemp
			--保留小数位
			set @numTailS=0
			SET @mainSQL = N''
			SET @mainSQL = @mainSQL + N'select top 1 @numTail=NumTail  from TB_MeasurePoint '
			SET @mainSQL = @mainSQL + N' where MPointCode = ''' +cast(@mptemp as varchar(50)) +N'''  '
			exec sp_executesql @mainSQL,N'@numTail varchar(10) output',@numTail output
			if((@numTail is not null) and (@numTail!='null') ) begin
				set @numTailS=CAST(@numTail as decimal(38,0))
			end

			SET @numTailSQL = N''
			SET @numTailSQL = @numTailSQL + N'set @vtemp=(SELECT CAST(' + cast(@vtemp as varchar(50)) +N' as decimal(38, ' + cast(@numTailS as varchar(50)) +N'))) '
			exec sp_executesql @numTailSQL,N'@vtemp float output',@vtemp output

			if(@vtemp is not null)
			begin
				if(@vtemp<0)
				begin
					--set @s= 0
					set @s = @s 
				end
				else
				begin
					--set @s= 0
					set @s = @s + @vtemp
				end		
			end
			--print @s
		end	
		if(@type='diff_hour') 
		begin
			SET @strSQL = N''
			SET @strSQL = @strSQL + N'select top 1 @av=parmvalue from tb_mp_' + @mptemp+N' ' +char(10)
			SET @strSQL = @strSQL + N'where measuredt >= ''' +cast(@starttime as varchar(20)) +N''' and measuredt < '''+cast(@endtime as varchar(20))+N''' order by measuredt asc'+char(10)
			print @strsql
			exec sp_executesql @strsql,N'@av money output',@v1 output
			print cast(@v1 as varchar(20))

			SET @strSQL = N''
			SET @strSQL = @strSQL + N'select top 1 @av=parmvalue from tb_mp_' + @mptemp+N' ' +char(10)
			SET @strSQL = @strSQL + N'where measuredt >= ''' +cast(dateadd(hour,1,@starttime) as varchar(20)) +N''' and measuredt < '''+cast(dateadd(hour,1,@endtime) as varchar(20))+N''' order by measuredt asc'+char(10)
			print @strsql
			exec sp_executesql @strsql,N'@av money output',@v2 output
			print cast(@v2 as varchar(20))
			
			set @vtemp = @v2 - @v1
			--print @vtemp
			--保留小数位
			set @numTailS=0
			SET @mainSQL = N''
			SET @mainSQL = @mainSQL + N'select top 1 @numTail=NumTail  from TB_MeasurePoint '
			SET @mainSQL = @mainSQL + N' where MPointCode = ''' +cast(@mptemp as varchar(50)) +N'''  '
			exec sp_executesql @mainSQL,N'@numTail varchar(10) output',@numTail output
			if((@numTail is not null) and (@numTail!='null') ) begin
				set @numTailS=CAST(@numTail as decimal(38,0))
			end

			SET @numTailSQL = N''
			SET @numTailSQL = @numTailSQL + N'set @vtemp=(SELECT CAST(' + cast(@vtemp as varchar(50)) +N' as decimal(38, ' + cast(@numTailS as varchar(50)) +N'))) '
			exec sp_executesql @numTailSQL,N'@vtemp float output',@vtemp output

			if(@vtemp is not null)
			begin
				if(@vtemp<0)
				begin
					--set @s= 0
					set @s = @s 
				end
				else
				begin
					--set @s= 0
					set @s = @s + @vtemp
				end		
			end
			--print @s
		end	
		if(@type='diff_hour_min') 
		begin
			SET @strSQL = N''
			SET @strSQL = @strSQL + N'select top 1 @av=parmvalue from tb_mp_' + @mptemp+N' ' +char(10)
			SET @strSQL = @strSQL + N'where measuredt >= ''' +cast(@starttime as varchar(20)) +N''' and measuredt < '''+cast(@endtime as varchar(20))+N''' order by measuredt asc'+char(10)
			print @strsql
			exec sp_executesql @strsql,N'@av money output',@v1 output
			print cast(@v1 as varchar(20))

			SET @strSQL = N''
			SET @strSQL = @strSQL + N'select top 1 @av=parmvalue from tb_mp_' + @mptemp+N' ' +char(10)
			SET @strSQL = @strSQL + N'where measuredt >= ''' +cast(dateadd(hour,1,@starttime) as varchar(20)) +N''' and measuredt < '''+cast(dateadd(hour,1,@endtime) as varchar(20))+N''' order by measuredt asc'+char(10)
			print @strsql
			exec sp_executesql @strsql,N'@av money output',@v2 output
			print cast(@v2 as varchar(20))
			
			set @vtemp2 = @v2 - @v1
			--print @vtemp2 
			--保留小数位
			set @numTailS=0
			SET @mainSQL = N''
			SET @mainSQL = @mainSQL + N'select top 1 @numTail=NumTail  from TB_MeasurePoint '
			SET @mainSQL = @mainSQL + N' where MPointCode = ''' +cast(@mptemp as varchar(50)) +N'''  '
			exec sp_executesql @mainSQL,N'@numTail varchar(10) output',@numTail output
			if((@numTail is not null) and (@numTail!='null') ) begin
				set @numTailS=CAST(@numTail as decimal(38,0))
			end

			SET @numTailSQL = N''
			SET @numTailSQL = @numTailSQL + N'set @vtemp=(SELECT CAST(' + cast(@vtemp as varchar(50)) +N' as decimal(38, ' + cast(@numTailS as varchar(50)) +N'))) '
			exec sp_executesql @numTailSQL,N'@vtemp float output',@vtemp output


			print @vtemp2
			
			if(@vtemp2 is not null)
			begin
				if(@vtemp2<0)
				begin
					--set @s= 0
					set @s = @s *60
				end
				else
				begin

					--set @s= 0
					set @s = (@s + @vtemp2) * 60
				end		
			end
			begin
					if(@s>=59)
					begin
						--set @s= 0
						set @s = 60
					end
			end		
			--print @s
		end	
		if(@type='diffsec') 
		begin
			SET @strSQL = N''
			SET @strSQL = @strSQL + N'select top 1 @av=parmvalue from tb_mp_' + @mptemp+N' ' +char(10)
			SET @strSQL = @strSQL + N'where measuredt >= ''' +cast(@starttime as varchar(20)) +N''' and measuredt < '''+cast(@endtime as varchar(20))+N''' order by measuredt asc'+char(10)
			print @strsql
			exec sp_executesql @strsql,N'@av money output',@v1 output
			print cast(@v1 as varchar(20))

			SET @strSQL = N''
			SET @strSQL = @strSQL + N'select top 1 @av=parmvalue from tb_mp_' + @mptemp+N' ' +char(10)
			SET @strSQL = @strSQL + N'where measuredt >= ''' +cast(dateadd(day,1,@starttime) as varchar(20)) +N''' and measuredt < '''+cast(dateadd(day,1,@endtime) as varchar(20))+N''' order by measuredt asc'+char(10)
			print @strsql
			exec sp_executesql @strsql,N'@av money output',@v2 output
			print cast(@v2 as varchar(20))
			
			set @vtemp = @v2 - @v1
			--print @vtemp
			--保留小数位
			set @numTailS=0
			SET @mainSQL = N''
			SET @mainSQL = @mainSQL + N'select top 1 @numTail=NumTail  from TB_MeasurePoint '
			SET @mainSQL = @mainSQL + N' where MPointCode = ''' +cast(@mptemp as varchar(50)) +N'''  '
			exec sp_executesql @mainSQL,N'@numTail varchar(10) output',@numTail output
			if((@numTail is not null) and (@numTail!='null') ) begin
				set @numTailS=CAST(@numTail as decimal(38,0))
			end

			SET @numTailSQL = N''
			SET @numTailSQL = @numTailSQL + N'set @vtemp=(SELECT CAST(' + cast(@vtemp as varchar(50)) +N' as decimal(38, ' + cast(@numTailS as varchar(50)) +N'))) '
			exec sp_executesql @numTailSQL,N'@vtemp float output',@vtemp output

			if(@vtemp is not null)
			begin
				if(@vtemp<0)
				begin
					--set @s= 0
					set @s = @s 
				end
				else
				begin
					--set @s= 0
					set @s = (@s + @vtemp)/3600
				end		
			end
			--print @s
		end	
		if(@type='diffH') 
		begin
			SET @strSQL = N''
			SET @strSQL = @strSQL + N'select top 1 @av=parmvalue from tb_mp_' + @mptemp+N' ' +char(10)
			SET @strSQL = @strSQL + N'where measuredt >= ''' +cast(@starttime as varchar(20)) +N''' and measuredt < '''+cast(@endtime as varchar(20))+N''' order by measuredt asc'+char(10)
			print @strsql
			exec sp_executesql @strsql,N'@av money output',@v1 output
			print cast(@v1 as varchar(20))

			SET @strSQL = N''
			SET @strSQL = @strSQL + N'select top 1 @av=parmvalue from tb_mp_' + @mptemp+N' ' +char(10)
			SET @strSQL = @strSQL + N'where measuredt >= ''' +cast(dateadd(HOUR,1,@starttime) as varchar(20)) +N''' and measuredt < '''+cast(dateadd(HOUR,1,@endtime) as varchar(20))+N''' order by measuredt asc'+char(10)
			print @strsql
			exec sp_executesql @strsql,N'@av money output',@v2 output
			print cast(@v2 as varchar(20))
			
			set @vtemp = @v2 - @v1
			--print @vtemp
			--保留小数位
			set @numTailS=0
			SET @mainSQL = N''
			SET @mainSQL = @mainSQL + N'select top 1 @numTail=NumTail  from TB_MeasurePoint '
			SET @mainSQL = @mainSQL + N' where MPointCode = ''' +cast(@mptemp as varchar(50)) +N'''  '
			exec sp_executesql @mainSQL,N'@numTail varchar(10) output',@numTail output
			if((@numTail is not null) and (@numTail!='null') ) begin
				set @numTailS=CAST(@numTail as decimal(38,0))
			end

			SET @numTailSQL = N''
			SET @numTailSQL = @numTailSQL + N'set @vtemp=(SELECT CAST(' + cast(@vtemp as varchar(50)) +N' as decimal(38, ' + cast(@numTailS as varchar(50)) +N'))) '
			exec sp_executesql @numTailSQL,N'@vtemp float output',@vtemp output

			if(@vtemp is not null)
			begin
				if(@vtemp<0)
				begin
					--set @s= 0
					set @s = @s 
				end
				else
				begin
					--set @s= 0
					set @s = @s + @vtemp
				end		
			end
			--print @s
		end	
		if(@type='diff_hour') 
		begin
			SET @strSQL = N''
			SET @strSQL = @strSQL + N'select top 1 @av=parmvalue from tb_mp_' + @mptemp+N' ' +char(10)
			SET @strSQL = @strSQL + N'where measuredt >= ''' +cast(@starttime as varchar(20)) +N''' and measuredt < '''+cast(@endtime as varchar(20))+N''' order by measuredt asc'+char(10)
			print @strsql
			exec sp_executesql @strsql,N'@av money output',@v1 output
			print cast(@v1 as varchar(20))

			SET @strSQL = N''
			SET @strSQL = @strSQL + N'select top 1 @av=parmvalue from tb_mp_' + @mptemp+N' ' +char(10)
			SET @strSQL = @strSQL + N'where measuredt >= ''' +cast(dateadd(hour,1,@starttime) as varchar(20)) +N''' and measuredt < '''+cast(dateadd(day,1,@endtime) as varchar(20))+N''' order by measuredt asc'+char(10)
			print @strsql
			exec sp_executesql @strsql,N'@av money output',@v2 output
			print cast(@v2 as varchar(20))
			
			set @vtemp = @v2 - @v1
			--print @vtemp
			--保留小数位
			set @numTailS=0
			SET @mainSQL = N''
			SET @mainSQL = @mainSQL + N'select top 1 @numTail=NumTail  from TB_MeasurePoint '
			SET @mainSQL = @mainSQL + N' where MPointCode = ''' +cast(@mptemp as varchar(50)) +N'''  '
			exec sp_executesql @mainSQL,N'@numTail varchar(10) output',@numTail output
			if((@numTail is not null) and (@numTail!='null') ) begin
				set @numTailS=CAST(@numTail as decimal(38,0))
			end

			SET @numTailSQL = N''
			SET @numTailSQL = @numTailSQL + N'set @vtemp=(SELECT CAST(' + cast(@vtemp as varchar(50)) +N' as decimal(38, ' + cast(@numTailS as varchar(50)) +N'))) '
			exec sp_executesql @numTailSQL,N'@vtemp float output',@vtemp output

			if(@vtemp is not null)
			begin
				if(@vtemp<0)
				begin
					--set @s= 0
					set @s = @s 
				end
				else
				begin
					--set @s= 0
					set @s = @s + @vtemp
				end		
			end
			--print @s
		end	
		if(@type='diff1') 
		begin
			SET @strSQL = N''
			SET @strSQL = @strSQL + N'select top 1 @av=parmvalue from tb_mp_' + @mptemp+N' ' +char(10)
			SET @strSQL = @strSQL + N'where measuredt >= ''' +cast(@starttime as varchar(20)) +N''' and measuredt < '''+cast(@endtime as varchar(20))+N''' order by measuredt asc'+char(10)
			print @strsql
			exec sp_executesql @strsql,N'@av money output',@v1 output
			print cast(@v1 as varchar(20))

			SET @strSQL = N''
			SET @strSQL = @strSQL + N'select top 1 @av=parmvalue from tb_mp_' + @mptemp+N' ' +char(10)
			SET @strSQL = @strSQL + N'where measuredt >= ''' +cast(dateadd(day,1,@starttime) as varchar(20)) +N''' and measuredt < '''+cast(dateadd(day,1,@endtime) as varchar(20))+N''' order by measuredt asc'+char(10)
			print @strsql
			exec sp_executesql @strsql,N'@av money output',@v2 output
			print cast(@v2 as varchar(20))
			
			set @vtemp = @v2 - @v1

			--保留小数位
			set @numTailS=0
			SET @mainSQL = N''
			SET @mainSQL = @mainSQL + N'select top 1 @numTail=NumTail  from TB_MeasurePoint '
			SET @mainSQL = @mainSQL + N' where MPointCode = ''' +cast(@mptemp as varchar(50)) +N'''  '
			exec sp_executesql @mainSQL,N'@numTail varchar(10) output',@numTail output
			if((@numTail is not null) and (@numTail!='null') ) begin
				set @numTailS=CAST(@numTail as decimal(38,0))
			end

			SET @numTailSQL = N''
			SET @numTailSQL = @numTailSQL + N'set @vtemp=(SELECT CAST(' + cast(@vtemp as varchar(50)) +N' as decimal(38, ' + cast(@numTailS as varchar(50)) +N'))) '
			exec sp_executesql @numTailSQL,N'@vtemp float output',@vtemp output

			--if(@vtemp is not null)
			--begin
				--if(@vtemp<0)
				--begin
					--set @s = @s 
				--end
				--else
				--begin
					set @s = @s + @vtemp
				--end		
			--end
			
		end	
		if(@type='diff6465') 
		begin
			SET @strSQL = N''
			SET @strSQL = @strSQL + N'select top 1 @av=parmvalue from tb_mp_' + @mptemp+N' ' +char(10)
			SET @strSQL = @strSQL + N'where measuredt >= ''' +cast(@starttime as varchar(20)) +N''' and measuredt < '''+cast(@endtime as varchar(20))+N''' order by measuredt asc'+char(10)
			print @strsql
			exec sp_executesql @strsql,N'@av money output',@v1 output
			print cast(@v1 as varchar(20))

			SET @strSQL = N''
			SET @strSQL = @strSQL + N'select top 1 @av=parmvalue from tb_mp_' + @mptemp+N' ' +char(10)
			SET @strSQL = @strSQL + N'where measuredt >= ''' +cast(dateadd(day,1,@starttime) as varchar(20)) +N''' and measuredt < '''+cast(dateadd(day,1,@endtime) as varchar(20))+N''' order by measuredt asc'+char(10)
			print @strsql
			exec sp_executesql @strsql,N'@av money output',@v2 output
			print cast(@v2 as varchar(20))
			
			set @vtemp = @v2 - @v1

			--保留小数位
			set @numTailS=0
			SET @mainSQL = N''
			SET @mainSQL = @mainSQL + N'select top 1 @numTail=NumTail  from TB_MeasurePoint '
			SET @mainSQL = @mainSQL + N' where MPointCode = ''' +cast(@mptemp as varchar(50)) +N'''  '
			exec sp_executesql @mainSQL,N'@numTail varchar(10) output',@numTail output
			if((@numTail is not null) and (@numTail!='null') ) begin
				set @numTailS=CAST(@numTail as decimal(38,0))
			end

			SET @numTailSQL = N''
			SET @numTailSQL = @numTailSQL + N'set @vtemp=(SELECT CAST(' + cast(@vtemp as varchar(50)) +N' as decimal(38, ' + cast(@numTailS as varchar(50)) +N'))) '
			exec sp_executesql @numTailSQL,N'@vtemp float output',@vtemp output

			--if(@vtemp is not null)
			--begin
				--if(@vtemp<0)
				--begin
					--set @s = @s 
				--end
				--else
				--begin
					set @s = 0
					set @s = (@s + @vtemp)*6465.6
				--end		
			--end
			
		end	
		if(@type='diff60') 
		begin
			SET @strSQL = N''
			SET @strSQL = @strSQL + N'select top 1 @av=parmvalue from tb_mp_' + @mptemp+N' ' +char(10)
			SET @strSQL = @strSQL + N'where measuredt >= ''' +cast(@starttime as varchar(20)) +N''' and measuredt < '''+cast(@endtime as varchar(20))+N''' order by measuredt asc'+char(10)
			print @strsql
			exec sp_executesql @strsql,N'@av money output',@v1 output
			print cast(@v1 as varchar(20))

			SET @strSQL = N''
			SET @strSQL = @strSQL + N'select top 1 @av=parmvalue from tb_mp_' + @mptemp+N' ' +char(10)
			SET @strSQL = @strSQL + N'where measuredt >= ''' +cast(dateadd(day,1,@starttime) as varchar(20)) +N''' and measuredt < '''+cast(dateadd(day,1,@endtime) as varchar(20))+N''' order by measuredt asc'+char(10)
			print @strsql
			exec sp_executesql @strsql,N'@av money output',@v2 output
			print cast(@v2 as varchar(20))
			
			set @vtemp = @v2 - @v1

			--保留小数位
			set @numTailS=0
			SET @mainSQL = N''
			SET @mainSQL = @mainSQL + N'select top 1 @numTail=NumTail  from TB_MeasurePoint '
			SET @mainSQL = @mainSQL + N' where MPointCode = ''' +cast(@mptemp as varchar(50)) +N'''  '
			exec sp_executesql @mainSQL,N'@numTail varchar(10) output',@numTail output
			if((@numTail is not null) and (@numTail!='null') ) begin
				set @numTailS=CAST(@numTail as decimal(38,0))
			end

			SET @numTailSQL = N''
			SET @numTailSQL = @numTailSQL + N'set @vtemp=(SELECT CAST(' + cast(@vtemp as varchar(50)) +N' as decimal(38, ' + cast(@numTailS as varchar(50)) +N'))) '
			exec sp_executesql @numTailSQL,N'@vtemp float output',@vtemp output

			if(@vtemp is not null)
			begin
				if(@vtemp<0)
				begin
					set @s = @s
				end
				else
				begin
					set @s = (@s + @vtemp)/60
				end		
			end
			
		end	
		if(@type='diffx') 
		begin
			SET @strSQL = N''
			SET @strSQL = @strSQL + N'select top 1 @av=parmvalue from tb_mp_' + @mptemp+N' ' +char(10)
			SET @strSQL = @strSQL + N'where measuredt >= ''' +cast(@starttime as varchar(20)) +N''' and measuredt < '''+cast(dateadd(day,1,@starttime)  as varchar(20))+N''' order by measuredt asc'+char(10)
			print @strsql
			exec sp_executesql @strsql,N'@av money output',@v1 output
			print cast(@v1 as varchar(20))

			SET @strSQL = N''
			SET @strSQL = @strSQL + N'select top 1 @av=parmvalue from tb_mp_' + @mptemp+N' ' +char(10)
			SET @strSQL = @strSQL + N'where measuredt >= ''' +cast(@endtime as varchar(20)) +N''' and measuredt < '''+cast(dateadd(day,1,@endtime) as varchar(20))+N''' order by measuredt asc'+char(10)
			print @strsql
			exec sp_executesql @strsql,N'@av money output',@v2 output
			print cast(@v2 as varchar(20))
			
			set @vtemp = @v2 - @v1

			--保留小数位
			set @numTailS=0
			SET @mainSQL = N''
			SET @mainSQL = @mainSQL + N'select top 1 @numTail=NumTail  from TB_MeasurePoint '
			SET @mainSQL = @mainSQL + N' where MPointCode = ''' +cast(@mptemp as varchar(50)) +N'''  '
			exec sp_executesql @mainSQL,N'@numTail varchar(10) output',@numTail output
			if((@numTail is not null) and (@numTail!='null') ) begin
				set @numTailS=CAST(@numTail as decimal(38,0))
			end

			SET @numTailSQL = N''
			SET @numTailSQL = @numTailSQL + N'set @vtemp=(SELECT CAST(' + cast(@vtemp as varchar(50)) +N' as decimal(38, ' + cast(@numTailS as varchar(50)) +N'))) '
			exec sp_executesql @numTailSQL,N'@vtemp float output',@vtemp output

			if(@vtemp is not null)
			begin
				if(@vtemp<0)
				begin
					set @s = @s
				end
				else
				begin
					set @s = @s + @vtemp
				end		
			end
			
		end	
		if(@type='diffm') 
		begin
			SET @strSQL = N''
			SET @strSQL = @strSQL + N'select top 1 @av=parmvalue from [tb_mp_' + @mp+N'] ' +char(10)
			SET @strSQL = @strSQL + N'where measuredt >= ''' +cast(@starttime as varchar(20)) +N''' and measuredt < '''+cast(dateadd(day,1,@endtime) as varchar(20))+N''' order by measuredt asc'+char(10)
			print @strsql
			exec sp_executesql @strsql,N'@av money output',@v1 output
			print cast(@v1 as varchar(20))

			SET @strSQL = N''
			SET @strSQL = @strSQL + N'select top 1 @av=parmvalue from [tb_mp_' + @mp+N'] ' +char(10)
			SET @strSQL = @strSQL + N'where measuredt >= ''' +cast(@endtime as varchar(20)) +N''' and measuredt < '''+cast(dateadd(day,1,@endtime) as varchar(20))+N''' order by measuredt asc'+char(10)
			print @strsql
			exec sp_executesql @strsql,N'@av money output',@v2 output
			print cast(@v2 as varchar(20))
			
			set @s = @v2 - @v1
			if(@s is not null)
			begin
				if(@s<0)
				begin
					set @v = null
				end
				else
				begin
					set @v = @s
				end
			end
			else
				set @v = null
			
		end
		if(@type='diffmonth') 
		begin
			SET @strSQL = N''
			SET @strSQL = @strSQL + N'select top 1 @av=parmvalue from [tb_mp_' + @mp+N'] ' +char(10)
			SET @strSQL = @strSQL + N'where measuredt >= ''' +cast(@starttime1 as varchar(20)) +N''' and measuredt < '''+cast(dateadd(day,1,@endtime) as varchar(20))+N''' order by measuredt asc'+char(10)
			print @strsql
			exec sp_executesql @strsql,N'@av money output',@v1 output
			print cast(@v1 as varchar(20))

			SET @strSQL = N''
			SET @strSQL = @strSQL + N'select top 1 @av=parmvalue from [tb_mp_' + @mp+N'] ' +char(10)
			SET @strSQL = @strSQL + N'where measuredt >= ''' +cast(@endtime as varchar(20)) +N''' and measuredt < '''+cast(dateadd(day,1,@endtime) as varchar(20))+N''' order by measuredt asc'+char(10)
			print @strsql
			exec sp_executesql @strsql,N'@av money output',@v2 output
			print cast(@v2 as varchar(20))
			
			set @vtemp = @v2 - @v1

			--保留小数位
			set @numTailS=0
			SET @mainSQL = N''
			SET @mainSQL = @mainSQL + N'select top 1 @numTail=NumTail  from TB_MeasurePoint '
			SET @mainSQL = @mainSQL + N' where MPointCode = ''' +cast(@mptemp as varchar(50)) +N'''  '
			exec sp_executesql @mainSQL,N'@numTail varchar(10) output',@numTail output
			if((@numTail is not null) and (@numTail!='null') ) begin
				set @numTailS=CAST(@numTail as decimal(38,0))
			end

			SET @numTailSQL = N''
			SET @numTailSQL = @numTailSQL + N'set @vtemp=(SELECT CAST(' + cast(@vtemp as varchar(50)) +N' as decimal(38, ' + cast(@numTailS as varchar(50)) +N'))) '
			exec sp_executesql @numTailSQL,N'@vtemp float output',@vtemp output

			if(@vtemp is not null)
			begin
				if(@vtemp<0)
				begin
					set @s = @s
				end
				else
				begin
					set @s = @s + @vtemp
				end		
			end
		end
		if(@type='diffmonthsec') 
		begin
			SET @strSQL = N''
			SET @strSQL = @strSQL + N'select top 1 @av=parmvalue from [tb_mp_' + @mp+N'] ' +char(10)
			SET @strSQL = @strSQL + N'where measuredt >= ''' +cast(@starttime1 as varchar(20)) +N''' and measuredt < '''+cast(dateadd(day,1,@endtime) as varchar(20))+N''' order by measuredt asc'+char(10)
			print @strsql
			exec sp_executesql @strsql,N'@av money output',@v1 output
			print cast(@v1 as varchar(20))

			SET @strSQL = N''
			SET @strSQL = @strSQL + N'select top 1 @av=parmvalue from [tb_mp_' + @mp+N'] ' +char(10)
			SET @strSQL = @strSQL + N'where measuredt >= ''' +cast(@endtime as varchar(20)) +N''' and measuredt < '''+cast(dateadd(day,1,@endtime) as varchar(20))+N''' order by measuredt asc'+char(10)
			print @strsql
			exec sp_executesql @strsql,N'@av money output',@v2 output
			print cast(@v2 as varchar(20))
			
			set @vtemp = @v2 - @v1

			--保留小数位
			set @numTailS=0
			SET @mainSQL = N''
			SET @mainSQL = @mainSQL + N'select top 1 @numTail=NumTail  from TB_MeasurePoint '
			SET @mainSQL = @mainSQL + N' where MPointCode = ''' +cast(@mptemp as varchar(50)) +N'''  '
			exec sp_executesql @mainSQL,N'@numTail varchar(10) output',@numTail output
			if((@numTail is not null) and (@numTail!='null') ) begin
				set @numTailS=CAST(@numTail as decimal(38,0))
			end

			SET @numTailSQL = N''
			SET @numTailSQL = @numTailSQL + N'set @vtemp=(SELECT CAST(' + cast(@vtemp as varchar(50)) +N' as decimal(38, ' + cast(@numTailS as varchar(50)) +N'))) '
			exec sp_executesql @numTailSQL,N'@vtemp float output',@vtemp output

			if(@vtemp is not null)
			begin
				if(@vtemp<0)
				begin
					set @s = @s
				end
				else
				begin
					set @s = (@s + @vtemp)/3600
				end		
			end
		end
		if(@type='sum') 
		begin
			SET @strSQL = N''
			SET @strSQL = @strSQL + N'select  @av=sum(ParmValue) from tb_mp_' + @mptemp+N' ' +char(10)
			SET @strSQL = @strSQL + N'where measuredt >= ''' +cast(@starttime as varchar(20)) +N''' and measuredt < '''+cast(@endtime as varchar(20))+N''''+char(10)
			print @strsql
			exec sp_executesql @strsql,N'@av money output',@vtemp output

			--保留小数位
			set @numTailS=0
			SET @mainSQL = N''
			SET @mainSQL = @mainSQL + N'select top 1 @numTail=NumTail  from TB_MeasurePoint '
			SET @mainSQL = @mainSQL + N' where MPointCode = ''' +cast(@mptemp as varchar(50)) +N'''  '
			exec sp_executesql @mainSQL,N'@numTail varchar(10) output',@numTail output
			if((@numTail is not null) and (@numTail!='null') ) begin
				set @numTailS=CAST(@numTail as decimal(38,0))
			end

			SET @numTailSQL = N''
			SET @numTailSQL = @numTailSQL + N'set @vtemp=(SELECT CAST(' + cast(@vtemp as varchar(50)) +N' as decimal(38, ' + cast(@numTailS as varchar(50)) +N'))) '
			exec sp_executesql @numTailSQL,N'@vtemp float output',@vtemp output

			if(@vtemp is not null)
			set @s = @s + @vtemp
		end
		if(@type='total') 
		begin
			SET @strSQL = N''
			SET @strSQL = @strSQL + N'select  @av=count(ParmValue) from tb_mp_' + @mptemp+N' ' +char(10)
			SET @strSQL = @strSQL + N'where measuredt >= ''' +cast(@starttime as varchar(20)) +N''' and measuredt < '''+cast(@endtime as varchar(20))+N''''+char(10)
			print @strsql
			exec sp_executesql @strsql,N'@av money output',@vtemp output

			--保留小数位
			set @numTailS=0
			SET @mainSQL = N''
			SET @mainSQL = @mainSQL + N'select top 1 @numTail=NumTail  from TB_MeasurePoint '
			SET @mainSQL = @mainSQL + N' where MPointCode = ''' +cast(@mptemp as varchar(50)) +N'''  '
			exec sp_executesql @mainSQL,N'@numTail varchar(10) output',@numTail output
			if((@numTail is not null) and (@numTail!='null') ) begin
				set @numTailS=CAST(@numTail as decimal(38,0))
			end

			SET @numTailSQL = N''
			SET @numTailSQL = @numTailSQL + N'set @vtemp=(SELECT CAST(' + cast(@vtemp as varchar(50)) +N' as decimal(38, ' + cast(@numTailS as varchar(50)) +N'))) '
			exec sp_executesql @numTailSQL,N'@vtemp float output',@vtemp output

			if(@vtemp is not null)
			set @s = @s + @vtemp
		end
		if(@type='qual_upper') 
		begin
			SET @tempSQL = N''
			set @tempVAL=0
			SET @tempSQL = @tempSQL + N'select top 1 @tempVAL=alarmmax  from TB_MeasurePoint '
			SET @tempSQL = @tempSQL + N' where MPointCode = ''' +cast(@mptemp as varchar(50)) +N'''  '
			exec sp_executesql @tempSQL,N'@tempVAL varchar(10) output',@tempVAL output
			print @tempSQL
			print @tempVAL
			
			SET @strSQL = N''
			SET @strSQL = @strSQL + N'select  @av=count(ParmValue) from tb_mp_' + @mptemp+N' ' +char(10)
			SET @strSQL = @strSQL + N'where measuredt >= ''' +cast(@starttime as varchar(20)) +N''' and measuredt < '''+cast(@endtime as varchar(20))+N''''+char(10)
			SET @strSQL = @strSQL + N' and ParmValue <=  ' +cast(@tempVAL as varchar(20)) +char(10)
			print @strsql
			exec sp_executesql @strsql,N'@av money output',@vtemp output

			--保留小数位
			set @numTailS=0
			SET @mainSQL = N''
			SET @mainSQL = @mainSQL + N'select top 1 @numTail=NumTail  from TB_MeasurePoint '
			SET @mainSQL = @mainSQL + N' where MPointCode = ''' +cast(@mptemp as varchar(50)) +N'''  '
			exec sp_executesql @mainSQL,N'@numTail varchar(10) output',@numTail output
			print @mainSQL
			if((@numTail is not null) and (@numTail!='null') ) begin
				set @numTailS=CAST(@numTail as decimal(38,0))
			end

			SET @numTailSQL = N''
			SET @numTailSQL = @numTailSQL + N'set @vtemp=(SELECT CAST(' + cast(@vtemp as varchar(50)) +N' as decimal(38, ' + cast(@numTailS as varchar(50)) +N'))) '
			exec sp_executesql @numTailSQL,N'@vtemp float output',@vtemp output

			if(@vtemp is not null)
			set @s = @s + @vtemp
		end
		if(@type='qual_upper_rate') 
		begin
			SET @tempSQL = N''
			set @tempVAL=0
			SET @tempSQL = @tempSQL + N'select top 1 @tempVAL=alarmmax  from TB_MeasurePoint '
			SET @tempSQL = @tempSQL + N' where MPointCode = ''' +cast(@mptemp as varchar(50)) +N'''  '
			exec sp_executesql @tempSQL,N'@tempVAL varchar(10) output',@tempVAL output
			--print @tempSQL
			--print @tempVAL
			
			set @tempVAL2 = 0
			SET @tempSQL = N''
			SET @tempSQL = @tempSQL + N'select @av2=count(ItemID) from tb_mp_' + @mptemp+N' ' +char(10)
			SET @tempSQL = @tempSQL + N'where measuredt >= ''' +cast(@starttime as varchar(20)) +N''' and measuredt < '''+cast(@endtime as varchar(20))+N''''+char(10)
			SET @tempSQL = @tempSQL + N' and ParmValue <=  ' +cast(@tempVAL as varchar(20)) +char(10)
			exec sp_executesql @tempSQL,N'@av2 money output',@tempVAL2 output
			print @tempSQL
			print @tempVAL2

			SET @strSQL = N''
			SET @strSQL = @strSQL + N'select  @av=count(ItemID) from tb_mp_' + @mptemp+N' ' +char(10)
			SET @strSQL = @strSQL + N'where measuredt >= ''' +cast(@starttime as varchar(20)) +N''' and measuredt < '''+cast(@endtime as varchar(20))+N''''+char(10)
			print @strsql
			
			exec sp_executesql @strsql,N'@av money output',@vtemp output

			--保留小数位
			set @numTailS=0
			SET @mainSQL = N''
			SET @mainSQL = @mainSQL + N'select top 1 @numTail=NumTail  from TB_MeasurePoint '
			SET @mainSQL = @mainSQL + N' where MPointCode = ''' +cast(@mptemp as varchar(50)) +N'''  '
			exec sp_executesql @mainSQL,N'@numTail varchar(10) output',@numTail output
			--print @mainSQL
			if((@numTail is not null) and (@numTail!='null') ) begin
				set @numTailS=CAST(@numTail as decimal(38,0))
			end

			SET @numTailSQL = N''
			SET @numTailSQL = @numTailSQL + N'set @vtemp=(SELECT CAST(' + cast(@vtemp as varchar(50)) +N' as decimal(38, ' + cast(@numTailS as varchar(50)) +N'))) '
			exec sp_executesql @numTailSQL,N'@vtemp float output',@vtemp output

			if(@vtemp is not null)
			set @s = @s + @vtemp
			if(@vtemp = 0)
			set @s = 0
			else
			set @s = @tempVAL2/@vtemp
			print @tempVAL2
			print @vtemp
			print @s
		end
		if(@type='qual_upper_max') 
		begin
			SET @tempSQL = N''
			set @tempVAL=0
			SET @tempSQL = @tempSQL + N'select top 1 @tempVAL=halarmmax  from TB_MeasurePoint '
			SET @tempSQL = @tempSQL + N' where MPointCode = ''' +cast(@mptemp as varchar(50)) +N'''  '
			exec sp_executesql @tempSQL,N'@tempVAL varchar(10) output',@tempVAL output
			print @tempSQL
			print @tempVAL
			
			SET @strSQL = N''
			SET @strSQL = @strSQL + N'select  @av=count(ParmValue) from tb_mp_' + @mptemp+N' ' +char(10)
			SET @strSQL = @strSQL + N'where measuredt >= ''' +cast(@starttime as varchar(20)) +N''' and measuredt < '''+cast(@endtime as varchar(20))+N''''+char(10)
			SET @strSQL = @strSQL + N' and ParmValue <=  ' +cast(@tempVAL as varchar(20)) +char(10)
			print @strsql
			exec sp_executesql @strsql,N'@av money output',@vtemp output

			--保留小数位
			set @numTailS=0
			SET @mainSQL = N''
			SET @mainSQL = @mainSQL + N'select top 1 @numTail=NumTail  from TB_MeasurePoint '
			SET @mainSQL = @mainSQL + N' where MPointCode = ''' +cast(@mptemp as varchar(50)) +N'''  '
			exec sp_executesql @mainSQL,N'@numTail varchar(10) output',@numTail output
			print @mainSQL
			if((@numTail is not null) and (@numTail!='null') ) begin
				set @numTailS=CAST(@numTail as decimal(38,0))
			end

			SET @numTailSQL = N''
			SET @numTailSQL = @numTailSQL + N'set @vtemp=(SELECT CAST(' + cast(@vtemp as varchar(50)) +N' as decimal(38, ' + cast(@numTailS as varchar(50)) +N'))) '
			exec sp_executesql @numTailSQL,N'@vtemp float output',@vtemp output

			if(@vtemp is not null)
			set @s = @s + @vtemp
		end
		if(@type='qual_upper_max_rate') 
		begin
			SET @tempSQL = N''
			set @tempVAL=0
			SET @tempSQL = @tempSQL + N'select top 1 @tempVAL=halarmmax  from TB_MeasurePoint '
			SET @tempSQL = @tempSQL + N' where MPointCode = ''' +cast(@mptemp as varchar(50)) +N'''  '
			exec sp_executesql @tempSQL,N'@tempVAL varchar(10) output',@tempVAL output
			print @tempSQL
			print @tempVAL
			
			set @tempVAL2 = 0
			SET @tempSQL = N''
			SET @tempSQL = @tempSQL + N'select  @av=count(ParmValue) from tb_mp_' + @mptemp+N' ' +char(10)
			SET @tempSQL = @tempSQL + N'where measuredt >= ''' +cast(@starttime as varchar(20)) +N''' and measuredt < '''+cast(@endtime as varchar(20))+N''''+char(10)
			SET @tempSQL = @tempSQL + N' and ParmValue <=  ' +cast(@tempVAL as varchar(20)) +char(10)
			exec sp_executesql @tempSQL,N'@av money output',@tempVAL2 output
			print @tempSQL
			print @tempVAL2

			SET @strSQL = N''
			SET @strSQL = @strSQL + N'select  @av=count(ParmValue) from tb_mp_' + @mptemp+N' ' +char(10)
			SET @strSQL = @strSQL + N'where measuredt >= ''' +cast(@starttime as varchar(20)) +N''' and measuredt < '''+cast(@endtime as varchar(20))+N''''+char(10)
			print @strsql
			exec sp_executesql @strsql,N'@av money output',@vtemp output

			--保留小数位
			set @numTailS=0
			SET @mainSQL = N''
			SET @mainSQL = @mainSQL + N'select top 1 @numTail=NumTail  from TB_MeasurePoint '
			SET @mainSQL = @mainSQL + N' where MPointCode = ''' +cast(@mptemp as varchar(50)) +N'''  '
			exec sp_executesql @mainSQL,N'@numTail varchar(10) output',@numTail output
			print @mainSQL
			if((@numTail is not null) and (@numTail!='null') ) begin
				set @numTailS=CAST(@numTail as decimal(38,0))
			end

			SET @numTailSQL = N''
			SET @numTailSQL = @numTailSQL + N'set @vtemp=(SELECT CAST(' + cast(@vtemp as varchar(50)) +N' as decimal(38, ' + cast(@numTailS as varchar(50)) +N'))) '
			exec sp_executesql @numTailSQL,N'@vtemp float output',@vtemp output

			if(@vtemp is not null)
			set @s = @s + @vtemp
			if(@vtemp = 0)
			set @s = 0
			else
			set @s = @tempVAL2/@vtemp
			print @tempVAL2
			print @vtemp
		end
		if(@type='qual_lower') 
		begin
			SET @tempSQL = N''
			set @tempVAL=0
			SET @tempSQL = @tempSQL + N'select top 1 @tempVAL=alarmmin  from TB_MeasurePoint '
			SET @tempSQL = @tempSQL + N' where MPointCode = ''' +cast(@mptemp as varchar(50)) +N'''  '
			exec sp_executesql @tempSQL,N'@tempVAL varchar(10) output',@tempVAL output
			print @tempSQL
			print @tempVAL
			
			SET @strSQL = N''
			SET @strSQL = @strSQL + N'select  @av=count(ParmValue) from tb_mp_' + @mptemp+N' ' +char(10)
			SET @strSQL = @strSQL + N'where measuredt >= ''' +cast(@starttime as varchar(20)) +N''' and measuredt < '''+cast(@endtime as varchar(20))+N''''+char(10)
			SET @strSQL = @strSQL + N' and ParmValue >=  ' +cast(@tempVAL as varchar(20)) +char(10)
			print @strsql
			exec sp_executesql @strsql,N'@av money output',@vtemp output

			--保留小数位
			set @numTailS=0
			SET @mainSQL = N''
			SET @mainSQL = @mainSQL + N'select top 1 @numTail=NumTail  from TB_MeasurePoint '
			SET @mainSQL = @mainSQL + N' where MPointCode = ''' +cast(@mptemp as varchar(50)) +N'''  '
			exec sp_executesql @mainSQL,N'@numTail varchar(10) output',@numTail output
			print @mainSQL
			if((@numTail is not null) and (@numTail!='null') ) begin
				set @numTailS=CAST(@numTail as decimal(38,0))
			end

			SET @numTailSQL = N''
			SET @numTailSQL = @numTailSQL + N'set @vtemp=(SELECT CAST(' + cast(@vtemp as varchar(50)) +N' as decimal(38, ' + cast(@numTailS as varchar(50)) +N'))) '
			exec sp_executesql @numTailSQL,N'@vtemp float output',@vtemp output

			if(@vtemp is not null)
			set @s = @s + @vtemp
		end
		if(@type='qual_lower_rate') 
		begin
			SET @tempSQL = N''
			set @tempVAL=0
			SET @tempSQL = @tempSQL + N'select top 1 @tempVAL=alarmmin  from TB_MeasurePoint '
			SET @tempSQL = @tempSQL + N' where MPointCode = ''' +cast(@mptemp as varchar(50)) +N'''  '
			exec sp_executesql @tempSQL,N'@tempVAL varchar(10) output',@tempVAL output
			print @tempSQL
			print @tempVAL
			
			set @tempVAL2 = 0
			SET @tempSQL = N''
			SET @tempSQL = @tempSQL + N'select  @av=count(ParmValue) from tb_mp_' + @mptemp+N' ' +char(10)
			SET @tempSQL = @tempSQL + N'where measuredt >= ''' +cast(@starttime as varchar(20)) +N''' and measuredt < '''+cast(@endtime as varchar(20))+N''''+char(10)
			SET @tempSQL = @tempSQL + N' and ParmValue >=  ' +cast(@tempVAL as varchar(20)) +char(10)
			exec sp_executesql @tempSQL,N'@av money output',@tempVAL2 output
			print @tempSQL
			print @tempVAL2

			SET @strSQL = N''
			SET @strSQL = @strSQL + N'select  @av=count(ParmValue) from tb_mp_' + @mptemp+N' ' +char(10)
			SET @strSQL = @strSQL + N'where measuredt >= ''' +cast(@starttime as varchar(20)) +N''' and measuredt < '''+cast(@endtime as varchar(20))+N''''+char(10)
			print @strsql
			exec sp_executesql @strsql,N'@av money output',@vtemp output

			--保留小数位
			set @numTailS=0
			SET @mainSQL = N''
			SET @mainSQL = @mainSQL + N'select top 1 @numTail=NumTail  from TB_MeasurePoint '
			SET @mainSQL = @mainSQL + N' where MPointCode = ''' +cast(@mptemp as varchar(50)) +N'''  '
			exec sp_executesql @mainSQL,N'@numTail varchar(10) output',@numTail output
			print @mainSQL
			if((@numTail is not null) and (@numTail!='null') ) begin
				set @numTailS=CAST(@numTail as decimal(38,0))
			end

			SET @numTailSQL = N''
			SET @numTailSQL = @numTailSQL + N'set @vtemp=(SELECT CAST(' + cast(@vtemp as varchar(50)) +N' as decimal(38, ' + cast(@numTailS as varchar(50)) +N'))) '
			exec sp_executesql @numTailSQL,N'@vtemp float output',@vtemp output

			if(@vtemp is not null)
			set @s = @s + @vtemp
			if(@vtemp = 0)
			set @s = 0
			else
			set @s = @tempVAL2/@vtemp
			print @tempVAL2
			print @vtemp
		end
		if(@type='qual_lower_max') 
		begin
			SET @tempSQL = N''
			set @tempVAL=0
			SET @tempSQL = @tempSQL + N'select top 1 @tempVAL=lalarmmin  from TB_MeasurePoint '
			SET @tempSQL = @tempSQL + N' where MPointCode = ''' +cast(@mptemp as varchar(50)) +N'''  '
			exec sp_executesql @tempSQL,N'@tempVAL varchar(10) output',@tempVAL output
			print @tempSQL
			print @tempVAL
			
			SET @strSQL = N''
			SET @strSQL = @strSQL + N'select  @av=count(ParmValue) from tb_mp_' + @mptemp+N' ' +char(10)
			SET @strSQL = @strSQL + N'where measuredt >= ''' +cast(@starttime as varchar(20)) +N''' and measuredt < '''+cast(@endtime as varchar(20))+N''''+char(10)
			SET @strSQL = @strSQL + N' and ParmValue >=  ' +cast(@tempVAL as varchar(20)) +char(10)
			print @strsql
			exec sp_executesql @strsql,N'@av money output',@vtemp output

			--保留小数位
			set @numTailS=0
			SET @mainSQL = N''
			SET @mainSQL = @mainSQL + N'select top 1 @numTail=NumTail  from TB_MeasurePoint '
			SET @mainSQL = @mainSQL + N' where MPointCode = ''' +cast(@mptemp as varchar(50)) +N'''  '
			exec sp_executesql @mainSQL,N'@numTail varchar(10) output',@numTail output
			print @mainSQL
			if((@numTail is not null) and (@numTail!='null') ) begin
				set @numTailS=CAST(@numTail as decimal(38,0))
			end

			SET @numTailSQL = N''
			SET @numTailSQL = @numTailSQL + N'set @vtemp=(SELECT CAST(' + cast(@vtemp as varchar(50)) +N' as decimal(38, ' + cast(@numTailS as varchar(50)) +N'))) '
			exec sp_executesql @numTailSQL,N'@vtemp float output',@vtemp output

			if(@vtemp is not null)
			set @s = @s + @vtemp
		end
		if(@type='qual_lower_max_rate') 
		begin
			SET @tempSQL = N''
			set @tempVAL=0
			SET @tempSQL = @tempSQL + N'select top 1 @tempVAL=lalarmmin  from TB_MeasurePoint '
			SET @tempSQL = @tempSQL + N' where MPointCode = ''' +cast(@mptemp as varchar(50)) +N'''  '
			exec sp_executesql @tempSQL,N'@tempVAL varchar(10) output',@tempVAL output
			print @tempSQL
			print @tempVAL

			set @tempVAL2 = 0
			SET @tempSQL = N''
			SET @tempSQL = @tempSQL + N'select  @av=count(ParmValue) from tb_mp_' + @mptemp+N' ' +char(10)
			SET @tempSQL = @tempSQL + N'where measuredt >= ''' +cast(@starttime as varchar(20)) +N''' and measuredt < '''+cast(@endtime as varchar(20))+N''''+char(10)
			SET @tempSQL = @tempSQL + N' and ParmValue >=  ' +cast(@tempVAL as varchar(20)) +char(10)
			exec sp_executesql @tempSQL,N'@av money output',@tempVAL2 output
			print @tempSQL
			print @tempVAL2
			
			SET @strSQL = N''
			SET @strSQL = @strSQL + N'select  @av=count(ParmValue) from tb_mp_' + @mptemp+N' ' +char(10)
			SET @strSQL = @strSQL + N'where measuredt >= ''' +cast(@starttime as varchar(20)) +N''' and measuredt < '''+cast(@endtime as varchar(20))+N''''+char(10)
			print @strsql
			exec sp_executesql @strsql,N'@av money output',@vtemp output

			--保留小数位
			set @numTailS=0
			SET @mainSQL = N''
			SET @mainSQL = @mainSQL + N'select top 1 @numTail=NumTail  from TB_MeasurePoint '
			SET @mainSQL = @mainSQL + N' where MPointCode = ''' +cast(@mptemp as varchar(50)) +N'''  '
			exec sp_executesql @mainSQL,N'@numTail varchar(10) output',@numTail output
			print @mainSQL
			if((@numTail is not null) and (@numTail!='null') ) begin
				set @numTailS=CAST(@numTail as decimal(38,0))
			end

			SET @numTailSQL = N''
			SET @numTailSQL = @numTailSQL + N'set @vtemp=(SELECT CAST(' + cast(@vtemp as varchar(50)) +N' as decimal(38, ' + cast(@numTailS as varchar(50)) +N'))) '
			exec sp_executesql @numTailSQL,N'@vtemp float output',@vtemp output

			if(@vtemp is not null)
			set @s = @s + @vtemp
			if(@vtemp = 0)
			set @s = 0
			else
			set @s = @tempVAL2/@vtemp
			print @tempVAL2
			print @vtemp
		end
		if(@type='qual_lower_max_yes_lower_no') 
		begin
			SET @tempSQL = N''
			set @tempVAL=0
			SET @tempSQL = @tempSQL + N'select top 1 @tempVAL=lalarmmin  from TB_MeasurePoint '
			SET @tempSQL = @tempSQL + N' where MPointCode = ''' +cast(@mptemp as varchar(50)) +N'''  '
			exec sp_executesql @tempSQL,N'@tempVAL varchar(10) output',@tempVAL output
			print @tempSQL
			print @tempVAL
			
			SET @tempSQL = N''
			set @tempVAL3=0
			SET @tempSQL = @tempSQL + N'select top 1 @tempVAL3=alarmmin  from TB_MeasurePoint '
			SET @tempSQL = @tempSQL + N' where MPointCode = ''' +cast(@mptemp as varchar(50)) +N'''  '
			exec sp_executesql @tempSQL,N'@tempVAL3 varchar(10) output',@tempVAL3 output
			print @tempSQL
			print @tempVAL3


			SET @strSQL = N''
			SET @strSQL = @strSQL + N'select  @av=count(ParmValue) from tb_mp_' + @mptemp+N' ' +char(10)
			SET @strSQL = @strSQL + N'where measuredt >= ''' +cast(@starttime as varchar(20)) +N''' and measuredt < '''+cast(@endtime as varchar(20))+N''''+char(10)
			SET @strSQL = @strSQL + N' and ParmValue >=  ' +cast(@tempVAL as varchar(20)) +char(10)
			SET @strSQL = @strSQL + N' and ParmValue <=  ' +cast(@tempVAL3 as varchar(20)) +char(10)
			print @strsql
			exec sp_executesql @strsql,N'@av money output',@vtemp output

			--保留小数位
			set @numTailS=0
			SET @mainSQL = N''
			SET @mainSQL = @mainSQL + N'select top 1 @numTail=NumTail  from TB_MeasurePoint '
			SET @mainSQL = @mainSQL + N' where MPointCode = ''' +cast(@mptemp as varchar(50)) +N'''  '
			exec sp_executesql @mainSQL,N'@numTail varchar(10) output',@numTail output
			print @mainSQL
			if((@numTail is not null) and (@numTail!='null') ) begin
				set @numTailS=CAST(@numTail as decimal(38,0))
			end

			SET @numTailSQL = N''
			SET @numTailSQL = @numTailSQL + N'set @vtemp=(SELECT CAST(' + cast(@vtemp as varchar(50)) +N' as decimal(38, ' + cast(@numTailS as varchar(50)) +N'))) '
			exec sp_executesql @numTailSQL,N'@vtemp float output',@vtemp output

			if(@vtemp is not null)
			set @s = @s + @vtemp
		end
		if(@type='qual_lower_max_yes_lower_no_rate') 
		begin
			SET @tempSQL = N''
			set @tempVAL=0
			SET @tempSQL = @tempSQL + N'select top 1 @tempVAL=lalarmmin  from TB_MeasurePoint '
			SET @tempSQL = @tempSQL + N' where MPointCode = ''' +cast(@mptemp as varchar(50)) +N'''  '
			exec sp_executesql @tempSQL,N'@tempVAL varchar(10) output',@tempVAL output
			print @tempSQL
			print @tempVAL

			SET @tempSQL = N''
			set @tempVAL3=0
			SET @tempSQL = @tempSQL + N'select top 1 @tempVAL3=alarmmin  from TB_MeasurePoint '
			SET @tempSQL = @tempSQL + N' where MPointCode = ''' +cast(@mptemp as varchar(50)) +N'''  '
			exec sp_executesql @tempSQL,N'@tempVAL3 varchar(10) output',@tempVAL3 output
			print @tempSQL
			print @tempVAL3


			set @tempVAL2 = 0
			SET @tempSQL = N''
			SET @tempSQL = @tempSQL + N'select  @av=count(ParmValue) from tb_mp_' + @mptemp+N' ' +char(10)
			SET @tempSQL = @tempSQL + N'where measuredt >= ''' +cast(@starttime as varchar(20)) +N''' and measuredt < '''+cast(@endtime as varchar(20))+N''''+char(10)
			SET @tempSQL = @tempSQL + N' and ParmValue >=  ' +cast(@tempVAL as varchar(20)) +char(10)
			SET @tempSQL = @tempSQL + N' and ParmValue <=  ' +cast(@tempVAL3 as varchar(20)) +char(10)
			exec sp_executesql @tempSQL,N'@av money output',@tempVAL2 output
			print @tempSQL
			print @tempVAL2
			
			SET @strSQL = N''
			SET @strSQL = @strSQL + N'select  @av=count(ParmValue) from tb_mp_' + @mptemp+N' ' +char(10)
			SET @strSQL = @strSQL + N'where measuredt >= ''' +cast(@starttime as varchar(20)) +N''' and measuredt < '''+cast(@endtime as varchar(20))+N''''+char(10)
			print @strsql
			exec sp_executesql @strsql,N'@av money output',@vtemp output

			--保留小数位
			set @numTailS=0
			SET @mainSQL = N''
			SET @mainSQL = @mainSQL + N'select top 1 @numTail=NumTail  from TB_MeasurePoint '
			SET @mainSQL = @mainSQL + N' where MPointCode = ''' +cast(@mptemp as varchar(50)) +N'''  '
			exec sp_executesql @mainSQL,N'@numTail varchar(10) output',@numTail output
			print @mainSQL
			if((@numTail is not null) and (@numTail!='null') ) begin
				set @numTailS=CAST(@numTail as decimal(38,0))
			end

			SET @numTailSQL = N''
			SET @numTailSQL = @numTailSQL + N'set @vtemp=(SELECT CAST(' + cast(@vtemp as varchar(50)) +N' as decimal(38, ' + cast(@numTailS as varchar(50)) +N'))) '
			exec sp_executesql @numTailSQL,N'@vtemp float output',@vtemp output

			if(@vtemp is not null)
			set @s = @s + @vtemp
			if(@vtemp = 0)
			set @s = 0
			else
			set @s = @tempVAL2/@vtemp
			print @tempVAL2
			print @vtemp
		end
		if(@type='summonth') 
		begin
			SET @strSQL = N''
			SET @strSQL = @strSQL + N'select  @av=sum(ParmValue) from tb_mp_' + @mptemp+N' ' +char(10)
			SET @strSQL = @strSQL + N'where measuredt >= ''' +cast(dateadd(hour,1,@starttime1) as varchar(20)) +N''' and measuredt < '''+cast(@endtime as varchar(20))+N''''+char(10)
			print @strsql
			exec sp_executesql @strsql,N'@av money output',@vtemp output

			--保留小数位
			set @numTailS=0
			SET @mainSQL = N''
			SET @mainSQL = @mainSQL + N'select top 1 @numTail=NumTail  from TB_MeasurePoint '
			SET @mainSQL = @mainSQL + N' where MPointCode = ''' +cast(@mptemp as varchar(50)) +N'''  '
			exec sp_executesql @mainSQL,N'@numTail varchar(10) output',@numTail output
			if((@numTail is not null) and (@numTail!='null') ) begin
				set @numTailS=CAST(@numTail as decimal(38,0))
			end

			SET @numTailSQL = N''
			SET @numTailSQL = @numTailSQL + N'set @vtemp=(SELECT CAST(' + cast(@vtemp as varchar(50)) +N' as decimal(38, ' + cast(@numTailS as varchar(50)) +N'))) '
			exec sp_executesql @numTailSQL,N'@vtemp float output',@vtemp output

			if(@vtemp is not null)
			set @s = @s + @vtemp
		end
		if(@type='time')
		begin
			SET @strSQL = N''
			SET @strSQL = @strSQL + N'select top 1 @av=parmvalue from tb_mp_' + @mp+N' ' +char(10)
			SET @strSQL = @strSQL + N'where measuredt >= ''' +cast(@starttime as varchar(20)) +N''' and measuredt < '''+cast(@endtime as varchar(20))+N''' order by measuredt asc'+char(10)
			print @strsql
			exec sp_executesql @strsql,N'@av money output',@v1 output
			print cast(@v1 as varchar(20))

			SET @strSQL = N''
			SET @strSQL = @strSQL + N'select top 1 @av=parmvalue from tb_mp_' + @mp+N' ' +char(10)
			SET @strSQL = @strSQL + N'where measuredt >= ''' +cast(dateadd(day,1,@starttime) as varchar(20)) +N''' and measuredt < '''+cast(dateadd(day,1,@endtime) as varchar(20))+N''' order by measuredt asc'+char(10)
			print @strsql
			exec sp_executesql @strsql,N'@av money output',@v2 output
			print cast(@v2 as varchar(20))
			
			set @vtemp = @v2*60 - @v1*60

			--保留小数位
			set @numTailS=0
			SET @mainSQL = N''
			SET @mainSQL = @mainSQL + N'select top 1 @numTail=NumTail  from TB_MeasurePoint '
			SET @mainSQL = @mainSQL + N' where MPointCode = ''' +cast(@mptemp as varchar(50)) +N'''  '
			exec sp_executesql @mainSQL,N'@numTail varchar(10) output',@numTail output
			if((@numTail is not null) and (@numTail!='null') ) begin
				set @numTailS=CAST(@numTail as decimal(38,0))
			end

			SET @numTailSQL = N''
			SET @numTailSQL = @numTailSQL + N'set @vtemp=(SELECT CAST(' + cast(@vtemp as varchar(50)) +N' as decimal(38, ' + cast(@numTailS as varchar(50)) +N'))) '
			exec sp_executesql @numTailSQL,N'@vtemp float output',@vtemp output

			if(@vtemp is not null)
			begin
				if(@vtemp<0)
				begin
					set @s = @s
				end
				else if((@v2*60 - @v1*60) >= 1440 )
				begin
					set @s = @s+24
				end
				else
				begin
					set @s = @s+(@v2*60 - @v1*60)/60.0
				end			
			end
			else
				set @s = @s
		end
		if(@type='timem') 
		begin
			SET @strSQL = N''
			SET @strSQL = @strSQL + N'select top 1 @av=parmvalue from tb_mp_' + @mptemp+N' ' +char(10)
			SET @strSQL = @strSQL + N'where measuredt >= ''' +cast(@starttime as varchar(20)) +N''' and measuredt < '''+cast(@endtime as varchar(20))+N''' order by measuredt asc'+char(10)
			print @strsql
			exec sp_executesql @strsql,N'@av money output',@v1 output
			print cast(@v1 as varchar(20))

			SET @strSQL = N''
			SET @strSQL = @strSQL + N'select top 1 @av=parmvalue from tb_mp_' + @mptemp+N' ' +char(10)
			SET @strSQL = @strSQL + N'where measuredt >= ''' +cast(dateadd(day,1,@starttime) as varchar(20)) +N''' and measuredt < '''+cast(dateadd(day,1,@endtime) as varchar(20))+N''' order by measuredt asc'+char(10)
			print @strsql
			exec sp_executesql @strsql,N'@av money output',@v2 output
			print cast(@v2 as varchar(20))
			
			set @vtemp = @v2 - @v1

			--保留小数位
			set @numTailS=0
			SET @mainSQL = N''
			SET @mainSQL = @mainSQL + N'select top 1 @numTail=NumTail  from TB_MeasurePoint '
			SET @mainSQL = @mainSQL + N' where MPointCode = ''' +cast(@mptemp as varchar(50)) +N'''  '
			exec sp_executesql @mainSQL,N'@numTail varchar(10) output',@numTail output
			if((@numTail is not null) and (@numTail!='null') ) begin
				set @numTailS=CAST(@numTail as decimal(38,0))
			end

			SET @numTailSQL = N''
			SET @numTailSQL = @numTailSQL + N'set @vtemp=(SELECT CAST(' + cast(@vtemp as varchar(50)) +N' as decimal(38, ' + cast(@numTailS as varchar(50)) +N'))) '
			exec sp_executesql @numTailSQL,N'@vtemp float output',@vtemp output

			if(@vtemp is not null)
			begin
				if(@vtemp<0)
				begin
					set @s = @s
				end
				else
				begin
					set @s = @s + @vtemp/60.0
				end		
			end
			
		end		
		if(@type='run') 
		begin
			SET @strSQL = N''
			SET @strSQL = @strSQL + N'select top 1 @av=ParmValue  from tb_mp_' + @mptemp+N' ' +char(10)
			SET @strSQL = @strSQL + N'where measuredt >= ''' +cast(@starttime as varchar(20)) +N''' and measuredt < '''+cast(@endtime as varchar(20))+N''' order by MeasureDT asc'+char(10)
			print @strsql
			exec sp_executesql @strsql,N'@av money output',@vtemp output

			--保留小数位
			set @numTailS=0
			SET @mainSQL = N''
			SET @mainSQL = @mainSQL + N'select top 1 @numTail=NumTail  from TB_MeasurePoint '
			SET @mainSQL = @mainSQL + N' where MPointCode = ''' +cast(@mptemp as varchar(50)) +N'''  '
			exec sp_executesql @mainSQL,N'@numTail varchar(10) output',@numTail output
			if((@numTail is not null) and (@numTail!='null') ) begin
				set @numTailS=CAST(@numTail as decimal(38,0))
			end

			SET @numTailSQL = N''
			SET @numTailSQL = @numTailSQL + N'set @vtemp=(SELECT CAST(' + cast(@vtemp as varchar(50)) +N' as decimal(38, ' + cast(@numTailS as varchar(50)) +N'))) '
			exec sp_executesql @numTailSQL,N'@vtemp float output',@vtemp output

			if(@vtemp is not null)
			set @s = @s + @vtemp
		end

		if(@type='pulldown') --针对数据填报中存的是数字，选择的是中文的变量，在报表总继续显示中文的采集方式
		begin
			SET @strSQL = N''
			SET @strSQL = @strSQL + N'select top 1 @av=ParmValue from tb_mp_' + @mptemp+N' ' +char(10)
			SET @strSQL = @strSQL + N'where measuredt >= ''' +cast(@starttime as varchar(20)) +N''' and measuredt < '''+cast(@endtime as varchar(20))+N''' order by MeasureDT asc'+char(10)
			print @strsql
			exec sp_executesql @strsql,N'@av money output',@vtemp output

			SET @strSQL = N''
			SET @strSQL = @strSQL + N'select top 1 @av=valueMeaning  from TB_MeasurePoint' +N' ' +char(10)
			SET @strSQL = @strSQL + N'where MPointCode = ''' +@mptemp +N''''+char(10)
			print @strsql
			exec sp_executesql @strsql,N'@av varchar(200) output',@vmstr output
			
			--从vmstr中遍历匹配的数值
			if(@vtemp is not null and @vmstr is not null)
			begin
			set @chararray = N''
			set @floatarray = N''
			set @chararray=substring(@vmstr,1,CHARINDEX(',',@vmstr)-1)
			set @floatarray=substring(@vmstr,CHARINDEX(',',@vmstr)+1,len(@vmstr))
			set @charmp = N''
			set @floatmp = N''
			set @x=0
			set @y=0

			print @chararray
			print @floatarray

			DECLARE floatstatus cursor for select * from dbo.FUN_split(@floatarray,'/')
		    open floatstatus
            FETCH floatstatus into @floatmp

			while (@@fetch_status=0)
			begin

				set @x=@x+1
				--print convert(varchar(20),@x)
				--if(convert(varchar(20),@vtemp)=@floatmp)
				if(@vtemp=convert(float,@floatmp))
				begin
				--print convert(varchar(20),@vtemp)
				--print @floatmp
				break 			
				end

			FETCH floatstatus into @floatmp			
			--print 'aaaaa'+@floatmp
			end
			close floatstatus
			DEALLOCATE floatstatus

			DECLARE charstatus cursor for select * from dbo.FUN_split(@chararray,'/')
		    open charstatus
            FETCH charstatus into @charmp

			while (@@fetch_status=0)
			begin

				set @y=@y+1
				--print convert(varchar(20),@y)
				--if(convert(varchar(20),@vtemp)=@floatmp)
				if(@y=@x)
				begin
				--print @charmp
				insert into TB_MP_TEMP_REPORT(ParmValue,MeasureDT,FormatType,decimal,userid,mpcode) values(@charmp,@starttime,'label',0,@userid,@mptemp)
				break 			
				end

			FETCH charstatus into @charmp			
			--print 'bbbbb'+@charmp
			end
			close charstatus
			DEALLOCATE charstatus
			
			end
			set @s=0
		end

		if(@type='mthavg') --只适用于月报平均
		begin
			while(@sdate<@edate)
			begin
			set @tempdate = dateadd(hour,2,@sdate)

			SET @strSQL = N''
			SET @strSQL = @strSQL + N'select top 1 @av=ParmValue  from tb_mp_' + @mptemp+N' ' +char(10)
			SET @strSQL = @strSQL + N'where measuredt >= ''' +cast(@sdate as varchar(20)) +N''' and measuredt < '''+cast(@edate as varchar(20))+N''' order by MeasureDT asc'+char(10)
			print @strsql
			exec sp_executesql @strsql,N'@av money output',@vtemp output

			--保留小数位
			set @numTailS=0
			SET @mainSQL = N''
			SET @mainSQL = @mainSQL + N'select top 1 @numTail=NumTail  from TB_MeasurePoint '
			SET @mainSQL = @mainSQL + N' where MPointCode = ''' +cast(@mptemp as varchar(50)) +N'''  '
			exec sp_executesql @mainSQL,N'@numTail varchar(10) output',@numTail output
			if((@numTail is not null) and (@numTail!='null') ) begin
				set @numTailS=CAST(@numTail as decimal(38,0))
			end

			SET @numTailSQL = N''
			SET @numTailSQL = @numTailSQL + N'set @vtemp=(SELECT CAST(' + cast(@vtemp as varchar(50)) +N' as decimal(38, ' + cast(@numTailS as varchar(50)) +N'))) '
			exec sp_executesql @numTailSQL,N'@vtemp float output',@vtemp output

			if(@vtemp is not null)
			set @s = @s + @vtemp

			set @sdate = @tempdate
			end

			set @s = @s/12
		end
		if(@type='firstavg') 
		begin
			SET @strSQL = N''
			SET @strSQL = @strSQL + N'select top 1 @av=ParmValue  from tb_mp_' + @mptemp+N' ' +char(10)
			SET @strSQL = @strSQL + N'where measuredt >= ''' +cast(@starttime as varchar(20)) +N''' and measuredt < '''+cast(@endtime as varchar(20))+N''' order by MeasureDT asc'+char(10)
			print @strsql
			exec sp_executesql @strsql,N'@av money output',@vtemp output

			--保留小数位
			set @numTailS=0
			SET @mainSQL = N''
			SET @mainSQL = @mainSQL + N'select top 1 @numTail=NumTail  from TB_MeasurePoint '
			SET @mainSQL = @mainSQL + N' where MPointCode = ''' +cast(@mptemp as varchar(50)) +N'''  '
			exec sp_executesql @mainSQL,N'@numTail varchar(10) output',@numTail output
			if((@numTail is not null) and (@numTail!='null') ) begin
				set @numTailS=CAST(@numTail as decimal(38,0))
			end

			SET @numTailSQL = N''
			SET @numTailSQL = @numTailSQL + N'set @vtemp=(SELECT CAST(' + cast(@vtemp as varchar(50)) +N' as decimal(38, ' + cast(@numTailS as varchar(50)) +N'))) '
			exec sp_executesql @numTailSQL,N'@vtemp float output',@vtemp output

			if(@vtemp is not null)
			set @s = @s + @vtemp
		end
		if(@type='avgavg') 
		begin
			SET @strSQL = N''
			SET @strSQL = @strSQL + N'select  @av=avg(ParmValue) from tb_mp_' + @mptemp+N' ' +char(10)
			SET @strSQL = @strSQL + N'where measuredt between ''' +cast(@starttime as varchar(20)) +N''' and '''+cast(@endtime as varchar(20))+N''' and parmvalue <> 0 '+char(10)
			print @strsql
			exec sp_executesql @strsql,N'@av money output',@vtemp output

			--保留小数位
			set @numTailS=0
			SET @mainSQL = N''
			SET @mainSQL = @mainSQL + N'select top 1 @numTail=NumTail  from TB_MeasurePoint '
			SET @mainSQL = @mainSQL + N' where MPointCode = ''' +cast(@mptemp as varchar(50)) +N'''  '
			exec sp_executesql @mainSQL,N'@numTail varchar(10) output',@numTail output
			if((@numTail is not null) and (@numTail!='null') ) begin
				set @numTailS=CAST(@numTail as decimal(38,0))
			end

			SET @numTailSQL = N''
			SET @numTailSQL = @numTailSQL + N'set @vtemp=(SELECT CAST(' + cast(@vtemp as varchar(50)) +N' as decimal(38, ' + cast(@numTailS as varchar(50)) +N'))) '
			exec sp_executesql @numTailSQL,N'@vtemp float output',@vtemp output

			if(@vtemp is not null)
			set @s = @s + @vtemp
		end
		if(@type='firstsdt')
		begin
			SET @strSQL = N''
			SET @strSQL = @strSQL + N'select top 1 @av=ParmValue  from tb_mp_' + @mptemp+N' ' +char(10)
			SET @strSQL = @strSQL + N'where measuredt < ''' +cast(@starttime as varchar(20)) +N'''  order by MeasureDT desc'+char(10)
			print @strsql
			exec sp_executesql @strsql,N'@av money output',@vtemp output

			--保留小数位
			set @numTailS=0
			SET @mainSQL = N''
			SET @mainSQL = @mainSQL + N'select top 1 @numTail=NumTail  from TB_MeasurePoint '
			SET @mainSQL = @mainSQL + N' where MPointCode = ''' +cast(@mptemp as varchar(50)) +N'''  '
			exec sp_executesql @mainSQL,N'@numTail varchar(10) output',@numTail output
			if((@numTail is not null) and (@numTail!='null') ) begin
				set @numTailS=CAST(@numTail as decimal(38,0))
			end

			SET @numTailSQL = N''
			SET @numTailSQL = @numTailSQL + N'set @vtemp=(SELECT CAST(' + cast(@vtemp as varchar(50)) +N' as decimal(38, ' + cast(@numTailS as varchar(50)) +N'))) '
			exec sp_executesql @numTailSQL,N'@vtemp float output',@vtemp output

			if(@vtemp is not null)
			set @s = @s + @vtemp
		end
		if(@type='lastedt')
		begin
			SET @strSQL = N''
			SET @strSQL = @strSQL + N'select top 1 @av=ParmValue  from tb_mp_' + @mptemp+N' ' +char(10)
			SET @strSQL = @strSQL + N'where measuredt > ''' +cast(@endtime as varchar(20)) +N'''  order by MeasureDT asc'+char(10)
			print @strsql
			exec sp_executesql @strsql,N'@av money output',@vtemp output

			--保留小数位
			set @numTailS=0
			SET @mainSQL = N''
			SET @mainSQL = @mainSQL + N'select top 1 @numTail=NumTail  from TB_MeasurePoint '
			SET @mainSQL = @mainSQL + N' where MPointCode = ''' +cast(@mptemp as varchar(50)) +N'''  '
			exec sp_executesql @mainSQL,N'@numTail varchar(10) output',@numTail output
			if((@numTail is not null) and (@numTail!='null') ) begin
				set @numTailS=CAST(@numTail as decimal(38,0))
			end

			SET @numTailSQL = N''
			SET @numTailSQL = @numTailSQL + N'set @vtemp=(SELECT CAST(' + cast(@vtemp as varchar(50)) +N' as decimal(38, ' + cast(@numTailS as varchar(50)) +N'))) '
			exec sp_executesql @numTailSQL,N'@vtemp float output',@vtemp output

			if(@vtemp is not null)
			set @s = @s + @vtemp
		end

		
	

		set @i=@i+1

		end

		close curRunStatus
		DEALLOCATE curRunStatus

		--if(@s>=0)
			--print @s
			set @v = @s
			print @v
		--else
			--set @v = null

		if @v IS NULL
		begin
			if (@type='firstrun')
			begin
			if @v3 IS NULL
			begin
				--print '当前没有拿到数据,没有操作'
				insert into TB_MP_TEMP_REPORT(ParmValue,MeasureDT,FormatType,decimal,userid,mpcode) values('0',@starttime,'number',0,@userid,@mptemp)--insert into TB_MP_TEMP_RPT(ParmValue,MeasureDT) values('0',@starttime)
			end
			--else 
			--begin
				--print 'insert into TB_MP_TEMP_RPT(ParmValue,MeasureDT) values('''+cast(@s as varchar)+''','''+@starttime+''')'
				--insert into TB_MP_TEMP_REPORT(ParmValue,MeasureDT,FormatType,decimal) values(cast(@v3 as varchar),@starttime,'number',0) --insert into TB_MP_TEMP_RPT(ParmValue,MeasureDT) values(cast(@s as varchar),@starttime)
			--end
			end 
			else if (@type!='firstrun')
			begin
			--print '-'
			insert into TB_MP_TEMP_REPORT(ParmValue,MeasureDT,FormatType,decimal,userid,mpcode) values('-',@starttime,'label',0,@userid,@mptemp)
			end
		end
		else 
		begin
			if(@type='run')
			begin
				if(@v = 0)
				begin
				--print '停止'
				insert into TB_MP_TEMP_REPORT(ParmValue,MeasureDT,FormatType,decimal,userid,mpcode) values('停止',@starttime,'label',0,@userid,@mptemp)
				end
				if(@v = 1)
				begin
				--print '运行'
				insert into TB_MP_TEMP_REPORT(ParmValue,MeasureDT,FormatType,decimal,userid,mpcode) values('运行',@starttime,'label',0,@userid,@mptemp)
				end
				if(@v > 1)
				begin
				--print '故障'
				insert into TB_MP_TEMP_REPORT(ParmValue,MeasureDT,FormatType,decimal,userid,mpcode) values('故障',@starttime,'label',0,@userid,@mptemp)
				end
			end
			else if(@type='timeequal')
			begin
			--print 'insert into TB_MP_TEMP_REPORT(ParmValue,MeasureDT,FormatType) values('''+cast(@v/@count as varchar)+''','''+@starttime+''','number')'
			insert into TB_MP_TEMP_REPORT(ParmValue,MeasureDT,FormatType,decimal,userid,mpcode) values(replace(cast(@v as varchar),'.',':'),@starttime,'datetime',0,@userid,@mptemp)
			end
			else if(@type='firstavg')
			begin
			--print 'insert into TB_MP_TEMP_REPORT(ParmValue,MeasureDT,FormatType) values('''+cast(@v/@count as varchar)+''','''+@starttime+''','number')'
			insert into TB_MP_TEMP_REPORT(ParmValue,MeasureDT,FormatType,decimal,userid,mpcode) values(cast(@v/@count as varchar),@starttime,'number',@numTailS,@userid,@mptemp)
			end
			else if(@type='avgavg')
			begin
			--print 'insert into TB_MP_TEMP_REPORT(ParmValue,MeasureDT,FormatType) values('''+cast(@v/@count as varchar)+''','''+@starttime+''','number')'
			insert into TB_MP_TEMP_REPORT(ParmValue,MeasureDT,FormatType,decimal,userid,mpcode) values(cast(@v/@count as varchar),@starttime,'number',@numTailS,@userid,@mptemp)
			end
			else if (@type='firstrun')
			begin
				if @v3 is null
				begin
				--print '拿到了值,没有操作'
				insert into TB_MP_TEMP_REPORT(ParmValue,MeasureDT,FormatType,decimal,userid,mpcode) values(cast(@v as varchar),@starttime,'number',0,@userid,@mptemp) --insert into TB_MP_TEMP_RPT(ParmValue,MeasureDT) values(cast(@s as varchar),@starttime)
				end
				else if @v3 is not null
				begin
				set @s = null
				SET @strSQL = N''
				SET @strSQL = @strSQL + N'select top 1 @av=ParmValue  from [tb_mp_' + @mp+N'_hour]' +char(10)
				SET @strSQL = @strSQL + N'where measuredt >= ''' +cast(dateadd(hour,-1,@starttime) as varchar(20)) +N''' and measuredt < '''+cast(@endtime as varchar(20))+N''' order by MeasureDT asc'+char(10)
				print @strsql
				exec sp_executesql @strsql,N'@av money output',@s output
				--print '拿到了值,且有操作'
				insert into TB_MP_TEMP_REPORT(ParmValue,MeasureDT,FormatType,decimal,userid,mpcode) values(substring(cast(@s as varchar),0,2)+','+convert(varchar(20),@v3,120),@starttime,'number',0,@userid,@mptemp)
				--insert into TB_MP_TEMP_RPT(ParmValue,MeasureDT) values(substring(cast(@s as varchar),0,2)+','+convert(varchar(20),@v,120),@starttime)
				end
			end
			else if(@type='pulldown')
			begin
			print 'pulldown'
			end
			else
			begin
			--print 'insert into TB_MP_TEMP_REPORT(ParmValue,MeasureDT,FormatType) values('''+cast(@v as varchar)+''','''+@starttime+''','number')'
			insert into TB_MP_TEMP_REPORT(ParmValue,MeasureDT,FormatType,decimal,userid,mpcode) values(@v,@starttime,'number',@numTailS,@userid,@mptemp)
			end
		end

		

	end
	else
	begin
		--print '-'
		insert into TB_MP_TEMP_REPORT(ParmValue,MeasureDT,FormatType,decimal,userid,mpcode) values('-',@starttime,'label',0,@userid,@mptemp)
	end


GO

-- ----------------------------
-- Procedure structure for sp_report_getdata_test
-- ----------------------------
DROP PROCEDURE [dbo].[sp_report_getdata_test]
GO



CREATE PROCEDURE [dbo].[sp_report_getdata_test]
@tbname varchar(200),@starttime varchar(20),@endtime varchar(20),@type varchar(80),@mp varchar(200)
as
Declare @strSQL nvarchar(4000)
Declare @memotypeSQL nvarchar(4000)
Declare @uuidSQL nvarchar(4000)
declare @v decimal(18,4)
declare @s decimal(18,4)
declare @vtemp decimal(18,4)
declare @memotypetemp varchar(500)
declare @mptemp varchar(80) --测量点code
declare @count int
declare @i int
declare @v1 decimal(18,4)
declare @v2 decimal(18,4)
declare @tempdate datetime
declare @sdate datetime
declare @edate datetime
declare @tablebname varchar(500)
declare @insertSQL nvarchar(4000)

Select @tablebname=@tbname
Select @sdate=cast(@starttime as datetime)
Select @edate=cast(@endtime as datetime)

declare @memotype varchar(500)--新增
declare @memotypes varchar(500)


declare @mpid varchar(80) --测量点id,新增
declare @uuid bigint --测量点历史表的itemid,每个表唯一新增
declare @valuemeaning varchar(80) --测量点含义,新增
declare @alarmmax decimal(18,4) --报警上限,新增
declare @alarmmin decimal(18,4) --报警下限,新增
declare @numtail varchar(80) --精度,新增
select @count = count(*) from dbo.FUN_split(@mp,',') 

print @starttime
set @i = 1
set @s = 0
	if(@mp!='-')
	begin

		DECLARE curRunStatus cursor for 
		select * from dbo.FUN_split(@mp,',') 
		open curRunStatus

		while(@i<=@count)
		begin

		FETCH curRunStatus into @mptemp

		--获取备注类型
		SET @memotypeSQL = N''
			SET @memotypeSQL = @memotypeSQL + N'select top 1 @bv=memotype  from tb_mp_' + @mptemp+N' ' +char(10)
			SET @memotypeSQL = @memotypeSQL + N'where measuredt >= ''' +cast(@starttime as varchar(20)) +N''' and measuredt < '''+cast(@endtime as varchar(20))+N''' order by MeasureDT asc'+char(10)
			print @memotypeSQL
			exec sp_executesql @memotypeSQL,N'@bv varchar(500) output',@memotypetemp output
		print N'@memotypetemp-----'+ @memotypetemp
		--获取唯一id
		SET @uuidSQL = N''
			SET @uuidSQL = @uuidSQL + N'select top 1 @bv=ItemID  from tb_mp_' + @mptemp+N' ' +char(10)
			SET @uuidSQL = @uuidSQL + N'where measuredt >= ''' +cast(@starttime as varchar(20)) +N''' and measuredt < '''+cast(@endtime as varchar(20))+N''' order by MeasureDT asc'+char(10)
			print @uuidSQL
			exec sp_executesql @uuidSQL,N'@bv bigint output',@uuid output
		print N'@@uuid-----'
		print @uuid
		--获取测量点id
		begin
			SET @strSQL = N''
			SET @strSQL = @strSQL + N'select top 1 @av=ID from TB_MeasurePoint'
			SET @strSQL = @strSQL + N' where MPointCode = ''' +@mptemp +N''''
			print @strsql
			exec sp_executesql @strsql,N'@av varchar(50) output',@mpid output
			print N'@mpid-----'+ @mpid
			if @mpid is null
				begin
				set @mpid=N'-'
			end
		end
		--获取测量点含义
		begin
			SET @strSQL = N''
			SET @strSQL = @strSQL + N'select top 1 @av=valueMeaning from TB_MeasurePoint'
			SET @strSQL = @strSQL + N' where MPointCode = ''' +@mptemp +N''''
			print @strsql
			exec sp_executesql @strsql,N'@av varchar(50) output',@valuemeaning output
			print N'@@valuemeaning-----'+ @valuemeaning
			if @valuemeaning is null
				begin
				set @valuemeaning=N'-'
			end
		end
		--获取报警上限
		begin
			SET @strSQL = N''
			SET @strSQL = @strSQL + N'select top 1 @av=alarmmax from TB_MeasurePoint'
			SET @strSQL = @strSQL + N' where MPointCode = ''' +@mptemp +N''''
			print @strsql
			exec sp_executesql @strsql,N'@av decimal(18,4) output',@alarmmax output
			
			if @alarmmax is null
				begin
				print N'@@alarmmax-----'
			print @alarmmax
				set @alarmmax= 9999999999999
				print @alarmmax
			end
		end
		--获取报警下限
		begin
			SET @strSQL = N''
			SET @strSQL = @strSQL + N'select top 1 @av=alarmmin from TB_MeasurePoint'
			SET @strSQL = @strSQL + N' where MPointCode = ''' +@mptemp +N''''
			print @strsql
			exec sp_executesql @strsql,N'@av decimal(18,4) output',@alarmmin output
			--print N'@@alarmmin-----'+ @alarmmin
			if @alarmmin is null
				begin
				set @alarmmin= -99999999999999
			end
		end
		--获取精度
		begin
			SET @strSQL = N''
			SET @strSQL = @strSQL + N'select top 1 @av=NumTail from TB_MeasurePoint'
			SET @strSQL = @strSQL + N' where MPointCode = ''' +@mptemp +N''''
			print @strsql
			exec sp_executesql @strsql,N'@av varchar(50) output',@numtail output
			print N'@@numtail-----'+ @numtail
			if @numtail is null
				begin
				set @numtail=N'-'
			end
		end
		if(@type='equal') 
		begin
			SET @strSQL = N''
			SET @strSQL = @strSQL + N'select top 1 @av=ParmValue  from tb_mp_' + @mptemp+N' ' +char(10)
			SET @strSQL = @strSQL + N'where measuredt = ''' +cast(@starttime as varchar(20)) +N'''  order by MeasureDT asc'+char(10)
			print @strsql
			exec sp_executesql @strsql,N'@av decimal(18,4) output',@vtemp output

			if(@vtemp is not null)
			set @s = @s + @vtemp
		end
		if(@type='timeequal')
		begin
			SET @strSQL = N''
			SET @strSQL = @strSQL + N'select top 1 @av=ParmValue  from tb_mp_' + @mptemp+N' ' +char(10)
			SET @strSQL = @strSQL + N'where measuredt = ''' +cast(@starttime as varchar(20)) +N'''  order by MeasureDT asc'+char(10)
			print @strsql
			exec sp_executesql @strsql,N'@av decimal(18,4) output',@vtemp output

			if(@vtemp is not null)
			set @s = @s + @vtemp
		end
		if(@type='first') 
		begin
			SET @strSQL = N''
			SET @strSQL = @strSQL + N'select top 1 @av=ParmValue  from tb_mp_' + @mptemp+N' ' +char(10)
			SET @strSQL = @strSQL + N'where measuredt >= ''' +cast(@starttime as varchar(20)) +N''' and measuredt < '''+cast(@endtime as varchar(20))+N''' order by MeasureDT asc'+char(10)
			print @strsql

			exec sp_executesql @strsql,N'@av decimal(18,4) output',@vtemp output
			print cast(@starttime as varchar(20))
			print @vtemp
			if(@vtemp is not null)
			set @s = @s + @vtemp
				--获取备注类型
			SET @memotypeSQL = N''
			SET @memotypeSQL = @memotypeSQL + N'select top 1 @av=memotype from tb_mp_' + @mptemp+N' ' +char(10)
			SET @memotypeSQL = @memotypeSQL + N'where measuredt >= ''' +cast(@starttime as varchar(20)) +N''' and measuredt < '''+cast(@endtime as varchar(20))+N''' order by MeasureDT asc'+char(10)
			print N'@@@@memotypeSQL-----'
			print @memotypeSQL
			exec sp_executesql @memotypeSQL,N'@av varchar(500) output',@memotypetemp output
			print N'@memotypetemp-----'+ @memotypetemp
			--获取唯一id
			SET @uuidSQL = N''
			SET @uuidSQL = @uuidSQL + N'select top 1 @cv=ItemID from tb_mp_' + @mptemp+N' ' +char(10)
			SET @uuidSQL = @uuidSQL + N'where measuredt >= ''' +cast(@starttime as varchar(20)) +N''' and measuredt < '''+cast(@endtime as varchar(20))+N''' order by MeasureDT asc'+char(10)
			print N'@@@uuidSQL-----'
			print @uuidSQL
			exec sp_executesql @uuidSQL,N'@cv bigint output',@uuid output
			print N'@@uuid-----'
			print @uuid
		end
		if(@type='last') 
		begin
			SET @strSQL = N''
			SET @strSQL = @strSQL + N'select top 1 @av=ParmValue from tb_mp_' + @mptemp+N' ' +char(10)
			SET @strSQL = @strSQL + N'where measuredt > ''' +cast(@starttime as varchar(20)) +N''' and measuredt <= '''+cast(@endtime as varchar(20))+N''' order by MeasureDT desc'+char(10)
			print @strsql
			exec sp_executesql @strsql,N'@av decimal(18,4) output',@vtemp output

			if(@vtemp is not null)
			set @s = @s + @vtemp
			--获取备注类型
			SET @memotypeSQL = N''
			SET @memotypeSQL = @memotypeSQL + N'select top 1 @av=memotype from tb_mp_' + @mptemp+N' ' +char(10)
			SET @memotypeSQL = @memotypeSQL + N'where measuredt > ''' +cast(@starttime as varchar(20)) +N''' and measuredt <= '''+cast(@endtime as varchar(20))+N''' order by MeasureDT desc'+char(10)
			print N'@@@@memotypeSQL-----'
			print @memotypeSQL
			exec sp_executesql @memotypeSQL,N'@av varchar(500) output',@memotypetemp output
			print N'@memotypetemp-----'+ @memotypetemp
			--获取唯一id
			SET @uuidSQL = N''
			SET @uuidSQL = @uuidSQL + N'select top 1 @cv=ItemID from tb_mp_' + @mptemp+N' ' +char(10)
			SET @uuidSQL = @uuidSQL + N'where measuredt > ''' +cast(@starttime as varchar(20)) +N''' and measuredt <= '''+cast(@endtime as varchar(20))+N''' order by MeasureDT desc'+char(10)
			print N'@@@uuidSQL-----'
			print @uuidSQL
			exec sp_executesql @uuidSQL,N'@cv bigint output',@uuid output
			print N'@@uuid-----'
			print @uuid
		end

		if(@type='avg') 
		begin
			SET @strSQL = N''
			SET @strSQL = @strSQL + N'select  @av=avg(ParmValue) from tb_mp_' + @mptemp+N' ' +char(10)
			SET @strSQL = @strSQL + N'where measuredt >= ''' +cast(@starttime as varchar(20)) +N''' and measuredt <'''+cast(@endtime as varchar(20))+N''' and parmvalue <> 0 '+char(10)
			print @strsql
			exec sp_executesql @strsql,N'@av decimal(18,4) output',@vtemp output

			if(@vtemp is not null)
				begin
					set @s = @s + @vtemp
					print '@vtemp is not null'
					print @s
				end
			else
				begin
					set @s = @s
					print '@vtemp is  null'
					print @s
				end
			end
		--end
		if(@type='runtimerow') --应用于两列分开显示设备开机时间和关机时间
		begin
			SET @strSQL = N''
			SET @strSQL = @strSQL + N'select top 1 @av=ParmValue  from tb_mp_' + @mptemp+N' ' +char(10)
			SET @strSQL = @strSQL + N'where measuredt >= ''' +cast(dateadd(MINUTE,-30,@starttime) as varchar(20)) +N''' and measuredt < '''+cast(@starttime as varchar(20))+N''' order by MeasureDT desc'+char(10)
			print @strsql
			exec sp_executesql @strsql,N'@av money output',@v1 output
		
			SET @strSQL = N''
			SET @strSQL = @strSQL + N'select top 1 @av=ParmValue  from tb_mp_' + @mptemp+N' ' +char(10)
			SET @strSQL = @strSQL + N'where measuredt >= ''' +cast(@starttime as varchar(20)) +N''' and measuredt < '''+cast(@endtime as varchar(20))+N''' order by MeasureDT asc'+char(10)
			print @strsql
			exec sp_executesql @strsql,N'@av money output',@v2 output
			

			if (@v2 is not null)
			begin
			if(@v2=1)
			begin
				SET @strSQL = N''
				SET @strSQL = @strSQL+N'declare @ParmValue money'+char(10)
				SET @strSQL = @strSQL+N'declare @MeasureDT datetime'+char(10)
				SET @strSQL = @strSQL+N'declare @ItemID bigint'+char(10)
				SET @strSQL = @strSQL+N'declare @memotype varchar(500)'+char(10)
				SET @strSQL = @strSQL+N'declare @lastParmValue money'+char(10)
				SET @strSQL = @strSQL+N'declare @lastMeasureDT datetime'+char(10)
				SET @strSQL = @strSQL+N'declare @idx int'+char(10)
				SET @strSQL = @strSQL+N'set @idx = 0' +char(10)	

				if(@v1=1)
				begin
				SET @strSQL = @strSQL + N'set @lastParmValue = 1 '+char(10)	
				SET @strSQL = @strSQL + N'set @lastMeasureDT = '''+@starttime+N'''' +char(10)								
				--SET @strSQL = @strSQL + N'insert into TB_MP_TEMP_REPORT(ParmValue,MeasureDT,FormatType) values('''+CONVERT(varchar(100),@starttime,24)+N''','''+CONVERT(varchar(100),@starttime,120)+N''',''label'')'+char(10)
				SET @strSQL  = @strSQL + N' insert into '+@tablebname+'(UUID,mpid,ValueMeaning,ParmValue,alarmmax,alarmmin,numtail,MeasureDT,memotype,FormatType) values(cast(@ItemID as varchar),'''+@mpid+''','''+@valuemeaning+''', '''+CONVERT(varchar(100),@startTime,120)+''', '''+cast(@alarmmax as varchar)+''', '''+cast(@alarmmin as varchar)+''','''+@numtail+''', '''+CONVERT(varchar(16),@startTime, 120 )+''',@memotype,''label'')'
				SET @strSQL = @strSQL + N'set @idx = @idx+1'+char(10)	
				end
				else
				begin
				SET @strSQL = @strSQL + N'set @lastParmValue = 0 '+char(10)	
				SET @strSQL = @strSQL + N'set @lastMeasureDT = '''+@starttime+N'''' +char(10)								
				end

				SET @strSQL = @strSQL + N'DECLARE curRun1 cursor for select ParmValue,MeasureDT,ItemID,memotype from tb_mp_' + @mptemp+N' ' +char(10)
				SET @strSQL = @strSQL + N'where measuredt >= ''' +cast(@starttime as varchar(20)) +N''' and measuredt < '''+cast(@endtime as varchar(20))+N''' order by MeasureDT asc'+char(10)
				SET @strSQL = @strSQL + N'open curRun1'+char(10)
				SET @strSQL = @strSQL + N'FETCH curRun1 into @ParmValue,@MeasureDT,@ItemID,@memotype'+char(10)
				SET @strSQL = @strSQL + N'while (@@fetch_status=0)'+char(10)
				SET @strSQL = @strSQL + N'begin'+char(10)
				SET @strSQL = @strSQL + N'if(@ParmValue<>@lastParmValue)'+char(10)
				SET @strSQL = @strSQL + N'begin'+char(10)

				--SET @strSQL  = @strSQL + N'insert into TB_MP_TEMP_REPORT(ParmValue,MeasureDT,FormatType) values(CONVERT(varchar(100),@MeasureDT,120),CONVERT(varchar(100),@MeasureDT,120),''label'')'+char(10)
				SET @strSQL  = @strSQL + N' insert into '+@tablebname+'(UUID,mpid,ValueMeaning,ParmValue,alarmmax,alarmmin,numtail,MeasureDT,memotype,FormatType) values(cast(@ItemID as varchar),'''+@mpid+''','''+@valuemeaning+''',CONVERT(varchar(100),@MeasureDT,120), '''+cast(@alarmmax as varchar)+''', '''+cast(@alarmmin as varchar)+''','''+@numtail+''', '''+CONVERT(varchar(16),@startTime, 120 )+''',@memotype,''label'')'

				SET @strSQL = @strSQL + N'set @lastParmValue = @ParmValue'+char(10)	
				SET @strSQL = @strSQL + N'set @lastMeasureDT = @MeasureDT'+char(10)	
				SET @strSQL = @strSQL + N'set @idx = @idx+1'+char(10)	
				SET @strSQL  = @strSQL + N'end'+char(10)
				SET @strSQL  = @strSQL + N'FETCH curRun1 into @ParmValue,@MeasureDT,@ItemID,@memotype'+char(10)	
				SET @strSQL  = @strSQL + N'end'+char(10)
				SET @strSQL = @strSQL + N'if(@idx%2=1)'+char(10)
				SET @strSQL = @strSQL + N'begin'+char(10)
				--if @memotypetemp IS NULL
				--begin
				--set @memotypetemp=N'-'
				--end
				--else
				--begin
				--set	@memotypetemp = @memotypetemp
				--end

				SET @strSQL  = @strSQL + N' insert into '+@tablebname+'(UUID,mpid,ValueMeaning,ParmValue,alarmmax,alarmmin,numtail,MeasureDT,memotype,FormatType) values(cast(@ItemID as varchar),'''+@mpid+''','''+@valuemeaning+''',CONVERT(varchar(100),@MeasureDT,120), '''+cast(@alarmmax as varchar)+''', '''+cast(@alarmmin as varchar)+''','''+@numtail+''', '''+CONVERT(varchar(16),@startTime, 120 )+''',@memotype,''label'')'

				--SET @strSQL  = @strSQL + N'insert into TB_MP_TEMP_REPORT(ParmValue,MeasureDT,FormatType) values('''+CONVERT(varchar(100),@endtime,24)+N''','''+CONVERT(varchar(100),@endtime,120)+N''',''label'')'+char(10)
				SET @strSQL  = @strSQL + N'end'+char(10)
				SET @strSQL  = @strSQL + N'close curRun1'+char(10)
				SET @strSQL  = @strSQL + N'DEALLOCATE curRun1'+char(10)
				print @strSQL
				exec (@strSQL)
			
			end
			
			if(@v2=0)
			begin
				SET @strSQL = N''
				SET @strSQL = @strSQL+N'declare @ParmValue money'+char(10)
				SET @strSQL = @strSQL+N'declare @MeasureDT datetime'+char(10)
				SET @strSQL = @strSQL+N'declare @ItemID bigint'+char(10)
				SET @strSQL = @strSQL+N'declare @memotype varchar(500)'+char(10)
				SET @strSQL = @strSQL+N'declare @lastParmValue money'+char(10)
				SET @strSQL = @strSQL+N'declare @lastMeasureDT datetime'+char(10)
				SET @strSQL = @strSQL+N'declare @idx int'+char(10)
				SET @strSQL = @strSQL+N'set @idx = 0' +char(10)
				SET @strSQL = @strSQL + N'set @lastParmValue = ' +cast(@v2 as varchar(20))+N' '+char(10)	
				SET @strSQL = @strSQL + N'set @lastMeasureDT = '''+@starttime+N'''' +char(10)	
				SET @strSQL = @strSQL + N'DECLARE curRun1 cursor for select ParmValue,MeasureDT,ItemID,memotype from tb_mp_' + @mptemp+N' ' +char(10)
				SET @strSQL = @strSQL + N'where measuredt >= ''' +cast(@starttime as varchar(20)) +N''' and measuredt < '''+cast(@endtime as varchar(20))+N''' order by MeasureDT asc'+char(10)
				SET @strSQL = @strSQL + N'open curRun1'+char(10)
				SET @strSQL = @strSQL + N'FETCH curRun1 into @ParmValue,@MeasureDT,@ItemID,@memotype'+char(10)
				SET @strSQL = @strSQL + N'while (@@fetch_status=0)'+char(10)
				SET @strSQL = @strSQL + N'begin'+char(10)
				SET @strSQL = @strSQL + N'if(@ParmValue<>@lastParmValue)'+char(10)
				SET @strSQL = @strSQL + N'begin'+char(10)
				--SET @strSQL  = @strSQL + N'insert into TB_MP_TEMP_REPORT(ParmValue,MeasureDT,FormatType) values(CONVERT(varchar(100),@MeasureDT,120),CONVERT(varchar(100),@MeasureDT,120),''label'')'+char(10)
				--if @memotypetemp IS NULL
				--begin
				--set @memotypetemp=N'-'
				--end
				--else
				--begin
				--set	@memotypetemp = @memotypetemp
				--end
				SET @strSQL  = @strSQL + N' insert into '+@tablebname+'(UUID,mpid,ValueMeaning,ParmValue,alarmmax,alarmmin,numtail,MeasureDT,memotype,FormatType) values(cast(@ItemID as varchar),'''+@mpid+''','''+@valuemeaning+''',CONVERT(varchar(100),@MeasureDT,120), '''+cast(@alarmmax as varchar)+''', '''+cast(@alarmmin as varchar)+''','''+@numtail+''', '''+CONVERT(varchar(16),@startTime, 120 )+''',@memotype,''label'')'

				SET @strSQL = @strSQL + N'set @lastParmValue = @ParmValue'+char(10)	
				SET @strSQL = @strSQL + N'set @lastMeasureDT = @MeasureDT'+char(10)	
				SET @strSQL = @strSQL + N'set @idx = @idx+1'+char(10)	
				SET @strSQL  = @strSQL + N'end'+char(10)
				SET @strSQL  = @strSQL + N'FETCH curRun1 into @ParmValue,@MeasureDT,@ItemID,@memotype'+char(10)	
				SET @strSQL  = @strSQL + N'end'+char(10)
				SET @strSQL = @strSQL + N'if(@idx%2=1)'+char(10)
				SET @strSQL = @strSQL + N'begin'+char(10)
				--SET @strSQL  = @strSQL + N'insert into TB_MP_TEMP_REPORT(ParmValue,MeasureDT,FormatType) values('''+CONVERT(varchar(100),@endtime,24)+N''','''+CONVERT(varchar(100),@endtime,120)+N''',''label'')'+char(10)
				--if @memotypetemp IS NULL
				--begin
				--set @memotypetemp=N'-'
				--end
				--else
				--begin
				--set	@memotypetemp = @memotypetemp
				--end
				SET @strSQL  = @strSQL + N' insert into '+@tablebname+'(UUID,mpid,ValueMeaning,ParmValue,alarmmax,alarmmin,numtail,MeasureDT,memotype,FormatType) values(cast(@ItemID as varchar),'''+@mpid+''','''+@valuemeaning+''', CONVERT(varchar(100),@MeasureDT,120), '''+cast(@alarmmax as varchar)+''', '''+cast(@alarmmin as varchar)+''','''+@numtail+''', '''+CONVERT(varchar(16),@startTime, 120 )+''',@memotype,''label'')'

				SET @strSQL  = @strSQL + N'end'+char(10)
				SET @strSQL  = @strSQL + N'close curRun1'+char(10)
				SET @strSQL  = @strSQL + N'DEALLOCATE curRun1'+char(10)
				print @strSQL
				exec (@strSQL)
			
			end
						
			end
			set @vtemp=0
		end
		if(@type='min') 
		begin
			SET @strSQL = N''
			SET @strSQL = @strSQL + N'select  @av=min(ParmValue) from tb_mp_' + @mptemp+N' ' +char(10)
			SET @strSQL = @strSQL + N'where measuredt >= ''' +cast(@starttime as varchar(20)) +N''' and measuredt <'''+cast(@endtime as varchar(20))+N''''+char(10)
			print @strsql
			exec sp_executesql @strsql,N'@av decimal(18,4) output',@vtemp output

			if(@vtemp is not null)
			set @s = @s + @vtemp
		end
		if(@type='max') 
		begin
			SET @strSQL = N''
			SET @strSQL = @strSQL + N'select  @av=max(ParmValue) from tb_mp_' + @mptemp+N' ' +char(10)
			SET @strSQL = @strSQL + N'where measuredt >= ''' +cast(@starttime as varchar(20)) +N''' and measuredt <'''+cast(@endtime as varchar(20))+N''''+char(10)
			print @strsql
			exec sp_executesql @strsql,N'@av decimal(18,4) output',@vtemp output

			if(@vtemp is not null)
			set @s = @s + @vtemp
		end
		if(@type='diff') 
		begin
			SET @strSQL = N''
			SET @strSQL = @strSQL + N'select top 1 @av=parmvalue from tb_mp_' + @mptemp+N' ' +char(10)
			SET @strSQL = @strSQL + N'where measuredt >= ''' +cast(@starttime as varchar(20)) +N''' and measuredt < '''+cast(@endtime as varchar(20))+N''' order by measuredt asc'+char(10)
			print @strsql
			exec sp_executesql @strsql,N'@av decimal(18,4) output',@v1 output
			print cast(@v1 as varchar(20))

			SET @strSQL = N''
			SET @strSQL = @strSQL + N'select top 1 @av=parmvalue from tb_mp_' + @mptemp+N' ' +char(10)
			SET @strSQL = @strSQL + N'where measuredt >= ''' +cast(dateadd(day,1,@starttime) as varchar(20)) +N''' and measuredt < '''+cast(dateadd(day,1,@endtime) as varchar(20))+N''' order by measuredt asc'+char(10)
			print @strsql
			exec sp_executesql @strsql,N'@av decimal(18,4) output',@v2 output
			print cast(@v2 as varchar(20))
			
			set @vtemp = @v2 - @v1
			if(@vtemp is not null)
			begin
				if(@vtemp<0)
				begin
					set @s = @s
				end
				else
				begin
					set @s = @s + @vtemp
				end		
			end
			
		end		
		if(@type='diffx') 
		begin
			SET @strSQL = N''
			SET @strSQL = @strSQL + N'select top 1 @av=parmvalue from tb_mp_' + @mptemp+N' ' +char(10)
			SET @strSQL = @strSQL + N'where measuredt >= ''' +cast(@starttime as varchar(20)) +N''' and measuredt < '''+cast(dateadd(day,1,@starttime)  as varchar(20))+N''' order by measuredt asc'+char(10)
			print @strsql
			exec sp_executesql @strsql,N'@av decimal(18,4) output',@v1 output
			print cast(@v1 as varchar(20))

			SET @strSQL = N''
			SET @strSQL = @strSQL + N'select top 1 @av=parmvalue from tb_mp_' + @mptemp+N' ' +char(10)
			SET @strSQL = @strSQL + N'where measuredt >= ''' +cast(@endtime as varchar(20)) +N''' and measuredt < '''+cast(dateadd(day,1,@endtime) as varchar(20))+N''' order by measuredt asc'+char(10)
			print @strsql
			exec sp_executesql @strsql,N'@av decimal(18,4) output',@v2 output
			print cast(@v2 as varchar(20))
			
			set @vtemp = @v2 - @v1
			if(@vtemp is not null)
			begin
				if(@vtemp<0)
				begin
					set @s = @s
				end
				else
				begin
					set @s = @s + @vtemp
				end		
			end
			
		end	
		if(@type='sum') 
		begin
			SET @strSQL = N''
			SET @strSQL = @strSQL + N'select  @av=sum(ParmValue) from tb_mp_' + @mptemp+N' ' +char(10)
			SET @strSQL = @strSQL + N'where measuredt >= ''' +cast(@starttime as varchar(20)) +N''' and measuredt < '''+cast(@endtime as varchar(20))+N''''+char(10)
			print @strsql
			exec sp_executesql @strsql,N'@av decimal(18,4) output',@vtemp output
			print @vtemp
			if(@vtemp is not null)
			set @s = @s + @vtemp


			

		end
		if(@type='time')
		begin
			SET @strSQL = N''
			SET @strSQL = @strSQL + N'select top 1 @av=parmvalue from tb_mp_' + @mp+N' ' +char(10)
			SET @strSQL = @strSQL + N'where measuredt >= ''' +cast(@starttime as varchar(20)) +N''' and measuredt < '''+cast(@endtime as varchar(20))+N''' order by measuredt asc'+char(10)
			print @strsql
			exec sp_executesql @strsql,N'@av decimal(18,4) output',@v1 output
			print cast(@v1 as varchar(20))

			SET @strSQL = N''
			SET @strSQL = @strSQL + N'select top 1 @av=parmvalue from tb_mp_' + @mp+N' ' +char(10)
			SET @strSQL = @strSQL + N'where measuredt >= ''' +cast(dateadd(day,1,@starttime) as varchar(20)) +N''' and measuredt < '''+cast(dateadd(day,1,@endtime) as varchar(20))+N''' order by measuredt asc'+char(10)
			print @strsql
			exec sp_executesql @strsql,N'@av decimal(18,4) output',@v2 output
			print cast(@v2 as varchar(20))
			
			set @vtemp = @v2*60 - @v1*60
			if(@vtemp is not null)
			begin
				if(@vtemp<0)
				begin
					set @s = @s
				end
				else if((@v2*60 - @v1*60) >= 1440 )
				begin
					set @s = @s+24
				end
				else
				begin
					set @s = @s+(@v2*60 - @v1*60)/60.0
				end			
			end
			else
				set @s = @s
		end
		if(@type='timem') 
		begin
			SET @strSQL = N''
			SET @strSQL = @strSQL + N'select top 1 @av=parmvalue from tb_mp_' + @mptemp+N' ' +char(10)
			SET @strSQL = @strSQL + N'where measuredt >= ''' +cast(@starttime as varchar(20)) +N''' and measuredt < '''+cast(@endtime as varchar(20))+N''' order by measuredt asc'+char(10)
			print @strsql
			exec sp_executesql @strsql,N'@av decimal(18,4) output',@v1 output
			print cast(@v1 as varchar(20))

			SET @strSQL = N''
			SET @strSQL = @strSQL + N'select top 1 @av=parmvalue from tb_mp_' + @mptemp+N' ' +char(10)
			SET @strSQL = @strSQL + N'where measuredt >= ''' +cast(dateadd(day,1,@starttime) as varchar(20)) +N''' and measuredt < '''+cast(dateadd(day,1,@endtime) as varchar(20))+N''' order by measuredt asc'+char(10)
			print @strsql
			exec sp_executesql @strsql,N'@av decimal(18,4) output',@v2 output
			print cast(@v2 as varchar(20))
			
			set @vtemp = @v2 - @v1
			if(@vtemp is not null)
			begin
				if(@vtemp<0)
				begin
					set @s = @s
				end
				else
				begin
					set @s = @s + @vtemp/60.0
				end		
			end
			
		end		
		if(@type='run') 
		begin
			SET @strSQL = N''
			SET @strSQL = @strSQL + N'select top 1 @av=ParmValue  from tb_mp_' + @mptemp+N' ' +char(10)
			SET @strSQL = @strSQL + N'where measuredt >= ''' +cast(@starttime as varchar(20)) +N''' and measuredt < '''+cast(@endtime as varchar(20))+N''' order by MeasureDT asc'+char(10)
			print @strsql
			exec sp_executesql @strsql,N'@av decimal(18,4) output',@vtemp output

			if(@vtemp is not null)
			set @s = @s + @vtemp
		end
		if(@type='mthavg') --只适用于月报平均
		begin
			while(@sdate<@edate)
			begin
			set @tempdate = dateadd(hour,2,@sdate)

			SET @strSQL = N''
			SET @strSQL = @strSQL + N'select top 1 @av=ParmValue  from tb_mp_' + @mptemp+N' ' +char(10)
			SET @strSQL = @strSQL + N'where measuredt >= ''' +cast(@sdate as varchar(20)) +N''' and measuredt < '''+cast(@edate as varchar(20))+N''' order by MeasureDT asc'+char(10)
			print @strsql
			exec sp_executesql @strsql,N'@av decimal(18,4) output',@vtemp output

			if(@vtemp is not null)
			set @s = @s + @vtemp

			set @sdate = @tempdate
			end

			set @s = @s/12
		end
		if(@type='firstavg') 
		begin
			SET @strSQL = N''
			SET @strSQL = @strSQL + N'select top 1 @av=ParmValue  from tb_mp_' + @mptemp+N' ' +char(10)
			SET @strSQL = @strSQL + N'where measuredt >= ''' +cast(@starttime as varchar(20)) +N''' and measuredt < '''+cast(@endtime as varchar(20))+N''' order by MeasureDT asc'+char(10)
			print @strsql
			exec sp_executesql @strsql,N'@av decimal(18,4) output',@vtemp output

			if(@vtemp is not null)
			set @s = @s + @vtemp
		end
		if(@type='avgavg') 
		begin
			SET @strSQL = N''
			SET @strSQL = @strSQL + N'select  @av=avg(ParmValue) from tb_mp_' + @mptemp+N' ' +char(10)
			SET @strSQL = @strSQL + N'where measuredt between ''' +cast(@starttime as varchar(20)) +N''' and '''+cast(@endtime as varchar(20))+N''' and parmvalue <> 0 '+char(10)
			print @strsql
			exec sp_executesql @strsql,N'@av decimal(18,4) output',@vtemp output

			if(@vtemp is not null)
			set @s = @s + @vtemp
		end
		if(@type='firstsdt')
		begin
			SET @strSQL = N''
			SET @strSQL = @strSQL + N'select top 1 @av=ParmValue  from tb_mp_' + @mptemp+N' ' +char(10)
			SET @strSQL = @strSQL + N'where measuredt < ''' +cast(@starttime as varchar(20)) +N'''  order by MeasureDT desc'+char(10)
			print @strsql
			exec sp_executesql @strsql,N'@av decimal(18,4) output',@vtemp output

			if(@vtemp is not null)
			set @s = @s + @vtemp
		end
		if(@type='lastedt')
		begin
			SET @strSQL = N''
			SET @strSQL = @strSQL + N'select top 1 @av=ParmValue  from tb_mp_' + @mptemp+N' ' +char(10)
			SET @strSQL = @strSQL + N'where measuredt > ''' +cast(@endtime as varchar(20)) +N'''  order by MeasureDT asc'+char(10)
			print @strsql
			exec sp_executesql @strsql,N'@av decimal(18,4) output',@vtemp output

			if(@vtemp is not null)
			set @s = @s + @vtemp
		end
		
		

		set @i=@i+1

		end

		close curRunStatus
		DEALLOCATE curRunStatus

		
		set @memotype = @memotypes
		if(@s>=0)
			set @v = @s
		else
			set @v = @s

		if @vtemp IS NULL
		--if (@v=0)
		begin
			
			--print '-'
			--set @insertSQL = N' insert into '+@tablebname+'(UUID,mpid,ValueMeaning,ParmValue,MeasureDT,memotype,FormatType) values('+cast(@uuid as varchar)+','+@mpid+''',''-'', ''-'', '''+CONVERT(varchar(16),@startTime, 120 )+''',''-'',''label'')'
			set @insertSQL = N' insert into '+@tablebname+'(UUID,mpid,ValueMeaning,ParmValue,alarmmax,alarmmin,numtail,MeasureDT,memotype,FormatType) values(''-'','''+@mpid+''',''-'', ''-'',''-'',''-'',''-'', '''+CONVERT(varchar(16),@startTime, 120 )+''',''-'',''label'')'
			print N'@insertSQL: '+@insertSQL
			print 'v----------'
			print @v
			EXEC sp_executesql @insertSQL
			--insert into TB_MP_TEMP_REPORT(ParmValue,MeasureDT,FormatType) values('-',@starttime,'label')
		end
		

		else

		 
		
		
		begin
			if(@type='run')
			begin
				if(@v = 0)
				begin
					if @memotypetemp IS NULL
					begin
					set @memotypetemp=N'-'
					
					set @insertSQL = N' insert into '+@tablebname+'(UUID,mpid,ValueMeaning,ParmValue,alarmmax,alarmmin,numtail,MeasureDT,memotype,FormatType) values('+cast(@uuid as varchar)+','+@mpid+''','''+@valuemeaning+''', ''停止'', '''+cast(@alarmmax as varchar)+''', '''+cast(@alarmmin as varchar)+''','''+@numtail+''', '''+CONVERT(varchar(16),@startTime, 120 )+''','''+@memotypetemp+''',''label'')'
					print N'@insertSQL: '+@insertSQL
					EXEC sp_executesql @insertSQL
					end
					else
					begin
				--print '停止'
					set @insertSQL = N' insert into '+@tablebname+'(UUID,mpid,ValueMeaning,ParmValue,alarmmax,alarmmin,numtail,MeasureDT,memotype,FormatType) values('+cast(@uuid as varchar)+','+@mpid+''','''+@valuemeaning+''', ''停止'', '''+cast(@alarmmax as varchar)+''', '''+cast(@alarmmin as varchar)+''','''+@numtail+''', '''+CONVERT(varchar(16),@startTime, 120 )+''','''+@memotypetemp+''',''label'')'
					print N'@insertSQL: '+@insertSQL
					EXEC sp_executesql @insertSQL
				--insert into TB_MP_TEMP_REPORT(ParmValue,MeasureDT,FormatType) values('停止',@starttime,'label')
					end
				end
				if(@v = 1)
				begin
					if @memotypetemp IS NULL
					begin
					set @memotypetemp=N'-'

					set @insertSQL = N' insert into '+@tablebname+'(UUID,mpid,ValueMeaning,ParmValue,alarmmax,alarmmin,numtail,MeasureDT,memotype,FormatType) values('+cast(@uuid as varchar)+','+@mpid+''','''+@valuemeaning+''', ''运行'', '''+cast(@alarmmax as varchar)+''', '''+cast(@alarmmin as varchar)+''','''+@numtail+''', '''+CONVERT(varchar(16),@startTime, 120 )+''','''+@memotypetemp+''',''label'')'
					print N'@insertSQL: '+@insertSQL
					EXEC sp_executesql @insertSQL
					end
					else
					begin
				--print '运行'
					set @insertSQL = N' insert into '+@tablebname+'(UUID,mpid,ValueMeaning,ParmValue,alarmmax,alarmmin,numtail,MeasureDT,memotype,FormatType) values('+cast(@uuid as varchar)+','+@mpid+''','''+@valuemeaning+''', ''运行'', '''+cast(@alarmmax as varchar)+''', '''+cast(@alarmmin as varchar)+''','''+@numtail+''', '''+CONVERT(varchar(16),@startTime, 120 )+''','''+@memotypetemp+''',''label'')'
					print N'@insertSQL: '+@insertSQL
					EXEC sp_executesql @insertSQL
				--insert into TB_MP_TEMP_REPORT(ParmValue,MeasureDT,FormatType) values('运行',@starttime,'label')
					end
				end
				if(@v > 1)
				begin
					if @memotypetemp IS NULL
					begin
					set @memotypetemp=N'-'

					set @insertSQL = N' insert into '+@tablebname+'(UUID,mpid,ValueMeaning,ParmValue,alarmmax,alarmmin,numtail,MeasureDT,memotype,FormatType) values('+cast(@uuid as varchar)+','+@mpid+''','''+@valuemeaning+''', ''故障'', '''+cast(@alarmmax as varchar)+''', '''+cast(@alarmmin as varchar)+''','''+@numtail+''', '''+CONVERT(varchar(16),@startTime, 120 )+''','''+@memotypetemp+''',''label'')'
					print N'@insertSQL: '+@insertSQL
					EXEC sp_executesql @insertSQL
					end
					else
					begin
				--print '故障'
					set @insertSQL = N' insert into '+@tablebname+'(UUID,mpid,ValueMeaning,ParmValue,alarmmax,alarmmin,numtail,MeasureDT,memotype,FormatType) values('+cast(@uuid as varchar)+','+@mpid+''','''+@valuemeaning+''', ''故障'', '''+cast(@alarmmax as varchar)+''', '''+cast(@alarmmin as varchar)+''','''+@numtail+''', '''+CONVERT(varchar(16),@startTime, 120 )+''','''+@memotypetemp+''',''label'')'
					print N'@insertSQL: '+@insertSQL
					EXEC sp_executesql @insertSQL
				--insert into TB_MP_TEMP_REPORT(ParmValue,MeasureDT,FormatType) values('故障',@starttime,'label')
					end
				end
			end
			else if(@type='timeequal')
			begin
				if @memotypetemp IS NULL
				begin
				set @memotypetemp=N'-'

				set @insertSQL = N' insert into '+@tablebname+'(UUID,mpid,ValueMeaning,ParmValue,alarmmax,alarmmin,numtail,MeasureDT,memotype,FormatType) values('+cast(@uuid as varchar)+','+@mpid+''','''+@valuemeaning+''', '''+replace(cast(@v as varchar),'.',':')+''', '''+cast(@alarmmax as varchar)+''', '''+cast(@alarmmin as varchar)+''','''+@numtail+''', '''+CONVERT(varchar(16),@startTime, 120 )+''','''+@memotypetemp+''',''datetime'')'
				print N'@insertSQL: '+@insertSQL
				EXEC sp_executesql @insertSQL
				end
				else
				begin
			--print 'insert into TB_MP_TEMP_REPORT(ParmValue,MeasureDT,FormatType) values('''+cast(@v/@count as varchar)+''','''+@starttime+''','number')'
				set @insertSQL = N' insert into '+@tablebname+'(UUID,mpid,ValueMeaning,ParmValue,alarmmax,alarmmin,numtail,MeasureDT,memotype,FormatType) values('+cast(@uuid as varchar)+','+@mpid+','''+@valuemeaning+''', '''+replace(cast(@v as varchar),'.',':')+''', '''+cast(@alarmmax as varchar)+''', '''+cast(@alarmmin as varchar)+''','''+@numtail+''', '''+CONVERT(varchar(16),@startTime, 120 )+''','''+@memotypetemp+''',''datetime'')'
				print N'@insertSQL: '+@insertSQL
				EXEC sp_executesql @insertSQL
			--insert into TB_MP_TEMP_REPORT(ParmValue,MeasureDT,FormatType) values(replace(cast(@v as varchar),'.',':'),@starttime,'datetime')
				end
			end
			else if(@type='firstavg')
			begin
				if @memotypetemp IS NULL
				begin
				set @memotypetemp=N'-'

				set @insertSQL = N' insert into '+@tablebname+'(UUID,mpid,ValueMeaning,ParmValue,alarmmax,alarmmin,numtail,MeasureDT,memotype,FormatType) values('+cast(@uuid as varchar)+','+@mpid+''','''+@valuemeaning+''', '''+cast(@v/@count as varchar)+''', '''+cast(@alarmmax as varchar)+''', '''+cast(@alarmmin as varchar)+''','''+@numtail+''', '''+CONVERT(varchar(16),@startTime, 120 )+''','''+@memotypetemp+''',''number'')'
				print N'@insertSQL: '+@insertSQL
				EXEC sp_executesql @insertSQL
				end
				else
				begin
			--print 'insert into TB_MP_TEMP_REPORT(ParmValue,MeasureDT,FormatType) values('''+cast(@v/@count as varchar)+''','''+@starttime+''','number')'
				set @insertSQL = N' insert into '+@tablebname+'(UUID,mpid,ValueMeaning,ParmValue,alarmmax,alarmmin,numtail,MeasureDT,memotype,FormatType) values('+cast(@uuid as varchar)+','+@mpid+''','+@valuemeaning+''', '''+cast(@v/@count as varchar)+''', '''+cast(@alarmmax as varchar)+''', '''+cast(@alarmmin as varchar)+''','''+@numtail+''', '''+CONVERT(varchar(16),@startTime, 120 )+''','''+@memotypetemp+''',''number'')'
				print N'@insertSQL: '+@insertSQL
				EXEC sp_executesql @insertSQL
			--insert into TB_MP_TEMP_REPORT(ParmValue,MeasureDT,FormatType) values(cast(@v/@count as varchar),@starttime,'number')
				end
			end
			else if(@type='avgavg')
			begin
				if @memotypetemp IS NULL
				begin
				set @memotypetemp=N'-'

				set @insertSQL = N' insert into '+@tablebname+'(UUID,mpid,ValueMeaning,ParmValue,alarmmax,alarmmin,numtail,MeasureDT,memotype,FormatType) values('+cast(@uuid as varchar)+','+@mpid+''','''+@valuemeaning+''', '''+cast(@v/@count as varchar)+''', '''+cast(@alarmmax as varchar)+''', '''+cast(@alarmmin as varchar)+''','''+@numtail+''', '''+CONVERT(varchar(16),@startTime, 120 )+''','''+@memotypetemp+''',''number'')'
				print N'@insertSQL: '+@insertSQL
				EXEC sp_executesql @insertSQL
				end
				else
				begin
			--print 'insert into TB_MP_TEMP_REPORT(ParmValue,MeasureDT,FormatType) values('''+cast(@v/@count as varchar)+''','''+@starttime+''','number')'
				set @insertSQL = N' insert into '+@tablebname+'(UUID,mpid,ValueMeaning,ParmValue,alarmmax,alarmmin,numtail,MeasureDT,memotype,FormatType) values('+cast(@uuid as varchar)+','+@mpid+''','''+@valuemeaning+''', '''+cast(@v/@count as varchar)+''', '''+cast(@alarmmax as varchar)+''', '''+cast(@alarmmin as varchar)+''','''+@numtail+''', '''+CONVERT(varchar(16),@startTime, 120 )+''','''+@memotypetemp+''',''number'')'
				print N'@insertSQL: '+@insertSQL
				EXEC sp_executesql @insertSQL
			--insert into TB_MP_TEMP_REPORT(ParmValue,MeasureDT,FormatType) values(cast(@v/@count as varchar),@starttime,'number')
				end
			end

			else if(@type='runtimerow')
			begin
			print 'runtimerow'
			end
			else
			begin
				if @memotypetemp IS NULL
				begin
				set @memotypetemp=N'-'
				end
				else
				begin
				set	@memotypetemp = @memotypetemp
				end

				set @insertSQL = N' insert into '+@tablebname+'(UUID,mpid,ValueMeaning,ParmValue,alarmmax,alarmmin,numtail,MeasureDT,memotype,FormatType) values('+cast(@uuid as varchar)+','''+@mpid+''','''+@valuemeaning+''', '''+cast(@v as varchar)+''', '''+cast(@alarmmax as varchar)+''', '''+cast(@alarmmin as varchar)+''','''+@numtail+''', '''+CONVERT(varchar(16),@startTime, 120 )+''','''+@memotypetemp+''',''number'')'
				print N'@insertSQL: '+@insertSQL
				EXEC sp_executesql @insertSQL
			end

		end
	end
	--else
	--begin
	--	--print '-'
	--	insert into TB_MP_TEMP_REPORT(ParmValue,MeasureDT,FormatType) values('-',@starttime,'label')
	--end


GO

-- ----------------------------
-- Procedure structure for sp_report_getmpexpress
-- ----------------------------
DROP PROCEDURE [dbo].[sp_report_getmpexpress]
GO



CREATE PROCEDURE [dbo].[sp_report_getmpexpress]  @stime varchar(10),  @starthour int 
AS


declare @MPointID varchar(50)
declare @MPointCode varchar(50)
declare @biztype varchar(50)
declare @exp varchar(5000)

declare @sqlstr varchar(8000)
declare @expstr varchar(1000)
declare @tmpstr varchar(20)
declare @numstr varchar(20)
declare @tid int
declare @value varchar(5000)
declare @mp varchar(50)
declare @type varchar(50)
declare @scadacode varchar(500)
declare @count int
declare @id varchar(50)


Declare @starttime datetime
Declare @endtime datetime
Declare @tempdate datetime

Select @starttime=cast(@stime as datetime)
Select @starttime=DateAdd(hour,@starthour,@starttime) 
select @endtime = DateAdd(day,1,@starttime)

set @tmpstr='()+-*/'


DECLARE rptfieldcs CURSOR FOR SELECT  MPointID, MPointCode, biztype, [exp] FROM TB_MeasurePoint  where  len(ltrim(rtrim([exp])))>0 and biztype = '平台' order by morder


open rptfieldcs
FETCH next FROM rptfieldcs INTO @MPointID,@MPointCode,@biztype,@exp



WHILE (@@FETCH_STATUS=0)
BEGIN
	--把公式遍历出来
	declare @expflag bit
	set @expflag = 1

	while @expflag=1
	begin

	set @expflag = 0
	set @expstr=''
	set @exp = ltrim(rtrim(@exp))
	declare @expcc varchar(8000)
    	declare expcur cursor for select a from dbo.f_get_exp_split(@exp,@tmpstr)
	--print 'select a from dbo.f_get_exp_split('''+@exp+''','''+@tmpstr+''')'
    	open expcur
    	fetch next from expcur into @expcc
	--print @expcc
	while @@fetch_status=0
	begin

     	if (charindex(@expcc,@tmpstr)>0)
     	begin
      	set @expstr=@expstr+@expcc
     	end
	else if (ISNUMERIC(@expcc)=1)
    	begin
     	set @expstr=@expstr+cast(@expcc as varchar(8000))
     	end
	else
	begin
	declare @sql1 nvarchar(1000)
	declare @biztype1 varchar(50)
	declare @exp1 varchar(5000)
	SET @sql1 = 'select  @biztype1 = biztype, @exp1 = exp from TB_MeasurePoint where MPointCode = '''+@expcc+''''
	--print @sql1	
	exec sp_executesql @sql1,N'@biztype1 varchar(50) output,@exp1 varchar(5000) output',@biztype1 output,@exp1 output
	if(@biztype1='污水')
	begin
	set @expstr=@expstr+@expcc
	--print @expstr
	end
	else if(@biztype1='平台' and  (len(ltrim(rtrim(@exp1)))<=0 or @exp1 is null))
	begin
	set @expstr=@expstr+@expcc
	end
	else if(@biztype1='平台' and  len(ltrim(rtrim(@exp1)))>0)
	begin
	set @expstr=@expstr+'('+@exp1+')'
	set @expflag = 1
	end
	end

     	fetch next from expcur into @expcc
    	end

    	close expcur
    	deallocate expcur

	set @exp = @expstr
	--print @exp
	end

	--根据公式列出完整数学计算公式
	 set @sqlstr=''
         declare @cc varchar(8000)
	 set @exp = ltrim(rtrim(@exp))
         declare c1 cursor for
	 select a from dbo.f_get_exp_split(@exp,@tmpstr)
    open c1
    fetch next from c1 into @cc
    while @@fetch_status=0
    begin
     if (charindex(@cc,@tmpstr)>0)
     begin
      set @sqlstr=@sqlstr+@cc
     end
	else if (ISNUMERIC(@cc)=1)
    begin
     set @sqlstr=@sqlstr+cast(@cc as varchar(8000))
     end
     else
     begin

	declare @sql2 nvarchar(1000)
	SET @sql2 = 'select  @biztype1 = biztype from TB_MeasurePoint where MPointCode='''+@cc+''''
	--print @sql2	
	exec sp_executesql @sql2,N'@biztype1 varchar(50) output',@biztype1 output

	delete from TB_MP_TEMP_REPORT

	if(@biztype1='污水')
	begin
	exec sp_report_getdata @starttime,@endtime,'first',@cc
	end
	else
	begin
	exec sp_report_getdata @starttime,@endtime,'equal',@cc
	end
	
	declare @sql3 nvarchar(1000)
	SET @sql3 = 'select top 1 @value = ParmValue from TB_MP_TEMP_REPORT order by MeasureDT asc '
	--print @sql3	
	exec sp_executesql @sql3,N'@value varchar(5000) output',@value output

	--print @value
	if (@value='-')
	begin
	set @value=0.0
	set @sqlstr=null
	end
	else
	begin
	set @sqlstr=@sqlstr+cast(@value as varchar(5000))
	end
	--print @sqlstr

     end
     fetch next from c1 into @cc
    end
    close c1
    deallocate c1

	--插入测量点表数值
    if(@sqlstr is null)
	begin
	set @sqlstr = '0.00'
	end

	declare @sql4 nvarchar(2000)
	SET @sql4 = N''

	set @sql4='delete from TB_MPExpress  where MPointCode = '''+@MPointCode+''' and MeasureDT = '''+cast(@starttime as varchar(19))+''''
	
    	--print @sql4
    	exec sp_executesql @sql4
	
	declare @sql5 nvarchar(2000)
	SET @sql5 = N''

	set @sql5='insert into TB_MPExpress (MPointCode,ParmValue,MeasureDT) values ('''+@MPointCode+''','+@sqlstr+','''+cast(@starttime as varchar(19))+''')'
	
    	--print @sql5
    	exec sp_executesql @sql5


FETCH next FROM rptfieldcs INTO  @MPointID,@MPointCode,@biztype,@exp
end



CLOSE rptfieldcs
DEALLOCATE rptfieldcs


Select * from TB_MPExpress order by itemid


GO

-- ----------------------------
-- Procedure structure for sp_report_halfYear_01
-- ----------------------------
DROP PROCEDURE [dbo].[sp_report_halfYear_01]
GO



CREATE PROCEDURE [dbo].[sp_report_halfYear_01]
@stime varchar(10), @etime varchar(10), @starthour int,  @intval int, @mpstr varchar(5000),@rownum int,@userid varchar(50)
 AS
declare @starttime datetime
declare @endtime datetime
declare @tempdate datetime
declare @mptemp varchar(500)
declare @collectmode varchar(50)
declare @mp varchar(500)


Select @starttime=cast(@stime as datetime)
Select @starttime=DateAdd(hour,@starthour,@starttime)
-- Select @starttime=DateAdd(MONTH,-1,@starttime)
Select @starttime=DateAdd(MONTH,0,@starttime)
Select @endtime=cast(@etime as datetime)
Select @endtime=DateAdd(hour,@starthour,@endtime)
if(@endtime>dateadd(day,-1,getdate()))
begin
set @endtime=dateadd(day,0,getdate())
end

-- DROP TABLE TB_MP_TEMP_REPORT
-- 
-- CREATE TABLE [dbo].[TB_MP_TEMP_REPORT] (
-- 	[ItemID] [bigint] IDENTITY (1, 1) NOT NULL ,
-- 	[ParmValue] [varchar] (2000) COLLATE Chinese_PRC_CI_AS NOT NULL ,
-- 	[MeasureDT] [datetime] NOT NULL ,
-- 	[FormatType] [varchar] (50) COLLATE Chinese_PRC_CI_AS NOT NULL,
-- 	[Decimal] [int] 
-- ) ON [PRIMARY]

while(@starttime<@endtime)
begin
	set @tempdate = dateadd(month,@intval,@starttime)
	--if(DATEPART(dd,@starttime)=1)
	--begin
	--insert into TB_MP_TEMP_REPORT(ParmValue,MeasureDT,FormatType) values(cast(DATEPART(mm,@starttime) as varchar)+'月',@starttime,'label')
	--end
	--else
	--begin
	--insert into TB_MP_TEMP_REPORT(ParmValue,MeasureDT,FormatType) values(cast(DATEPART(mm,dateadd(mm,1,@starttime)) as varchar)+'月',@starttime,'label')
	--end
	
	DECLARE CRStatus cursor for 
	select * from dbo.FUN_split(@mpstr,'|')
	open CRStatus

	FETCH  CRStatus into @mptemp

	set @mp = SUBSTRING ( @mptemp , 0 , CHARINDEX(';',@mptemp) )
	set @collectmode = SUBSTRING ( @mptemp , CHARINDEX(';',@mptemp)+1, LEN ( @mptemp )-CHARINDEX(';',@mptemp) )

	while (@@fetch_status=0)
	begin

        exec sp_report_getdata @starttime,@tempdate,@collectmode,@mp,@userid

	FETCH  CRStatus into @mptemp

	set @mp = SUBSTRING ( @mptemp , 0 , CHARINDEX(';',@mptemp) )
	set @collectmode = SUBSTRING ( @mptemp , CHARINDEX(';',@mptemp)+1, LEN ( @mptemp )-CHARINDEX(';',@mptemp) )

	end

	close CRStatus
	DEALLOCATE CRStatus
	
	set @starttime = @tempdate
end

Select * from TB_MP_TEMP_REPORT where userid=@userid order by itemid


GO

-- ----------------------------
-- Procedure structure for sp_report_mth_01
-- ----------------------------
DROP PROCEDURE [dbo].[sp_report_mth_01]
GO



CREATE PROCEDURE [dbo].[sp_report_mth_01]
@stime varchar(10), @etime varchar(10), @starthour int,  @intval int, @mpstr varchar(5000),@rownum int,@userid varchar(50)
 AS
declare @starttime datetime
declare @endtime datetime
declare @tempdate datetime
declare @mptemp varchar(500)
declare @collectmode varchar(50)
declare @mp varchar(500)


Select @starttime=cast(@stime as datetime)
Select @starttime=DateAdd(hour,@starthour,@starttime)
Select @endtime=cast(@etime as datetime)
Select @endtime=DateAdd(hour,@starthour,@endtime)
if(@endtime>dateadd(day,-1,getdate()))
begin
set @endtime=dateadd(day,0,getdate())
--set @endtime = dateadd(hh,-datepart(hh,@endtime),@endtime)
--set @endtime = dateadd(ss,-datepart(ss,@endtime),@endtime)
--set @endtime = dateadd(mi,-datepart(mi,@endtime),@endtime)
--set @endtime = dateadd(ms,-datepart(ms,@endtime),@endtime)
--set @endtime = dateadd(hh,@starthour,@endtime)
end

--print @starttime
--print @endtime

-- DROP TABLE TB_MP_TEMP_REPORT

-- CREATE TABLE [dbo].[TB_MP_TEMP_REPORT] (
-- 	[ItemID] [bigint] IDENTITY (1, 1) NOT NULL ,
-- 	[ParmValue] [varchar] (2000) COLLATE Chinese_PRC_CI_AS NOT NULL ,
-- 	[MeasureDT] [datetime] NOT NULL ,
-- 	[FormatType] [varchar] (50) COLLATE Chinese_PRC_CI_AS NOT NULL,
-- 	[Decimal] [int] 
-- ) ON [PRIMARY]

while(@starttime<=@endtime)
begin
	set @tempdate = dateadd(hour,@intval,@starttime)

-- 	insert into TB_MP_TEMP_REPORT(ParmValue,MeasureDT,FormatType,Decimal) values(cast(DATEPART(mm,@starttime)  as varchar)+'月'+cast(DATEPART(dd,@starttime)  as varchar)+'日',@starttime,'label',0)

	DECLARE CRStatus cursor for 
	select * from dbo.FUN_split(@mpstr,'|')
	open CRStatus

	FETCH  CRStatus into @mptemp

	set @mp = SUBSTRING ( @mptemp , 0 , CHARINDEX(';',@mptemp) )
	set @collectmode = SUBSTRING ( @mptemp , CHARINDEX(';',@mptemp)+1, LEN ( @mptemp )-CHARINDEX(';',@mptemp) )

	while (@@fetch_status=0)
	begin
		
        exec sp_report_getdata @starttime,@tempdate,@collectmode,@mp,@userid

	FETCH  CRStatus into @mptemp

	set @mp = SUBSTRING ( @mptemp , 0 , CHARINDEX(';',@mptemp) )
	set @collectmode = SUBSTRING ( @mptemp , CHARINDEX(';',@mptemp)+1, LEN ( @mptemp )-CHARINDEX(';',@mptemp) )

	end

	close CRStatus
	DEALLOCATE CRStatus

	set @tempdate = dateadd(hour,@intval,@starttime)
	set @starttime = @tempdate
end

Select * from TB_MP_TEMP_REPORT where userid=@userid order by itemid


GO

-- ----------------------------
-- Procedure structure for sp_report_mth_01_test
-- ----------------------------
DROP PROCEDURE [dbo].[sp_report_mth_01_test]
GO



CREATE PROCEDURE [dbo].[sp_report_mth_01_test]
@tbname varchar(200), @stime varchar(10), @etime varchar(10), @starthour int,  @intval int, @mpstr varchar(800),@rownum int
 AS
declare @starttime datetime
declare @endtime datetime
declare @tempdate datetime
declare @mptemp varchar(500)
declare @tablebname varchar(500)
declare @collectmode varchar(50)
declare @mp varchar(500)
declare @dropSQL nvarchar(4000)
declare @createSQL nvarchar(4000)
declare @insertSQL nvarchar(4000)
declare @selectSQL nvarchar(4000)

Select @tablebname=@tbname
Select @starttime=cast(@stime as datetime)
Select @starttime=DateAdd(hour,@starthour,@starttime)
Select @endtime=cast(@etime as datetime)
Select @endtime=DateAdd(hour,@starthour,@endtime)

if(@endtime>dateadd(day,-1,getdate()))
begin
set @endtime=dateadd(day,-1,getdate())
set @endtime = dateadd(hh,-datepart(hh,@endtime),@endtime)
set @endtime = dateadd(ss,-datepart(ss,@endtime),@endtime)
set @endtime = dateadd(mi,-datepart(mi,@endtime),@endtime)
set @endtime = dateadd(ms,-datepart(ms,@endtime),@endtime)
set @endtime = dateadd(hh,@starthour,@endtime)
end
print N'@@tablebname: '+@tablebname
--print @starttime
--print @endtime





begin
set @dropSQL = 'truncate TABLE '+@tablebname 
set @createSQL=' CREATE TABLE  [dbo].['+@tablebname+'] (
	[ItemID] [bigint] IDENTITY (1, 1) NOT NULL ,
	[ParmValue] [varchar] (2000) COLLATE Chinese_PRC_CI_AS NOT NULL ,
	[MeasureDT] [datetime] NOT NULL ,
	[mpid] [varchar] (50) COLLATE Chinese_PRC_CI_AS ,
	[ValueMeaning] [varchar] (50) COLLATE Chinese_PRC_CI_AS ,
	[alarmmax] money ,
	[alarmmin] money ,
	[numtail] [varchar] (50) COLLATE Chinese_PRC_CI_AS,
	[memotype] [varchar]  (500) COLLATE Chinese_PRC_CI_AS,
	[UUID] [bigint] ,
	[FormatType] [varchar] (50) COLLATE Chinese_PRC_CI_AS NOT NULL 
) ON [PRIMARY] '
end

begin
	if Exists(select top 1 * from sysObjects where Id=OBJECT_ID(@tablebname))
	begin
		EXEC sp_executesql @dropSQL
		print N'@dropSQL: '+@dropSQL
	end
	else
	begin
		EXEC sp_executesql @createSQL
	end
end
--DROP TABLE if exists TB_MP_TEMP_REPORT 

--CREATE TABLE [dbo].[TB_MP_TEMP_REPORT] (
--	[ItemID] [bigint] IDENTITY (1, 1) NOT NULL ,
--	[ParmValue] [varchar] (2000) COLLATE Chinese_PRC_CI_AS NOT NULL ,
--	[MeasureDT] [datetime] NOT NULL ,
--	[FormatType] [varchar] (50) COLLATE Chinese_PRC_CI_AS NOT NULL 
--) ON [PRIMARY]
print @starttime
print @endtime
while(@starttime<=@endtime)
begin
	set @tempdate = dateadd(hour,@intval,@starttime)
	set @insertSQL = N' insert into '+@tablebname+'(ParmValue,MeasureDT,FormatType) values('''+cast(DATEPART(mm,@starttime)  as varchar)+'月'+cast(DATEPART(dd,@starttime)  as varchar)+'日'', '''+CONVERT(varchar(16),@startTime, 120 )+''',''label'')'
	print N'@insertSQL: '+@insertSQL
	EXEC sp_executesql @insertSQL
	--insert into TB_MP_TEMP_REPORT(ParmValue,MeasureDT,FormatType) values(cast(DATEPART(mm,@starttime)  as varchar)+'月'+cast(DATEPART(dd,@starttime)  as varchar)+'日',@starttime,'label')

	DECLARE CRStatus cursor for 
	select * from dbo.FUN_split(@mpstr,'|')
	open CRStatus

	FETCH  CRStatus into @mptemp

	set @mp = SUBSTRING ( @mptemp , 0 , CHARINDEX(';',@mptemp) )
	set @collectmode = SUBSTRING ( @mptemp , CHARINDEX(';',@mptemp)+1, LEN ( @mptemp )-CHARINDEX(';',@mptemp) )

	while (@@fetch_status=0)
	begin

        exec sp_report_getdata_test @tbname,@starttime,@tempdate,@collectmode,@mp

	FETCH  CRStatus into @mptemp

	set @mp = SUBSTRING ( @mptemp , 0 , CHARINDEX(';',@mptemp) )
	set @collectmode = SUBSTRING ( @mptemp , CHARINDEX(';',@mptemp)+1, LEN ( @mptemp )-CHARINDEX(';',@mptemp) )

	end

	close CRStatus
	DEALLOCATE CRStatus
	
	set @starttime = @tempdate

	--begin
	--set @dropSQL=N' DROP TABLE if exists  [dbo].['+@tablebname+'] '
	--print N'@dropSQL: '+@dropSQL
	--EXEC sp_executesql @dropSQL
	--end
end

begin
 set @selectSQL = N' Select * from [dbo].['+@tablebname+'] order by itemid'
 print N'@selectSQL: '+@selectSQL
 EXEC sp_executesql @selectSQL
 --EXEC sp_executesql @dropSQL
 end
--Select * from TB_MP_TEMP_REPORT order by itemid
begin
set @dropSQL=N' DROP TABLE if exists  '+@tablebname+' '
print N'@dropSQL: '+@dropSQL
EXEC sp_executesql @dropSQL
end


GO

-- ----------------------------
-- Procedure structure for sp_report_mth_02
-- ----------------------------
DROP PROCEDURE [dbo].[sp_report_mth_02]
GO



CREATE PROCEDURE [dbo].[sp_report_mth_02]
@stime varchar(10), @etime varchar(10), @starthour int,  @intval int, @mpstr varchar(800),@rownum int
 AS
declare @starttime datetime
declare @endtime datetime
declare @tempdate datetime
declare @mptemp varchar(500)
declare @collectmode varchar(50)
declare @mp varchar(500)


Select @starttime=cast(@stime as datetime)
Select @starttime=DateAdd(hour,@starthour,@starttime)
Select @endtime=cast(@etime as datetime)
Select @endtime=DateAdd(hour,@starthour,@endtime)

--print @starttime
--print @endtime

DROP TABLE TB_MP_TEMP_REPORT

CREATE TABLE [dbo].[TB_MP_TEMP_REPORT] (
	[ItemID] [bigint] IDENTITY (1, 1) NOT NULL ,
	[ParmValue] [varchar] (2000) COLLATE Chinese_PRC_CI_AS NOT NULL ,
	[MeasureDT] [datetime] NOT NULL ,
	[FormatType] [varchar] (50) COLLATE Chinese_PRC_CI_AS NOT NULL,
	[Decimal] [int] NOT NULL  
) ON [PRIMARY]


	DECLARE CRStatus cursor for 
	select * from dbo.FUN_split(@mpstr,'|')
	open CRStatus

	FETCH  CRStatus into @mptemp

	set @mp = SUBSTRING ( @mptemp , 0 , CHARINDEX(';',@mptemp) )
	set @collectmode = SUBSTRING ( @mptemp , CHARINDEX(';',@mptemp)+1, LEN ( @mptemp )-CHARINDEX(';',@mptemp) )

	while (@@fetch_status=0)
	begin

        exec sp_report_getdata @starttime,@endtime,@collectmode,@mp

	FETCH  CRStatus into @mptemp

	set @mp = SUBSTRING ( @mptemp , 0 , CHARINDEX(';',@mptemp) )
	set @collectmode = SUBSTRING ( @mptemp , CHARINDEX(';',@mptemp)+1, LEN ( @mptemp )-CHARINDEX(';',@mptemp) )

	end

	close CRStatus
	DEALLOCATE CRStatus
	

Select * from TB_MP_TEMP_REPORT order by itemid


GO

-- ----------------------------
-- Procedure structure for sp_report_mth_03
-- ----------------------------
DROP PROCEDURE [dbo].[sp_report_mth_03]
GO



CREATE PROCEDURE [dbo].[sp_report_mth_03]
@stime varchar(10), @etime varchar(10), @starthour int,  @intval int, @mpstr varchar(1000),@rownum int
 AS
declare @starttime datetime
declare @endtime datetime
declare @tempdate datetime
declare @mptemp varchar(500)
declare @collectmode varchar(50)
declare @mp varchar(500)


Select @starttime=cast(@stime as datetime)
Select @starttime=DateAdd(hour,@starthour,@starttime)
Select @endtime=cast(@etime as datetime)
Select @endtime=DateAdd(hour,@starthour,@endtime)
if(@endtime>dateadd(day,-1,getdate()))
begin
set @endtime=dateadd(day,-1,getdate())
set @endtime = dateadd(hh,-datepart(hh,@endtime),@endtime)
set @endtime = dateadd(ss,-datepart(ss,@endtime),@endtime)
set @endtime = dateadd(mi,-datepart(mi,@endtime),@endtime)
set @endtime = dateadd(ms,-datepart(ms,@endtime),@endtime)
set @endtime = dateadd(hh,@starthour,@endtime)
end

--print @starttime
--print @endtime

DROP TABLE TB_MP_TEMP_REPORT

CREATE TABLE [dbo].[TB_MP_TEMP_REPORT] (
	[ItemID] [bigint] IDENTITY (1, 1) NOT NULL ,
	[ParmValue] [varchar] (2000) COLLATE Chinese_PRC_CI_AS NOT NULL ,
	[MeasureDT] [datetime] NOT NULL ,
	[FormatType] [varchar] (50) COLLATE Chinese_PRC_CI_AS NOT NULL,
	[Decimal] [int] NOT NULL 
) ON [PRIMARY]

while(@starttime<=@endtime)
begin
	set @tempdate = dateadd(hour,@intval,@starttime)

	DECLARE CRStatus cursor for 
	select * from dbo.FUN_split(@mpstr,'|')
	open CRStatus

	FETCH  CRStatus into @mptemp

	set @mp = SUBSTRING ( @mptemp , 0 , CHARINDEX(';',@mptemp) )
	set @collectmode = SUBSTRING ( @mptemp , CHARINDEX(';',@mptemp)+1, LEN ( @mptemp )-CHARINDEX(';',@mptemp) )

	while (@@fetch_status=0)
	begin

        exec sp_report_getdata @starttime,@tempdate,@collectmode,@mp

	FETCH  CRStatus into @mptemp

	set @mp = SUBSTRING ( @mptemp , 0 , CHARINDEX(';',@mptemp) )
	set @collectmode = SUBSTRING ( @mptemp , CHARINDEX(';',@mptemp)+1, LEN ( @mptemp )-CHARINDEX(';',@mptemp) )

	end

	close CRStatus
	DEALLOCATE CRStatus
	
	set @starttime = @tempdate
end

Select * from TB_MP_TEMP_REPORT order by itemid


GO

-- ----------------------------
-- Procedure structure for sp_report_mth_03_test
-- ----------------------------
DROP PROCEDURE [dbo].[sp_report_mth_03_test]
GO



CREATE PROCEDURE [dbo].[sp_report_mth_03_test]
@tbname varchar(200), @stime varchar(10), @etime varchar(10), @starthour int,  @intval int, @mpstr varchar(800),@rownum int
 AS
declare @starttime datetime
declare @endtime datetime
declare @tempdate datetime
declare @mptemp varchar(500)
declare @collectmode varchar(50)
declare @mp varchar(500)
declare @tablebname varchar(500)
declare @dropSQL nvarchar(4000)
declare @createSQL nvarchar(4000)
declare @insertSQL nvarchar(4000)
declare @selectSQL nvarchar(4000)

Select @tablebname=@tbname
Select @starttime=cast(@stime as datetime)
Select @starttime=DateAdd(hour,@starthour,@starttime)
Select @endtime=cast(@etime as datetime)
Select @endtime=DateAdd(hour,@starthour,@endtime)
if(@endtime>dateadd(day,-1,getdate()))
begin
set @endtime=dateadd(day,-1,getdate())
set @endtime = dateadd(hh,-datepart(hh,@endtime),@endtime)
set @endtime = dateadd(ss,-datepart(ss,@endtime),@endtime)
set @endtime = dateadd(mi,-datepart(mi,@endtime),@endtime)
set @endtime = dateadd(ms,-datepart(ms,@endtime),@endtime)
set @endtime = dateadd(hh,@starthour,@endtime)
end

--print @starttime
--print @endtime
begin
--set @dropSQL=  N'IF OBJECT_ID(N'+@tablebname+',N'U') is not null DROP TABLE '+@tablebname+''
--set @dropSQL = 'if Exists(select top 1 * from sysObjects where Id=OBJECT_ID('+@tablebname+')) DROP TABLE '+@tablebname
-- YYJ 20200829 用删除数据方式替代删除表格
set @dropSQL = 'truncate TABLE '+@tablebname 
set @createSQL=' CREATE TABLE  [dbo].['+@tablebname+'] (
	[ItemID] [bigint] IDENTITY (1, 1) NOT NULL ,
	[ParmValue] [varchar] (2000) COLLATE Chinese_PRC_CI_AS NOT NULL ,
	[MeasureDT] [datetime] NOT NULL ,
	[mpid] [varchar] (50) COLLATE Chinese_PRC_CI_AS ,
	[ValueMeaning] [varchar] (50) COLLATE Chinese_PRC_CI_AS ,
	[alarmmax] money ,
	[alarmmin] money ,
	[numtail] [varchar] (50) COLLATE Chinese_PRC_CI_AS,
	[memotype] [varchar]  (500) COLLATE Chinese_PRC_CI_AS,
	[UUID] [bigint] ,
	[FormatType] [varchar] (50) COLLATE Chinese_PRC_CI_AS NOT NULL 
) ON [PRIMARY] '
--set @dropSQL = N'IF EXISTS(Select ItemID from '+@tablebname+')
	--DROP TABLE [dbo].['+@tablebname+'] 
	 --CREATE TABLE  [dbo].['+@tablebname+'] (
		--[ItemID] [bigint] IDENTITY (1, 1) NOT NULL ,
	--[ParmValue] [varchar] (2000) COLLATE Chinese_PRC_CI_AS NOT NULL ,
	--[MeasureDT] [datetime] NOT NULL ,
	--[mpid] [varchar] (50) COLLATE Chinese_PRC_CI_AS ,
	--[memotype] [varchar]  (500) COLLATE Chinese_PRC_CI_AS,
	--[UUID] [bigint] NOT NULL ,
	--[FormatType] [varchar] (50) COLLATE Chinese_PRC_CI_AS NOT NULL
	--) ON [PRIMARY] '
end

--DROP TABLE TB_MP_TEMP_REPORT

--CREATE TABLE [dbo].[TB_MP_TEMP_REPORT] (
--	[ItemID] [bigint] IDENTITY (1, 1) NOT NULL ,
--	[ParmValue] [varchar] (2000) COLLATE Chinese_PRC_CI_AS NOT NULL ,
--	[MeasureDT] [datetime] NOT NULL ,
--	[FormatType] [varchar] (50) COLLATE Chinese_PRC_CI_AS NOT NULL 
--) ON [PRIMARY]
begin
	if Exists(select top 1 * from sysObjects where Id=OBJECT_ID(@tablebname))
	begin
		EXEC sp_executesql @dropSQL
		print N'@dropSQL: '+@dropSQL
	end
	else
	begin
		EXEC sp_executesql @createSQL
	end
end



while(@starttime<=@endtime)
begin
	set @tempdate = dateadd(hour,@intval,@starttime)

	DECLARE CRStatus cursor for 
	select * from dbo.FUN_split(@mpstr,'|')
	open CRStatus

	FETCH  CRStatus into @mptemp

	set @mp = SUBSTRING ( @mptemp , 0 , CHARINDEX(';',@mptemp) )
	set @collectmode = SUBSTRING ( @mptemp , CHARINDEX(';',@mptemp)+1, LEN ( @mptemp )-CHARINDEX(';',@mptemp) )

	while (@@fetch_status=0)
	begin

        exec sp_report_getdata_test @tbname,@starttime,@tempdate,@collectmode,@mp

	FETCH  CRStatus into @mptemp

	set @mp = SUBSTRING ( @mptemp , 0 , CHARINDEX(';',@mptemp) )
	set @collectmode = SUBSTRING ( @mptemp , CHARINDEX(';',@mptemp)+1, LEN ( @mptemp )-CHARINDEX(';',@mptemp) )

	end

	close CRStatus
	DEALLOCATE CRStatus
	
	set @starttime = @tempdate
end

begin
 set @selectSQL = N' Select * from [dbo].['+@tablebname+'] order by itemid'
 print N'@selectSQL: '+@selectSQL
 EXEC sp_executesql @selectSQL
--EXEC sp_executesql @dropSQL
 end
--Select * from TB_MP_TEMP_REPORT order by itemid

begin
set @dropSQL=N' DROP TABLE if exists  '+@tablebname+' '
print N'@dropSQL: '+@dropSQL
EXEC sp_executesql @dropSQL
end


GO

-- ----------------------------
-- Procedure structure for sp_report_mth_04
-- ----------------------------
DROP PROCEDURE [dbo].[sp_report_mth_04]
GO



CREATE PROCEDURE [dbo].[sp_report_mth_04]
@stime varchar(10), @etime varchar(10), @starthour int,  @intval int, @mpstr varchar(800),@rownum int
 AS
declare @starttime datetime
declare @endtime datetime
declare @tempdate datetime
declare @mptemp varchar(500)
declare @collectmode varchar(50)
declare @mp varchar(500)
declare @starttime1 datetime

Select @starttime=cast(@stime as datetime)
Select @starttime=DateAdd(hour,@starthour,@starttime)
Select @endtime=cast(@etime as datetime)
Select @endtime=DateAdd(hour,@starthour,@endtime)
if(@endtime>dateadd(day,-1,getdate()))
begin
set @endtime=dateadd(day,-1,getdate())
set @endtime = dateadd(hh,-datepart(hh,@endtime),@endtime)
set @endtime = dateadd(ss,-datepart(ss,@endtime),@endtime)
set @endtime = dateadd(mi,-datepart(mi,@endtime),@endtime)
set @endtime = dateadd(ms,-datepart(ms,@endtime),@endtime)
set @endtime = dateadd(hh,@starthour,@endtime)
end

set @starttime=DateAdd(DAY,1,@starttime)
set @endtime=DateAdd(DAY,1,@endtime)

--print @starttime
--print @endtime

DROP TABLE TB_MP_TEMP_REPORT

CREATE TABLE [dbo].[TB_MP_TEMP_REPORT] (
	[ItemID] [bigint] IDENTITY (1, 1) NOT NULL ,
	[ParmValue] [varchar] (2000) COLLATE Chinese_PRC_CI_AS NOT NULL ,
	[MeasureDT] [datetime] NOT NULL ,
	[FormatType] [varchar] (50) COLLATE Chinese_PRC_CI_AS NOT NULL,
	[Decimal] [int] NOT NULL 
) ON [PRIMARY]

while(@starttime<@endtime)
begin
	set @tempdate = dateadd(hour,@intval,@starttime)
	set @starttime1=DATEADD(DAY,-1,@starttime)
	--insert into TB_MP_TEMP_REPORT(ParmValue,MeasureDT,FormatType) values(cast((DATEPART(mm,@starttime1))  as varchar)+'月'+cast((DATEPART(dd,@starttime1))  as varchar)+'日',@starttime,'label')

	DECLARE CRStatus cursor for 
	select * from dbo.FUN_split(@mpstr,'|')
	open CRStatus

	FETCH  CRStatus into @mptemp

	set @mp = SUBSTRING ( @mptemp , 0 , CHARINDEX(';',@mptemp) )
	set @collectmode = SUBSTRING ( @mptemp , CHARINDEX(';',@mptemp)+1, LEN ( @mptemp )-CHARINDEX(';',@mptemp) )

	while (@@fetch_status=0)
	begin

        exec sp_report_getdata @starttime,@tempdate,@collectmode,@mp

	FETCH  CRStatus into @mptemp

	set @mp = SUBSTRING ( @mptemp , 0 , CHARINDEX(';',@mptemp) )
	set @collectmode = SUBSTRING ( @mptemp , CHARINDEX(';',@mptemp)+1, LEN ( @mptemp )-CHARINDEX(';',@mptemp) )

	end

	close CRStatus
	DEALLOCATE CRStatus
	
	set @starttime = @tempdate
end

Select * from TB_MP_TEMP_REPORT order by itemid


GO

-- ----------------------------
-- Procedure structure for sp_report_mth_041
-- ----------------------------
DROP PROCEDURE [dbo].[sp_report_mth_041]
GO



CREATE PROCEDURE [dbo].[sp_report_mth_041]
@stime varchar(10), @etime varchar(10), @starthour int,  @intval int, @mpstr varchar(800),@rownum int
 AS
declare @starttime datetime
declare @endtime datetime
declare @tempdate datetime
declare @mptemp varchar(500)
declare @collectmode varchar(50)
declare @mp varchar(500)
declare @starttime1 datetime

Select @starttime=cast(@stime as datetime)
Select @starttime=DateAdd(hour,@starthour,@starttime)
Select @endtime=cast(@etime as datetime)
Select @endtime=DateAdd(hour,@starthour,@endtime)
if(@endtime>dateadd(day,-1,getdate()))
begin
set @endtime=dateadd(day,-1,getdate())
set @endtime = dateadd(hh,-datepart(hh,@endtime),@endtime)
set @endtime = dateadd(ss,-datepart(ss,@endtime),@endtime)
set @endtime = dateadd(mi,-datepart(mi,@endtime),@endtime)
set @endtime = dateadd(ms,-datepart(ms,@endtime),@endtime)
set @endtime = dateadd(hh,@starthour,@endtime)
end

set @starttime=DateAdd(DAY,1,@starttime)
set @endtime=DateAdd(DAY,1,@endtime)

--print @starttime
--print @endtime

DROP TABLE TB_MP_TEMP_REPORT

CREATE TABLE [dbo].[TB_MP_TEMP_REPORT] (
	[ItemID] [bigint] IDENTITY (1, 1) NOT NULL ,
	[ParmValue] [varchar] (2000) COLLATE Chinese_PRC_CI_AS NOT NULL ,
	[MeasureDT] [datetime] NOT NULL ,
	[FormatType] [varchar] (50) COLLATE Chinese_PRC_CI_AS NOT NULL,
	[Decimal] [int] NOT NULL 
) ON [PRIMARY]

while(@starttime<=@endtime)
begin
	set @tempdate = dateadd(hour,@intval,@starttime)
	set @starttime1=DATEADD(DAY,-1,@starttime)
	--insert into TB_MP_TEMP_REPORT(ParmValue,MeasureDT,FormatType) values(cast((DATEPART(mm,@starttime1))  as varchar)+'月'+cast((DATEPART(dd,@starttime1))  as varchar)+'日',@starttime,'label')

	DECLARE CRStatus cursor for 
	select * from dbo.FUN_split(@mpstr,'|')
	open CRStatus

	FETCH  CRStatus into @mptemp

	set @mp = SUBSTRING ( @mptemp , 0 , CHARINDEX(';',@mptemp) )
	set @collectmode = SUBSTRING ( @mptemp , CHARINDEX(';',@mptemp)+1, LEN ( @mptemp )-CHARINDEX(';',@mptemp) )

	while (@@fetch_status=0)
	begin

        exec sp_report_getdata @starttime,@tempdate,@collectmode,@mp

	FETCH  CRStatus into @mptemp

	set @mp = SUBSTRING ( @mptemp , 0 , CHARINDEX(';',@mptemp) )
	set @collectmode = SUBSTRING ( @mptemp , CHARINDEX(';',@mptemp)+1, LEN ( @mptemp )-CHARINDEX(';',@mptemp) )

	end

	close CRStatus
	DEALLOCATE CRStatus
	
	set @starttime = @tempdate
end

Select * from TB_MP_TEMP_REPORT order by itemid


GO

-- ----------------------------
-- Procedure structure for sp_report_mth_05
-- ----------------------------
DROP PROCEDURE [dbo].[sp_report_mth_05]
GO



CREATE PROCEDURE [dbo].[sp_report_mth_05]
@stime varchar(10), @etime varchar(10), @starthour int,  @intval int, @mpstr varchar(800),@rownum int
 AS
declare @starttime datetime
declare @endtime datetime
declare @tempdate datetime
declare @mptemp varchar(500)
declare @collectmode varchar(50)
declare @mp varchar(500)


Select @starttime=cast(@stime as datetime)
Select @starttime=DateAdd(hour,@starthour,@starttime)
Select @endtime=cast(@etime as datetime)
Select @endtime=DateAdd(hour,@starthour,@endtime)
if(@endtime>dateadd(day,-1,getdate()))
begin
set @endtime=dateadd(day,-1,getdate())
set @endtime = dateadd(hh,-datepart(hh,@endtime),@endtime)
set @endtime = dateadd(ss,-datepart(ss,@endtime),@endtime)
set @endtime = dateadd(mi,-datepart(mi,@endtime),@endtime)
set @endtime = dateadd(ms,-datepart(ms,@endtime),@endtime)
set @endtime = dateadd(hh,@starthour,@endtime)
end

set @starttime=DateAdd(MONTH,-1,@starttime)
set @endtime=DateAdd(MONTH,-1,@endtime)

--print @starttime
--print @endtime

DROP TABLE TB_MP_TEMP_REPORT

CREATE TABLE [dbo].[TB_MP_TEMP_REPORT] (
	[ItemID] [bigint] IDENTITY (1, 1) NOT NULL ,
	[ParmValue] [varchar] (2000) COLLATE Chinese_PRC_CI_AS NOT NULL ,
	[MeasureDT] [datetime] NOT NULL ,
	[FormatType] [varchar] (50) COLLATE Chinese_PRC_CI_AS NOT NULL,
	[Decimal] [int] NOT NULL 
) ON [PRIMARY]

while(@starttime<=@endtime)
begin
	set @tempdate = dateadd(hour,@intval,@starttime)

	DECLARE CRStatus cursor for 
	select * from dbo.FUN_split(@mpstr,'|')
	open CRStatus

	FETCH  CRStatus into @mptemp

	set @mp = SUBSTRING ( @mptemp , 0 , CHARINDEX(';',@mptemp) )
	set @collectmode = SUBSTRING ( @mptemp , CHARINDEX(';',@mptemp)+1, LEN ( @mptemp )-CHARINDEX(';',@mptemp) )

	while (@@fetch_status=0)
	begin

        exec sp_report_getdata @starttime,@tempdate,@collectmode,@mp

	FETCH  CRStatus into @mptemp

	set @mp = SUBSTRING ( @mptemp , 0 , CHARINDEX(';',@mptemp) )
	set @collectmode = SUBSTRING ( @mptemp , CHARINDEX(';',@mptemp)+1, LEN ( @mptemp )-CHARINDEX(';',@mptemp) )

	end

	close CRStatus
	DEALLOCATE CRStatus
	
	set @starttime = @tempdate
end

Select * from TB_MP_TEMP_REPORT order by itemid


GO

-- ----------------------------
-- Procedure structure for sp_report_mth_06
-- ----------------------------
DROP PROCEDURE [dbo].[sp_report_mth_06]
GO



CREATE PROCEDURE [dbo].[sp_report_mth_06]
@stime varchar(10), @etime varchar(10), @starthour int,  @intval int, @mpstr varchar(800),@rownum int
 AS
declare @starttime datetime
declare @endtime datetime
declare @tempdate datetime
declare @mptemp varchar(500)
declare @collectmode varchar(50)
declare @mp varchar(500)


Select @starttime=cast(@stime as datetime)
Select @starttime=DateAdd(hour,@starthour,@starttime)
Select @endtime=cast(@etime as datetime)
Select @endtime=DateAdd(hour,@starthour,@endtime)
if(@endtime>dateadd(day,-1,getdate()))
begin
set @endtime=dateadd(day,-1,getdate())
set @endtime = dateadd(hh,-datepart(hh,@endtime),@endtime)
set @endtime = dateadd(ss,-datepart(ss,@endtime),@endtime)
set @endtime = dateadd(mi,-datepart(mi,@endtime),@endtime)
set @endtime = dateadd(ms,-datepart(ms,@endtime),@endtime)
set @endtime = dateadd(hh,@starthour,@endtime)
end

Select @endtime=DateAdd(DAY,1,@endtime)

--print @starttime
--print @endtime

DROP TABLE TB_MP_TEMP_REPORT

CREATE TABLE [dbo].[TB_MP_TEMP_REPORT] (
	[ItemID] [bigint] IDENTITY (1, 1) NOT NULL ,
	[ParmValue] [varchar] (2000) COLLATE Chinese_PRC_CI_AS NOT NULL ,
	[MeasureDT] [datetime] NOT NULL ,
	[FormatType] [varchar] (50) COLLATE Chinese_PRC_CI_AS NOT NULL,
	[Decimal] [int] NOT NULL  
) ON [PRIMARY]

while(@starttime<=@endtime)
begin
	set @tempdate = dateadd(hour,@intval,@starttime)

	insert into TB_MP_TEMP_REPORT(ParmValue,MeasureDT,FormatType,Decimal) values(cast(DATEPART(dd,@starttime)  as varchar),@starttime,'label',0)

	DECLARE CRStatus cursor for 
	select * from dbo.FUN_split(@mpstr,'|')
	open CRStatus

	FETCH  CRStatus into @mptemp

	set @mp = SUBSTRING ( @mptemp , 0 , CHARINDEX(';',@mptemp) )
	set @collectmode = SUBSTRING ( @mptemp , CHARINDEX(';',@mptemp)+1, LEN ( @mptemp )-CHARINDEX(';',@mptemp) )

	while (@@fetch_status=0)
	begin

        exec sp_report_getdata @starttime,@tempdate,@collectmode,@mp

	FETCH  CRStatus into @mptemp

	set @mp = SUBSTRING ( @mptemp , 0 , CHARINDEX(';',@mptemp) )
	set @collectmode = SUBSTRING ( @mptemp , CHARINDEX(';',@mptemp)+1, LEN ( @mptemp )-CHARINDEX(';',@mptemp) )

	end

	close CRStatus
	DEALLOCATE CRStatus
	
	set @starttime = @tempdate
end

Select * from TB_MP_TEMP_REPORT order by itemid


GO

-- ----------------------------
-- Procedure structure for sp_report_mth_07
-- ----------------------------
DROP PROCEDURE [dbo].[sp_report_mth_07]
GO



CREATE PROCEDURE [dbo].[sp_report_mth_07]
@stime varchar(10), @etime varchar(10), @starthour int,  @intval int, @mpstr varchar(800),@rownum int
 AS
declare @starttime datetime
declare @endtime datetime
declare @tempdate datetime
declare @mptemp varchar(500)
declare @collectmode varchar(50)
declare @mp varchar(500)


Select @starttime=cast(@stime as datetime)
Select @starttime=DateAdd(hour,@starthour,@starttime)
Select @endtime=cast(@etime as datetime)
Select @endtime=DateAdd(hour,@starthour,@endtime)
if(@endtime>dateadd(day,-1,getdate()))
begin
set @endtime=dateadd(day,-1,getdate())
set @endtime = dateadd(hh,-datepart(hh,@endtime),@endtime)
set @endtime = dateadd(ss,-datepart(ss,@endtime),@endtime)
set @endtime = dateadd(mi,-datepart(mi,@endtime),@endtime)
set @endtime = dateadd(ms,-datepart(ms,@endtime),@endtime)
set @endtime = dateadd(hh,@starthour,@endtime)
end

--print @starttime
--print @endtime

DROP TABLE TB_MP_TEMP_REPORT

CREATE TABLE [dbo].[TB_MP_TEMP_REPORT] (
	[ItemID] [bigint] IDENTITY (1, 1) NOT NULL ,
	[ParmValue] [varchar] (2000) COLLATE Chinese_PRC_CI_AS NOT NULL ,
	[MeasureDT] [datetime] NOT NULL ,
	[FormatType] [varchar] (50) COLLATE Chinese_PRC_CI_AS NOT NULL,
	[Decimal] [int] NOT NULL 
) ON [PRIMARY]

while(@starttime<=@endtime)
begin
	set @tempdate = dateadd(MONTH,@intval,@starttime)

	insert into TB_MP_TEMP_REPORT(ParmValue,MeasureDT,FormatType,Decimal) values(DATEPART(MM,@starttime)+1,@starttime,'label',0)
	insert into TB_MP_TEMP_REPORT(ParmValue,MeasureDT,FormatType,Decimal) values(DATEPART(YY,@starttime),@starttime,'label',0)

	DECLARE CRStatus cursor for 
	select * from dbo.FUN_split(@mpstr,'|')
	open CRStatus

	FETCH  CRStatus into @mptemp

	set @mp = SUBSTRING ( @mptemp , 0 , CHARINDEX(';',@mptemp) )
	set @collectmode = SUBSTRING ( @mptemp , CHARINDEX(';',@mptemp)+1, LEN ( @mptemp )-CHARINDEX(';',@mptemp) )

	while (@@fetch_status=0)
	begin

        exec sp_report_getdata @starttime,@tempdate,@collectmode,@mp

	FETCH  CRStatus into @mptemp

	set @mp = SUBSTRING ( @mptemp , 0 , CHARINDEX(';',@mptemp) )
	set @collectmode = SUBSTRING ( @mptemp , CHARINDEX(';',@mptemp)+1, LEN ( @mptemp )-CHARINDEX(';',@mptemp) )

	end

	close CRStatus
	DEALLOCATE CRStatus
	
	set @starttime = @tempdate
end

Select * from TB_MP_TEMP_REPORT order by itemid


GO

-- ----------------------------
-- Procedure structure for sp_report_mth_08
-- ----------------------------
DROP PROCEDURE [dbo].[sp_report_mth_08]
GO



CREATE PROCEDURE [dbo].[sp_report_mth_08]
@stime varchar(10), @etime varchar(10), @starthour int,  @intval int, @mpstr varchar(800),@rownum int
 AS
declare @starttime datetime
declare @endtime datetime
declare @tempdate datetime
declare @mptemp varchar(500)
declare @collectmode varchar(50)
declare @mp varchar(500)


Select @starttime=cast(@stime as datetime)
Select @starttime=DateAdd(hour,@starthour,@starttime)
Select @endtime=cast(@etime as datetime)
Select @endtime=DateAdd(hour,@starthour,@endtime)
if(@endtime>dateadd(day,-1,getdate()))
begin
set @endtime=dateadd(day,-1,getdate())
set @endtime = dateadd(hh,-datepart(hh,@endtime),@endtime)
set @endtime = dateadd(ss,-datepart(ss,@endtime),@endtime)
set @endtime = dateadd(mi,-datepart(mi,@endtime),@endtime)
set @endtime = dateadd(ms,-datepart(ms,@endtime),@endtime)
set @endtime = dateadd(hh,@starthour,@endtime)
end

Select @endtime=DateAdd(DAY,1,@endtime)

--print @starttime
--print @endtime

DROP TABLE TB_MP_TEMP_REPORT

CREATE TABLE [dbo].[TB_MP_TEMP_REPORT] (
	[ItemID] [bigint] IDENTITY (1, 1) NOT NULL ,
	[ParmValue] [varchar] (2000) COLLATE Chinese_PRC_CI_AS NOT NULL ,
	[MeasureDT] [datetime] NOT NULL ,
	[FormatType] [varchar] (50) COLLATE Chinese_PRC_CI_AS NOT NULL,
	[Decimal] [int] NOT NULL  
) ON [PRIMARY]

while(@starttime<=@endtime)
begin
	set @tempdate = dateadd(hour,@intval,@starttime)

	--insert into TB_MP_TEMP_REPORT(ParmValue,MeasureDT,FormatType,Decimal) values(cast(DATEPART(mm,@starttime)  as varchar)+'月'+cast(DATEPART(dd,@starttime)  as varchar)+'日',@starttime,'label',0)

	DECLARE CRStatus cursor for 
	select * from dbo.FUN_split(@mpstr,'|')
	open CRStatus

	FETCH  CRStatus into @mptemp

	set @mp = SUBSTRING ( @mptemp , 0 , CHARINDEX(';',@mptemp) )
	set @collectmode = SUBSTRING ( @mptemp , CHARINDEX(';',@mptemp)+1, LEN ( @mptemp )-CHARINDEX(';',@mptemp) )

	while (@@fetch_status=0)
	begin

        exec sp_report_getdata @starttime,@tempdate,@collectmode,@mp

	FETCH  CRStatus into @mptemp

	set @mp = SUBSTRING ( @mptemp , 0 , CHARINDEX(';',@mptemp) )
	set @collectmode = SUBSTRING ( @mptemp , CHARINDEX(';',@mptemp)+1, LEN ( @mptemp )-CHARINDEX(';',@mptemp) )

	end

	close CRStatus
	DEALLOCATE CRStatus
	
	set @starttime = @tempdate
end

Select * from TB_MP_TEMP_REPORT order by itemid


GO

-- ----------------------------
-- Procedure structure for sp_report_mth_081
-- ----------------------------
DROP PROCEDURE [dbo].[sp_report_mth_081]
GO



CREATE PROCEDURE [dbo].[sp_report_mth_081]
@stime varchar(10), @etime varchar(10), @starthour int,  @intval int, @mpstr varchar(1000),@rownum int
 AS
declare @starttime datetime
declare @endtime datetime
declare @tempdate datetime
declare @mptemp varchar(500)
declare @collectmode varchar(50)
declare @mp varchar(500)


Select @starttime=cast(@stime as datetime)
Select @starttime=DateAdd(hour,@starthour,@starttime)
Select @endtime=cast(@etime as datetime)
Select @endtime=DateAdd(hour,@starthour,@endtime)
if(@endtime>dateadd(day,-1,getdate()))
begin
set @endtime=dateadd(day,-1,getdate())
set @endtime = dateadd(hh,-datepart(hh,@endtime),@endtime)
set @endtime = dateadd(ss,-datepart(ss,@endtime),@endtime)
set @endtime = dateadd(mi,-datepart(mi,@endtime),@endtime)
set @endtime = dateadd(ms,-datepart(ms,@endtime),@endtime)
set @endtime = dateadd(hh,@starthour,@endtime)
end

--Select @endtime=DateAdd(DAY,1,@endtime)

--print @starttime
--print @endtime

DROP TABLE TB_MP_TEMP_REPORT

CREATE TABLE [dbo].[TB_MP_TEMP_REPORT] (
	[ItemID] [bigint] IDENTITY (1, 1) NOT NULL ,
	[ParmValue] [varchar] (2000) COLLATE Chinese_PRC_CI_AS NOT NULL ,
	[MeasureDT] [datetime] NOT NULL ,
	[FormatType] [varchar] (50) COLLATE Chinese_PRC_CI_AS NOT NULL,
	[Decimal] [int] NOT NULL  
) ON [PRIMARY]

while(@starttime<@endtime)
begin
	set @tempdate = dateadd(hour,@intval,@starttime)

	--insert into TB_MP_TEMP_REPORT(ParmValue,MeasureDT,FormatType,Decimal) values(cast(DATEPART(mm,@starttime)  as varchar)+'月'+cast(DATEPART(dd,@starttime)  as varchar)+'日',@starttime,'label',0)

	DECLARE CRStatus cursor for 
	select * from dbo.FUN_split(@mpstr,'|')
	open CRStatus

	FETCH  CRStatus into @mptemp

	set @mp = SUBSTRING ( @mptemp , 0 , CHARINDEX(';',@mptemp) )
	set @collectmode = SUBSTRING ( @mptemp , CHARINDEX(';',@mptemp)+1, LEN ( @mptemp )-CHARINDEX(';',@mptemp) )

	while (@@fetch_status=0)
	begin

        exec sp_report_getdata @starttime,@tempdate,@collectmode,@mp

	FETCH  CRStatus into @mptemp

	set @mp = SUBSTRING ( @mptemp , 0 , CHARINDEX(';',@mptemp) )
	set @collectmode = SUBSTRING ( @mptemp , CHARINDEX(';',@mptemp)+1, LEN ( @mptemp )-CHARINDEX(';',@mptemp) )

	end

	close CRStatus
	DEALLOCATE CRStatus
	
	set @starttime = @tempdate
end

Select * from TB_MP_TEMP_REPORT order by itemid


GO

-- ----------------------------
-- Procedure structure for sp_report_mth_09
-- ----------------------------
DROP PROCEDURE [dbo].[sp_report_mth_09]
GO



CREATE PROCEDURE [dbo].[sp_report_mth_09]
@stime varchar(10), @etime varchar(10), @starthour int,  @intval int, @mpstr varchar(800),@rownum int
 AS
declare @starttime datetime
declare @endtime datetime
declare @tempdate datetime
declare @mptemp varchar(500)
declare @collectmode varchar(50)
declare @mp varchar(500)


Select @starttime=cast(@stime as datetime)
Select @starttime=DateAdd(hour,@starthour,@starttime)
Select @endtime=cast(@etime as datetime)
Select @endtime=DateAdd(hour,@starthour,@endtime)
if(@endtime>dateadd(day,-1,getdate()))
begin
set @endtime=dateadd(day,-1,getdate())
set @endtime = dateadd(hh,-datepart(hh,@endtime),@endtime)
set @endtime = dateadd(ss,-datepart(ss,@endtime),@endtime)
set @endtime = dateadd(mi,-datepart(mi,@endtime),@endtime)
set @endtime = dateadd(ms,-datepart(ms,@endtime),@endtime)
set @endtime = dateadd(hh,@starthour,@endtime)
end

--print @starttime
--print @endtime

DROP TABLE TB_MP_TEMP_REPORT

CREATE TABLE [dbo].[TB_MP_TEMP_REPORT] (
	[ItemID] [bigint] IDENTITY (1, 1) NOT NULL ,
	[ParmValue] [varchar] (2000) COLLATE Chinese_PRC_CI_AS NOT NULL ,
	[MeasureDT] [datetime] NOT NULL ,
	[FormatType] [varchar] (50) COLLATE Chinese_PRC_CI_AS NOT NULL,
	[Decimal] [int] NOT NULL 
) ON [PRIMARY]

while(@starttime<=@endtime)
begin
	set @tempdate = dateadd(hour,@intval,@starttime)

	insert into TB_MP_TEMP_REPORT(ParmValue,MeasureDT,FormatType,Decimal) values(DATEPART(dd,@starttime),@starttime,'dateNumber',0)

	DECLARE CRStatus cursor for 
	select * from dbo.FUN_split(@mpstr,'|')
	open CRStatus

	FETCH  CRStatus into @mptemp

	set @mp = SUBSTRING ( @mptemp , 0 , CHARINDEX(';',@mptemp) )
	set @collectmode = SUBSTRING ( @mptemp , CHARINDEX(';',@mptemp)+1, LEN ( @mptemp )-CHARINDEX(';',@mptemp) )

	while (@@fetch_status=0)
	begin

        exec sp_report_getdata @starttime,@tempdate,@collectmode,@mp

	FETCH  CRStatus into @mptemp

	set @mp = SUBSTRING ( @mptemp , 0 , CHARINDEX(';',@mptemp) )
	set @collectmode = SUBSTRING ( @mptemp , CHARINDEX(';',@mptemp)+1, LEN ( @mptemp )-CHARINDEX(';',@mptemp) )

	end

	close CRStatus
	DEALLOCATE CRStatus
	
	set @starttime = @tempdate
end

Select * from TB_MP_TEMP_REPORT order by itemid


GO

-- ----------------------------
-- Procedure structure for sp_report_mth_091
-- ----------------------------
DROP PROCEDURE [dbo].[sp_report_mth_091]
GO



CREATE PROCEDURE [dbo].[sp_report_mth_091]
@stime varchar(10), @etime varchar(10), @starthour int,  @intval int, @mpstr varchar(800),@rownum int
 AS
declare @starttime datetime
declare @endtime datetime
declare @tempdate datetime
declare @mptemp varchar(500)
declare @collectmode varchar(50)
declare @mp varchar(500)


Select @starttime=cast(@stime as datetime)
Select @starttime=DateAdd(hour,@starthour,@starttime)
Select @endtime=cast(@etime as datetime)
Select @endtime=DateAdd(hour,@starthour,@endtime)
if(@endtime>dateadd(day,-1,getdate()))
begin
set @endtime=dateadd(day,-1,getdate())
set @endtime = dateadd(hh,-datepart(hh,@endtime),@endtime)
set @endtime = dateadd(ss,-datepart(ss,@endtime),@endtime)
set @endtime = dateadd(mi,-datepart(mi,@endtime),@endtime)
set @endtime = dateadd(ms,-datepart(ms,@endtime),@endtime)
set @endtime = dateadd(hh,@starthour,@endtime)
end

--print @starttime
--print @endtime

DROP TABLE TB_MP_TEMP_REPORT

CREATE TABLE [dbo].[TB_MP_TEMP_REPORT] (
	[ItemID] [bigint] IDENTITY (1, 1) NOT NULL ,
	[ParmValue] [varchar] (2000) COLLATE Chinese_PRC_CI_AS NOT NULL ,
	[MeasureDT] [datetime] NOT NULL ,
	[FormatType] [varchar] (50) COLLATE Chinese_PRC_CI_AS NOT NULL,
	[Decimal] [int] NOT NULL 
) ON [PRIMARY]

while(@starttime<@endtime)
begin
	set @tempdate = dateadd(hour,@intval,@starttime)

	insert into TB_MP_TEMP_REPORT(ParmValue,MeasureDT,FormatType,Decimal) values(DATEPART(dd,@starttime),@starttime,'dateNumber',0)

	DECLARE CRStatus cursor for 
	select * from dbo.FUN_split(@mpstr,'|')
	open CRStatus

	FETCH  CRStatus into @mptemp

	set @mp = SUBSTRING ( @mptemp , 0 , CHARINDEX(';',@mptemp) )
	set @collectmode = SUBSTRING ( @mptemp , CHARINDEX(';',@mptemp)+1, LEN ( @mptemp )-CHARINDEX(';',@mptemp) )

	while (@@fetch_status=0)
	begin

        exec sp_report_getdata @starttime,@tempdate,@collectmode,@mp

	FETCH  CRStatus into @mptemp

	set @mp = SUBSTRING ( @mptemp , 0 , CHARINDEX(';',@mptemp) )
	set @collectmode = SUBSTRING ( @mptemp , CHARINDEX(';',@mptemp)+1, LEN ( @mptemp )-CHARINDEX(';',@mptemp) )

	end

	close CRStatus
	DEALLOCATE CRStatus
	
	set @starttime = @tempdate
end

Select * from TB_MP_TEMP_REPORT order by itemid


GO

-- ----------------------------
-- Procedure structure for sp_report_qua_01
-- ----------------------------
DROP PROCEDURE [dbo].[sp_report_qua_01]
GO



CREATE PROCEDURE [dbo].[sp_report_qua_01]
@stime varchar(10), @etime varchar(10), @starthour int,  @intval int, @mpstr varchar(5000),@rownum int,@userid varchar(50)
 AS
declare @starttime datetime
declare @endtime datetime
declare @tempdate datetime
declare @mptemp varchar(500)
declare @collectmode varchar(50)
declare @mp varchar(500)


Select @starttime=cast(@stime as datetime)
Select @starttime=DateAdd(hour,@starthour,@starttime)
-- Select @starttime=DateAdd(MONTH,-1,@starttime)
Select @starttime=DateAdd(MONTH,0,@starttime)
Select @endtime=cast(@etime as datetime)
Select @endtime=DateAdd(hour,@starthour,@endtime)
if(@endtime>dateadd(day,-1,getdate()))
begin
set @endtime=dateadd(day,0,getdate())
end

-- DROP TABLE TB_MP_TEMP_REPORT
-- 
-- CREATE TABLE [dbo].[TB_MP_TEMP_REPORT] (
-- 	[ItemID] [bigint] IDENTITY (1, 1) NOT NULL ,
-- 	[ParmValue] [varchar] (2000) COLLATE Chinese_PRC_CI_AS NOT NULL ,
-- 	[MeasureDT] [datetime] NOT NULL ,
-- 	[FormatType] [varchar] (50) COLLATE Chinese_PRC_CI_AS NOT NULL,
-- 	[Decimal] [int] 
-- ) ON [PRIMARY]

while(@starttime<@endtime)
begin
	set @tempdate = dateadd(hour,@intval,@starttime)
	--if(DATEPART(dd,@starttime)=1)
	--begin
	--insert into TB_MP_TEMP_REPORT(ParmValue,MeasureDT,FormatType) values(cast(DATEPART(mm,@starttime) as varchar)+'月',@starttime,'label')
	--end
	--else
	--begin
	--insert into TB_MP_TEMP_REPORT(ParmValue,MeasureDT,FormatType) values(cast(DATEPART(mm,dateadd(mm,1,@starttime)) as varchar)+'月',@starttime,'label')
	--end
	
	DECLARE CRStatus cursor for 
	select * from dbo.FUN_split(@mpstr,'|')
	open CRStatus

	FETCH  CRStatus into @mptemp

	set @mp = SUBSTRING ( @mptemp , 0 , CHARINDEX(';',@mptemp) )
	set @collectmode = SUBSTRING ( @mptemp , CHARINDEX(';',@mptemp)+1, LEN ( @mptemp )-CHARINDEX(';',@mptemp) )

	while (@@fetch_status=0)
	begin
		--print 'aaaaaa'
		--print @starttime
		--print @tempdate
		--print @collectmode
		--print @mp
		--print @userid


		print 'bbbbbb'
        exec sp_report_getdata @starttime,@tempdate,@collectmode,@mp,@userid

	FETCH  CRStatus into @mptemp

	set @mp = SUBSTRING ( @mptemp , 0 , CHARINDEX(';',@mptemp) )
	set @collectmode = SUBSTRING ( @mptemp , CHARINDEX(';',@mptemp)+1, LEN ( @mptemp )-CHARINDEX(';',@mptemp) )

	end

	close CRStatus
	DEALLOCATE CRStatus
	
	set @starttime = @tempdate
end

Select * from TB_MP_TEMP_REPORT where userid=@userid order by itemid


GO

-- ----------------------------
-- Procedure structure for sp_report_runrecord
-- ----------------------------
DROP PROCEDURE [dbo].[sp_report_runrecord]
GO



CREATE PROCEDURE [dbo].[sp_report_runrecord] 
@starttime varchar(20),@endtime varchar(20),@type varchar(80),@mp varchar(800),@rownum int
as
Declare @strSQL1 nvarchar(4000)
Declare @strSQL2 nvarchar(4000)
Declare @strSQL3 nvarchar(4000)

declare @count int
declare @ParmValue money
declare @MeasureDT datetime
declare @lastParmValue money
declare @lastMeasureDT datetime
declare @colcount int
declare @i int

declare @idx int
declare @idxo int

declare @mainmp varchar(200)
declare @mp1 varchar(200)
declare @mp2 varchar(200)
declare @mp3 varchar(200)
declare @mp4 varchar(200)
declare @mp5 varchar(200)
declare @mp6 varchar(200)
declare @mp7 varchar(200)
declare @mp8 varchar(200)
declare @mp9 varchar(200)
declare @mp10 varchar(200)

--delete from TB_MP_TEMP_REPORT
select @colcount = count(*) from dbo.FUN_split(@mp,',') 
set @i = 1

if(SUBSTRING(@mp,1,1)!='-')
	begin

set @idx = charindex(',',@mp,1)
if(@idx>0) 
begin
	set @mainmp = SUBSTRING(@mp,1,@idx-1)
	print @mainmp

	set @mp= SUBSTRING(@mp,@idx+1,len(@mp)) 
	print @mp

	set @idx = charindex(',',@mp,1)
	if(@idx>0)
	begin
		set @mp1 = SUBSTRING(@mp,1,@idx-1)
		print @mp1	


		set @mp= SUBSTRING(@mp,@idx+1,len(@mp)) 
		print @mp
	
		set @idx = charindex(',',@mp,1)
		if(@idx>0)
		begin
			set @mp2 = SUBSTRING(@mp,1,@idx-1)
			print @mp2	
	
			set @mp= SUBSTRING(@mp,@idx+1,len(@mp)) 
			print @mp


			set @idx = charindex(',',@mp,1)
			if(@idx>0)
			begin
				set @mp3 = SUBSTRING(@mp,1,@idx-1)
				print @mp3	
			
				set @mp= SUBSTRING(@mp,@idx+1,len(@mp)) 
				print @mp
	
				set @idx = charindex(',',@mp,1)
				if(@idx>0)
				begin
					set @mp4 = SUBSTRING(@mp,1,@idx-1)
					print @mp4	
			
					set @mp= SUBSTRING(@mp,@idx+1,len(@mp)) 
					print @mp

					set @idx = charindex(',',@mp,1)
					if(@idx>0)
					begin
						set @mp5 = SUBSTRING(@mp,1,@idx-1)
						print @mp5	
				
						set @mp= SUBSTRING(@mp,@idx+1,len(@mp)) 
						print @mp
	
						set @idx = charindex(',',@mp,1)
						if(@idx>0)
						begin
							set @mp6 = SUBSTRING(@mp,1,@idx-1)
							print @mp6	
						end 
						else
						begin
							set @mp6 = @mp
							print @mp6	
						end
	
					end
					else
					begin
						set @mp5 = @mp
						print @mp5	
					end
				end
				else
				begin
					set @mp4 = @mp
					print @mp4	
				end
			end
			else
			begin
				set @mp3 = @mp
				print @mp3	
			end
	
		end
		else
		begin
			set @mp2= @mp
			print @mp2	
		end

	end
	else
	begin
		set @mp1 = @mp
		print @mp1	
	end
end
else
begin
	set @mainmp=@mp
	print @mainmp
end



SET @strSQL1 = N''
SET @strSQL1 = @strSQL1+N'declare @count int'+char(10)
SET @strSQL1 = @strSQL1+N'declare @ParmValue money'+char(10)
SET @strSQL1 = @strSQL1+N'declare @MeasureDT datetime'+char(10)
SET @strSQL1 = @strSQL1+N'declare @lastParmValue money'+char(10)
SET @strSQL1 = @strSQL1+N'declare @lastMeasureDT datetime'+char(10)
SET @strSQL1 = @strSQL1+N'declare @str nvarchar(4000)'+char(10)

--寻找今天第一条记录
SET @strSQL1 = @strSQL1 + N'select top 1 @lastParmValue =ParmValue ,@lastMeasureDT=MeasureDT from TB_MP_'+@mainmp+N' where measuredt between '''+@starttime+N''' and ''' +@endtime+ N''' order by measuredt asc ' +char(10)
if(@type='+')
begin
	--如果第一天记录代表运行
	SET @strSQL1 = @strSQL1 + N'if(@lastParmValue=1)'+char(10)
	SET @strSQL1 = @strSQL1 + N'begin'+char(10)
	--寻找今天之前最后一条记录
	SET @strSQL1 = @strSQL1 + N'select top 1 @lastParmValue =ParmValue ,@lastMeasureDT=MeasureDT from TB_MP_'+@mainmp+N' where measuredt < '''+@starttime+N'''  order by measuredt desc' +char(10)
	--如果记录存在且为运行
	SET @strSQL1 = @strSQL1 + N'if(not @lastParmValue is null and @lastParmValue=1)'+char(10)
	SET @strSQL1 = @strSQL1 + N'begin'+char(10)
	--做一条假的开机记录
	SET @strSQL1 = @strSQL1 + N'set @lastMeasureDT='''+@starttime+''''+char(10)
	SET @strSQL1 = @strSQL1 + N'set @lastParmValue=1 '+char(10)
	SET @strSQL1  = @strSQL1 + N'end'+char(10)
	SET @strSQL1  = @strSQL1 + N'end'+char(10)
end
else
begin
	SET @strSQL1 = @strSQL1 + N'if(@lastParmValue=0)'+char(10)
	SET @strSQL1 = @strSQL1 + N'begin'+char(10)
	SET @strSQL1 = @strSQL1 + N'select top 1 @lastParmValue =ParmValue ,@lastMeasureDT=MeasureDT from TB_MP_'+@mainmp+N' where measuredt < '''+@starttime+N''' order by measuredt desc' +char(10)
	--如果记录存在且为运行
	SET @strSQL1 = @strSQL1 + N'if(not @lastParmValue is null and @lastParmValue=0)'+char(10)
	SET @strSQL1 = @strSQL1 + N'begin'+char(10)
	--做一条假的开机记录
	SET @strSQL1 = @strSQL1 + N'set @lastMeasureDT='''+@starttime+''''+char(10)
	SET @strSQL1 = @strSQL1 + N'set @lastParmValue=0 '+char(10)
	SET @strSQL1  = @strSQL1 + N'end'+char(10)
	SET @strSQL1  = @strSQL1 + N'end'+char(10)
end

SET @strSQL1 = @strSQL1 + N'set @count=0'+char(10)

SET @strSQL1 = @strSQL1 + N'DECLARE curRunStatus cursor for select ParmValue,MeasureDT from TB_MP_' + @mainmp+N' ' +char(10)
SET @strSQL1 = @strSQL1 + N'where measuredt between ''' +cast(@starttime as varchar(20)) +N''' and '''+cast(@endtime as varchar(20))+N''' order by MeasureDT asc'+char(10)
SET @strSQL1 = @strSQL1 + N'open curRunStatus'+char(10)
SET @strSQL1 = @strSQL1 + N'FETCH curRunStatus into @ParmValue,@MeasureDT'+char(10)
SET @strSQL1 = @strSQL1 + N'while (@@fetch_status=0)'+char(10)
SET @strSQL1 = @strSQL1 + N'begin'+char(10)
SET @strSQL1 = @strSQL1 + N'if(@lastParmValue is null)'+char(10)
SET @strSQL1 = @strSQL1 + N'begin'+char(10)
SET @strSQL1 = @strSQL1 + N'set @lastParmValue = @ParmValue'+char(10)	
SET @strSQL1 = @strSQL1 + N'set @lastMeasureDT = @MeasureDT'+char(10)	
SET @strSQL1 = @strSQL1 + N'end'+char(10)

SET @strSQL1 = @strSQL1 + N'if(@ParmValue<>@lastParmValue)'+char(10)
SET @strSQL1 = @strSQL1 + N'begin'+char(10)
SET @strSQL1 = @strSQL1 + N'set  @count=@count+1'+char(10)	

if(@type='+')
SET @strSQL1  = @strSQL1 + N'if(@ParmValue=0)'++char(10)	
else
SET @strSQL1  = @strSQL1 + N'if(@ParmValue=1)'++char(10)	

SET @strSQL1  = @strSQL1 + N'begin'+char(10)
SET @strSQL1  = @strSQL1 + N'insert into TB_MP_TEMP_REPORT(ParmValue,MeasureDT,FormatType) values(''停止'',''2009-06-15'',''label'')'+char(10)

if(not @mp1 is null)
begin
	if(@mp1='-')
	begin
		SET @strSQL1  = @strSQL1 + N'insert into TB_MP_TEMP_REPORT(ParmValue,MeasureDT,FormatType) values(''-'',''2009-06-15'',''label'')'+char(10)	
	end
	if(@mp1='MeasureDT')
	begin
		SET @strSQL1  = @strSQL1 + N'insert into TB_MP_TEMP_REPORT(ParmValue,MeasureDT,FormatType) values(''''+cast(datepart(hh,@MeasureDT) as varchar)+'':''+cast(datepart(mi,@MeasureDT) as varchar)+'''',''2009-06-15'',''datetime'')'+char(10)	
	end
	if(@mp1='dur')
	begin

		SET @strSQL1  = @strSQL1 + N'insert into TB_MP_TEMP_REPORT(ParmValue,MeasureDT,FormatType) values(''''+cast(datediff(mi,@lastMeasureDT,@MeasureDT)/60 as varchar)+'':''+cast(datediff(mi,@lastMeasureDT,@MeasureDT)%60 as varchar)+'''',''2009-06-15'',''datetime'')'+char(10)
	end
	if(@mp1<>'dur' and @mp1<>'MeasureDT' and @mp1<>'-')
	begin
		SET @strSQL1  = @strSQL1 + N'SET @str = N'''''+char(10)
		SET @strSQL1  = @strSQL1 + N'SET @str = @str + N''if exists( select ParmValue , MeasureDT from tb_mp_'+@mp1+' where measuredt>=''''''+cast(@MeasureDT as varchar)+'''''') insert into TB_MP_TEMP_REPORT(ParmValue,MeasureDT,FormatType) select top 1 cast(ParmValue as varchar), MeasureDT,''number'' from tb_mp_'+@mp1+' where measuredt>=''''''+cast(@MeasureDT as varchar)+'''''' order by measuredt '''+char(10)
		SET @strSQL1  = @strSQL1 + N'SET @str = @str + N'' else   insert into TB_MP_TEMP_REPORT(ParmValue,MeasureDT,FormatType)  values(''''-'''',''''2009-06-15'''',''''label'''')'''+char(10)
		--SET @strSQl  = @strSQL + N'print @str'+char(10)
		SET @strSQL1  = @strSQL1 + N'exec(@str)'+char(10)
	end
end	
if(not @mp2 is null)
begin
	if(@mp2='-')
	begin
		SET @strSQL1  = @strSQL1 + N'insert into TB_MP_TEMP_REPORT(ParmValue,MeasureDT,FormatType) values(''-'',''2009-06-15'',''label'')'+char(10)	
	end
	if(@mp2='MeasureDT')
	begin
		SET @strSQL1  = @strSQL1 + N'insert into TB_MP_TEMP_REPORT(ParmValue,MeasureDT,FormatType) values(''''+cast(datepart(hh,@MeasureDT) as varchar)+'':''+cast(datepart(mi,@MeasureDT) as varchar)+'''',''2009-06-15'',''datetime'')'+char(10)	
	end
	if(@mp2='dur')
	begin
		SET @strSQL1  = @strSQL1 + N'insert into TB_MP_TEMP_REPORT(ParmValue,MeasureDT,FormatType) values(''''+cast(datediff(mi,@lastMeasureDT,@MeasureDT)/60 as varchar)+'':''+cast(datediff(mi,@lastMeasureDT,@MeasureDT)%60 as varchar)+'''',''2009-06-15'',''datetime'')'+char(10)
	end
	if(@mp2<>'dur' and @mp2<>'MeasureDT' and @mp2<>'-')
	begin
		SET @strSQL1  = @strSQL1 + N'SET @str = N'''''+char(10)
		SET @strSQL1  = @strSQL1 + N'SET @str = @str + N''if exists( select ParmValue , MeasureDT from tb_mp_'+@mp2+' where measuredt>=''''''+cast(@MeasureDT as varchar)+'''''') insert into TB_MP_TEMP_REPORT(ParmValue,MeasureDT,FormatType) select top 1 cast(ParmValue as varchar), MeasureDT,''number'' from tb_mp_'+@mp2+' where measuredt>=''''''+cast(@MeasureDT as varchar)+'''''' order by measuredt '''+char(10)
		SET @strSQL1  = @strSQL1 + N'SET @str = @str + N'' else   insert into TB_MP_TEMP_REPORT(ParmValue,MeasureDT,FormatType)  values(''''-'''',''''2009-06-15'''',''''label'''')'''+char(10)
		--SET @strSQl  = @strSQL + N'print @str'+char(10)
		SET @strSQL1  = @strSQL1 + N'exec(@str)'+char(10)
	end
end	
if(not @mp3 is null)
begin
	if(@mp3='-')
	begin
		SET @strSQL1  = @strSQL1 + N'insert into TB_MP_TEMP_REPORT(ParmValue,MeasureDT,FormatType) values(''-'',''2009-06-15'',''label'')'+char(10)	
	end
	if(@mp3='MeasureDT')
	begin
		SET @strSQL1  = @strSQL1 + N'insert into TB_MP_TEMP_REPORT(ParmValue,MeasureDT,FormatType) values(''''+cast(datepart(hh,@MeasureDT) as varchar)+'':''+cast(datepart(mi,@MeasureDT) as varchar)+'''',''2009-06-15'',''datetime'')'+char(10)	
	end
	if(@mp3='dur')
	begin
		SET @strSQL1  = @strSQL1 + N'insert into TB_MP_TEMP_REPORT(ParmValue,MeasureDT,FormatType) values(''''+cast(datediff(mi,@lastMeasureDT,@MeasureDT)/60 as varchar)+'':''+cast(datediff(mi,@lastMeasureDT,@MeasureDT)%60 as varchar)+'''',''2009-06-15'',''datetime'')'+char(10)
	end
	if(@mp3<>'dur' and @mp3<>'MeasureDT' and @mp3<>'-')
	begin
		SET @strSQL1  = @strSQL1 + N'SET @str = N'''''+char(10)
		SET @strSQL1  = @strSQL1 + N'SET @str = @str + N''if exists( select ParmValue , MeasureDT from tb_mp_'+@mp3+' where measuredt>=''''''+cast(@MeasureDT as varchar)+'''''') insert into TB_MP_TEMP_REPORT(ParmValue,MeasureDT,FormatType) select top 1 cast(ParmValue as varchar), MeasureDT,''number'' from tb_mp_'+@mp3+' where measuredt>=''''''+cast(@MeasureDT as varchar)+'''''' order by measuredt '''+char(10)
		SET @strSQL1  = @strSQL1 + N'SET @str = @str + N'' else   insert into TB_MP_TEMP_REPORT(ParmValue,MeasureDT,FormatType)  values(''''-'''',''''2009-06-15'''',''''label'''')'''+char(10)
		--SET @strSQl  = @strSQL + N'print @str'+char(10)
		SET @strSQL1  = @strSQL1 + N'exec(@str)'+char(10)
	end
end	
if(not @mp4 is null)
begin
	if(@mp4='-')
	begin
		SET @strSQL1  = @strSQL1 + N'insert into TB_MP_TEMP_REPORT(ParmValue,MeasureDT,FormatType) values(''-'',''2009-06-15'',''label'')'+char(10)	
	end
	if(@mp4='MeasureDT')
	begin
		SET @strSQL1  = @strSQL1 + N'insert into TB_MP_TEMP_REPORT(ParmValue,MeasureDT,FormatType) values(''''+cast(datepart(hh,@MeasureDT) as varchar)+'':''+cast(datepart(mi,@MeasureDT) as varchar)+'''',''2009-06-15'',''datetime'')'+char(10)	
	end
	if(@mp4='dur')
	begin
		SET @strSQL1  = @strSQL1 + N'insert into TB_MP_TEMP_REPORT(ParmValue,MeasureDT,FormatType) values(''''+cast(datediff(mi,@lastMeasureDT,@MeasureDT)/60 as varchar)+'':''+cast(datediff(mi,@lastMeasureDT,@MeasureDT)%60 as varchar)+'''',''2009-06-15'',''datetime'')'+char(10)
	end
	if(@mp4<>'dur' and @mp4<>'MeasureDT' and @mp4<>'-')
	begin
		SET @strSQL1  = @strSQL1 + N'SET @str = N'''''+char(10)
		SET @strSQL1  = @strSQL1 + N'SET @str = @str + N''if exists( select ParmValue , MeasureDT from tb_mp_'+@mp4+' where measuredt>=''''''+cast(@MeasureDT as varchar)+'''''') insert into TB_MP_TEMP_REPORT(ParmValue,MeasureDT,FormatType) select top 1 cast(ParmValue as varchar), MeasureDT,''number'' from tb_mp_'+@mp4+' where measuredt>=''''''+cast(@MeasureDT as varchar)+'''''' order by measuredt '''+char(10)
		SET @strSQL1  = @strSQL1 + N'SET @str = @str + N'' else   insert into TB_MP_TEMP_REPORT(ParmValue,MeasureDT,FormatType)  values(''''-'''',''''2009-06-15'''',''''label'''')'''+char(10)
		--SET @strSQl  = @strSQL + N'print @str'+char(10)
		SET @strSQL1  = @strSQL1 + N'exec(@str)'+char(10)
	end
end	
if(not @mp5 is null)
begin
	if(@mp5='-')
	begin
		SET @strSQL1  = @strSQL1 + N'insert into TB_MP_TEMP_REPORT(ParmValue,MeasureDT,FormatType) values(''-'',''2009-06-15'',''label'')'+char(10)	
	end
	if(@mp5='MeasureDT')
	begin
		SET @strSQL1  = @strSQL1 + N'insert into TB_MP_TEMP_REPORT(ParmValue,MeasureDT,FormatType) values(''''+cast(datepart(hh,@MeasureDT) as varchar)+'':''+cast(datepart(mi,@MeasureDT) as varchar)+'''',''2009-06-15'',''datetime'')'+char(10)	
	end
	if(@mp5='dur')
	begin
		SET @strSQL1  = @strSQL1 + N'insert into TB_MP_TEMP_REPORT(ParmValue,MeasureDT,FormatType) values(''''+cast(datediff(mi,@lastMeasureDT,@MeasureDT)/60 as varchar)+'':''+cast(datediff(mi,@lastMeasureDT,@MeasureDT)%60 as varchar)+'''',''2009-06-15'',''datetime'')'+char(10)
	end
	if(@mp5<>'dur' and @mp5<>'MeasureDT' and @mp5<>'-')
	begin
		SET @strSQL1  = @strSQL1 + N'SET @str = N'''''+char(10)
		SET @strSQL1  = @strSQL1 + N'SET @str = @str + N''if exists( select ParmValue , MeasureDT from tb_mp_'+@mp5+' where measuredt>=''''''+cast(@MeasureDT as varchar)+'''''') insert into TB_MP_TEMP_REPORT(ParmValue,MeasureDT,FormatType) select top 1 cast(ParmValue as varchar), MeasureDT,''number'' from tb_mp_'+@mp5+' where measuredt>=''''''+cast(@MeasureDT as varchar)+'''''' order by measuredt '''+char(10)
		SET @strSQL1  = @strSQL1 + N'SET @str = @str + N'' else   insert into TB_MP_TEMP_REPORT(ParmValue,MeasureDT,FormatType)  values(''''-'''',''''2009-06-15'''',''''label'''')'''+char(10)
		--SET @strSQl  = @strSQL + N'print @str'+char(10)
		SET @strSQL1  = @strSQL1 + N'exec(@str)'+char(10)
	end
end	
if(not @mp6 is null)
begin
	if(@mp6='-')
	begin
		SET @strSQL1  = @strSQL1 + N'insert into TB_MP_TEMP_REPORT(ParmValue,MeasureDT,FormatType) values(''-'',''2009-06-15'',''label'')'+char(10)	
	end
	if(@mp6='MeasureDT')
	begin
		SET @strSQL1  = @strSQL1 + N'insert into TB_MP_TEMP_REPORT(ParmValue,MeasureDT,FormatType) values(''''+cast(datepart(hh,@MeasureDT) as varchar)+'':''+cast(datepart(mi,@MeasureDT) as varchar)+'''',''2009-06-15'',''datetime'')'+char(10)	
	end
	if(@mp6='dur')
	begin
		SET @strSQL1  = @strSQL1 + N'insert into TB_MP_TEMP_REPORT(ParmValue,MeasureDT,FormatType) values(''''+cast(datediff(mi,@lastMeasureDT,@MeasureDT)/60 as varchar)+'':''+cast(datediff(mi,@lastMeasureDT,@MeasureDT)%60 as varchar)+'''',''2009-06-15'',''datetime'')'+char(10)
	end
	if(@mp6<>'dur' and @mp6<>'MeasureDT' and @mp6<>'-')
	begin
		SET @strSQL1  = @strSQL1 + N'SET @str = N'''''+char(10)
		SET @strSQL1  = @strSQL1 + N'SET @str = @str + N''if exists( select ParmValue , MeasureDT from tb_mp_'+@mp6+' where measuredt>=''''''+cast(@MeasureDT as varchar)+'''''') insert into TB_MP_TEMP_REPORT(ParmValue,MeasureDT,FormatType) select top 1 cast(ParmValue as varchar), MeasureDT,''number'' from tb_mp_'+@mp6+' where measuredt>=''''''+cast(@MeasureDT as varchar)+'''''' order by measuredt '''+char(10)
		SET @strSQL1  = @strSQL1 + N'SET @str = @str + N'' else   insert into TB_MP_TEMP_REPORT(ParmValue,MeasureDT,FormatType)  values(''''-'''',''''2009-06-15'''',''''label'''')'''+char(10)
		--SET @strSQl  = @strSQL + N'print @str'+char(10)
		SET @strSQL1  = @strSQL1 + N'exec(@str)'+char(10)
	end
end	
SET @strSQL1  = @strSQL1 + N'end'+char(10)

SET @strSQL2 = N''
if(@type='+')	
SET @strSQL2  = @strSQL2 + N'if(@ParmValue=1)'+char(10)	
else
SET @strSQL2  = @strSQL2 + N'if(@ParmValue=0)'+char(10)	

SET @strSQL2  = @strSQL2 + N'begin'+char(10)
SET @strSQL2  = @strSQL2 + N'insert into TB_MP_TEMP_REPORT(ParmValue,MeasureDT,FormatType) values(''启动'',''2009-06-15'',''label'')'+char(10)	
if(not @mp1 is null)
begin
	if(@mp1='-')
	begin
		SET @strSQL2  = @strSQL2 + N'insert into TB_MP_TEMP_REPORT(ParmValue,MeasureDT,FormatType) values(''-'',''2009-06-15'',''label'')'+char(10)	
	end
	if(@mp1='MeasureDT')
	begin
		SET @strSQL2  = @strSQL2 + N'insert into TB_MP_TEMP_REPORT(ParmValue,MeasureDT,FormatType) values(''''+cast(datepart(hh,@MeasureDT) as varchar)+'':''+cast(datepart(mi,@MeasureDT) as varchar)+'''',''2009-06-15'',''datetime'')'+char(10)	
	end
	if(@mp1='dur')
	begin
		SET @strSQL2  = @strSQL2 + N'insert into TB_MP_TEMP_REPORT(ParmValue,MeasureDT,FormatType) values(''-'',''2009-06-15'',''label'')'+char(10)
	end
	if(@mp1<>'dur' and @mp1<>'MeasureDT' and @mp1<>'-')
	begin
		SET @strSQL2  = @strSQL2 + N'SET @str = N'''''+char(10)
		SET @strSQL2  = @strSQL2 + N'SET @str = @str + N''if exists( select ParmValue , MeasureDT from tb_mp_'+@mp1+' where measuredt>=''''''+cast(@MeasureDT as varchar)+'''''') insert into TB_MP_TEMP_REPORT(ParmValue,MeasureDT,FormatType) select top 1 cast(ParmValue as varchar), MeasureDT,''number'' from tb_mp_'+@mp1+' where measuredt>=''''''+cast(@MeasureDT as varchar)+'''''' order by measuredt '''+char(10)
		SET @strSQL2  = @strSQL2 + N'SET @str = @str + N'' else   insert into TB_MP_TEMP_REPORT(ParmValue,MeasureDT,FormatType)  values(''''-'''',''''2009-06-15'''',''''label'''')'''+char(10)
		--SET @strSQl  = @strSQL + N'print @str'+char(10)
		SET @strSQL2  = @strSQL2 + N'exec(@str)'+char(10)
	end
end	
if(not @mp2 is null)
begin
	if(@mp2='-')
	begin
		SET @strSQL2  = @strSQL2 + N'insert into TB_MP_TEMP_REPORT(ParmValue,MeasureDT,FormatType) values(''-'',''2009-06-15'',''label'')'+char(10)	
	end
	if(@mp2='MeasureDT')
	begin
		SET @strSQL2  = @strSQL2 + N'insert into TB_MP_TEMP_REPORT(ParmValue,MeasureDT,FormatType) values(''''+cast(datepart(hh,@MeasureDT) as varchar)+'':''+cast(datepart(mi,@MeasureDT) as varchar)+'''',''2009-06-15'',''datetime'')'+char(10)	
	end
	if(@mp2='dur')
	begin
		SET @strSQL2  = @strSQL2 + N'insert into TB_MP_TEMP_REPORT(ParmValue,MeasureDT,FormatType) values(''-'',''2009-06-15'',''label'')'+char(10)
	end
	if(@mp2<>'dur' and @mp2<>'MeasureDT' and @mp2<>'-')
	begin
		SET @strSQL2  = @strSQL2 + N'SET @str = N'''''+char(10)
		SET @strSQL2  = @strSQL2 + N'SET @str = @str + N''if exists( select ParmValue , MeasureDT from tb_mp_'+@mp2+' where measuredt>=''''''+cast(@MeasureDT as varchar)+'''''') insert into TB_MP_TEMP_REPORT(ParmValue,MeasureDT,FormatType) select top 1 cast(ParmValue as varchar), MeasureDT,''number'' from tb_mp_'+@mp2+' where measuredt>=''''''+cast(@MeasureDT as varchar)+'''''' order by measuredt '''+char(10)
		SET @strSQL2  = @strSQL2 + N'SET @str = @str + N'' else   insert into TB_MP_TEMP_REPORT(ParmValue,MeasureDT,FormatType)  values(''''-'''',''''2009-06-15'''',''''label'''')'''+char(10)
		--SET @strSQl  = @strSQL + N'print @str'+char(10)
		SET @strSQL2  = @strSQL2 + N'exec(@str)'+char(10)
	end
end	
if(not @mp3 is null)
begin
	if(@mp3='-')
	begin
		SET @strSQL2  = @strSQL2 + N'insert into TB_MP_TEMP_REPORT(ParmValue,MeasureDT,FormatType) values(''-'',''2009-06-15'',''label'')'+char(10)	
	end
	if(@mp3='MeasureDT')
	begin
		SET @strSQL2  = @strSQL2 + N'insert into TB_MP_TEMP_REPORT(ParmValue,MeasureDT,FormatType) values(''''+cast(datepart(hh,@MeasureDT) as varchar)+'':''+cast(datepart(mi,@MeasureDT) as varchar)+'''',''2009-06-15'',''datetime'')'+char(10)	
	end
	if(@mp3='dur')
	begin
		SET @strSQL2  = @strSQL2 + N'insert into TB_MP_TEMP_REPORT(ParmValue,MeasureDT,FormatType) values(''-'',''2009-06-15'',''label'')'+char(10)
	end
	if(@mp3<>'dur' and @mp3<>'MeasureDT' and @mp3<>'-')
	begin
		SET @strSQL2  = @strSQL2 + N'SET @str = N'''''+char(10)
		SET @strSQL2  = @strSQL2 + N'SET @str = @str + N''if exists( select ParmValue , MeasureDT from tb_mp_'+@mp3+' where measuredt>=''''''+cast(@MeasureDT as varchar)+'''''') insert into TB_MP_TEMP_REPORT(ParmValue,MeasureDT,FormatType) select top 1 cast(ParmValue as varchar), MeasureDT,''number'' from tb_mp_'+@mp3+' where measuredt>=''''''+cast(@MeasureDT as varchar)+'''''' order by measuredt '''+char(10)
		SET @strSQL2  = @strSQL2 + N'SET @str = @str + N'' else   insert into TB_MP_TEMP_REPORT(ParmValue,MeasureDT,FormatType)  values(''''-'''',''''2009-06-15'''',''''label'''')'''+char(10)
		--SET @strSQl  = @strSQL + N'print @str'+char(10)
		SET @strSQL2  = @strSQL2 + N'exec(@str)'+char(10)
	end
end	
if(not @mp4 is null)
begin
	if(@mp4='-')
	begin
		SET @strSQL2  = @strSQL2 + N'insert into TB_MP_TEMP_REPORT(ParmValue,MeasureDT,FormatType) values(''-'',''2009-06-15'',''label'')'+char(10)	
	end
	if(@mp4='MeasureDT')
	begin
		SET @strSQL2  = @strSQL2 + N'insert into TB_MP_TEMP_REPORT(ParmValue,MeasureDT,FormatType) values(''''+cast(datepart(hh,@MeasureDT) as varchar)+'':''+cast(datepart(mi,@MeasureDT) as varchar)+'''',''2009-06-15'',''datetime'')'+char(10)	
	end
	if(@mp4='dur')
	begin
		SET @strSQL2  = @strSQL2 + N'insert into TB_MP_TEMP_REPORT(ParmValue,MeasureDT,FormatType) values(''-'',''2009-06-15'',''label'')'+char(10)
	end
	if(@mp4<>'dur' and @mp4<>'MeasureDT' and @mp4<>'-')
	begin
		SET @strSQL2  = @strSQL2 + N'SET @str = N'''''+char(10)
		SET @strSQL2  = @strSQL2 + N'SET @str = @str + N''if exists( select ParmValue , MeasureDT from tb_mp_'+@mp4+' where measuredt>=''''''+cast(@MeasureDT as varchar)+'''''') insert into TB_MP_TEMP_REPORT(ParmValue,MeasureDT,FormatType) select top 1 cast(ParmValue as varchar), MeasureDT,''number'' from tb_mp_'+@mp4+' where measuredt>=''''''+cast(@MeasureDT as varchar)+'''''' order by measuredt '''+char(10)
		SET @strSQL2  = @strSQL2 + N'SET @str = @str + N'' else   insert into TB_MP_TEMP_REPORT(ParmValue,MeasureDT,FormatType)  values(''''-'''',''''2009-06-15'''',''''label'''')'''+char(10)
		--SET @strSQl  = @strSQL + N'print @str'+char(10)
		SET @strSQL2  = @strSQL2 + N'exec(@str)'+char(10)
	end
end	
if(not @mp5 is null)
begin
	if(@mp5='-')
	begin
		SET @strSQL2  = @strSQL2 + N'insert into TB_MP_TEMP_REPORT(ParmValue,MeasureDT,FormatType) values(''-'',''2009-06-15'',''label'')'+char(10)	
	end
	if(@mp5='MeasureDT')
	begin
		SET @strSQL2  = @strSQL2 + N'insert into TB_MP_TEMP_REPORT(ParmValue,MeasureDT,FormatType) values(''''+cast(datepart(hh,@MeasureDT) as varchar)+'':''+cast(datepart(mi,@MeasureDT) as varchar)+'''',''2009-06-15'',''datetime'')'+char(10)	
	end
	if(@mp5='dur')
	begin
		SET @strSQL2  = @strSQL2 + N'insert into TB_MP_TEMP_REPORT(ParmValue,MeasureDT,FormatType) values(''-'',''2009-06-15'',''label'')'+char(10)
	end
	if(@mp5<>'dur' and @mp5<>'MeasureDT' and @mp5<>'-')
	begin
		SET @strSQL2  = @strSQL2 + N'SET @str = N'''''+char(10)
		SET @strSQL2  = @strSQL2 + N'SET @str = @str + N''if exists( select ParmValue , MeasureDT from tb_mp_'+@mp5+' where measuredt>=''''''+cast(@MeasureDT as varchar)+'''''') insert into TB_MP_TEMP_REPORT(ParmValue,MeasureDT,FormatType) select top 1 cast(ParmValue as varchar), MeasureDT,''number'' from tb_mp_'+@mp5+' where measuredt>=''''''+cast(@MeasureDT as varchar)+'''''' order by measuredt '''+char(10)
		SET @strSQL2  = @strSQL2 + N'SET @str = @str + N'' else   insert into TB_MP_TEMP_REPORT(ParmValue,MeasureDT,FormatType)  values(''''-'''',''''2009-06-15'''',''''label'''')'''+char(10)
		--SET @strSQl  = @strSQL + N'print @str'+char(10)
		SET @strSQL2  = @strSQL2 + N'exec(@str)'+char(10)
	end
end	
if(not @mp6 is null)
begin
	if(@mp6='-')
	begin
		SET @strSQL2  = @strSQL2 + N'insert into TB_MP_TEMP_REPORT(ParmValue,MeasureDT,FormatType) values(''-'',''2009-06-15'',''label'')'+char(10)	
	end
	if(@mp6='MeasureDT')
	begin
		SET @strSQL2  = @strSQL2 + N'insert into TB_MP_TEMP_REPORT(ParmValue,MeasureDT,FormatType) values(''''+cast(datepart(hh,@MeasureDT) as varchar)+'':''+cast(datepart(mi,@MeasureDT) as varchar)+'''',''2009-06-15'',''datetime'')'+char(10)	
	end
	if(@mp6='dur')
	begin
		SET @strSQL2  = @strSQL2 + N'insert into TB_MP_TEMP_REPORT(ParmValue,MeasureDT,FormatType) values(''-'',''2009-06-15'',''label'')'+char(10)
	end
	if(@mp6<>'dur' and @mp6<>'MeasureDT' and @mp6<>'-')
	begin
		SET @strSQL2  = @strSQL2 + N'SET @str = N'''''+char(10)
		SET @strSQL2  = @strSQL2 + N'SET @str = @str + N''if exists( select ParmValue , MeasureDT from tb_mp_'+@mp6+' where measuredt>=''''''+cast(@MeasureDT as varchar)+'''''') insert into TB_MP_TEMP_REPORT(ParmValue,MeasureDT,FormatType) select top 1 cast(ParmValue as varchar), MeasureDT,''number'' from tb_mp_'+@mp6+' where measuredt>=''''''+cast(@MeasureDT as varchar)+'''''' order by measuredt '''+char(10)
		SET @strSQL2  = @strSQL2 + N'SET @str = @str + N'' else   insert into TB_MP_TEMP_REPORT(ParmValue,MeasureDT,FormatType)  values(''''-'''',''''2009-06-15'''',''''label'''')'''+char(10)
		--SET @strSQl  = @strSQL + N'print @str'+char(10)
		SET @strSQL2  = @strSQL2 + N'exec(@str)'+char(10)
	end
end	
SET @strSQL2  = @strSQL2 + N'end'+char(10)
SET @strSQL2  = @strSQL2 + N'set @lastParmValue = @ParmValue'+char(10)	
SET @strSQL2  = @strSQL2 + N'set @lastMeasureDT = @MeasureDT'+char(10)	
SET @strSQL2  = @strSQL2 + N'end'+char(10)



SET @strSQL2  = @strSQL2 + N'FETCH curRunStatus into @ParmValue,@MeasureDT'+char(10)	
SET @strSQL2  = @strSQL2 + N'end'+char(10)


SET @strSQL2  = @strSQL2 + N'close curRunStatus'+char(10)
SET @strSQL2  = @strSQL2 + N'DEALLOCATE curRunStatus'+char(10)

---------------------------------
if(@type='+')
SET @strSQL2  = @strSQL2 + N'if (@lastParmValue=1 and @ParmValue=1)'+char(10)
else
SET @strSQL2  = @strSQL2 + N'if (@lastParmValue=0 and @ParmValue=0)'+char(10)
SET @strSQL2  = @strSQL2 + N'begin'+char(10)
SET @strSQL2  = @strSQL2 + N'set @count = @count+1'+char(10)
SET @strSQL2  = @strSQL2 + N'insert into TB_MP_TEMP_REPORT(ParmValue,MeasureDT,FormatType) values(''占位'',''2009-06-15'',''label'')'+char(10)
if(not @mp1 is null)
begin
	if(@mp1='dur')
	begin
		SET @strSQL2  = @strSQL2 + N'insert into TB_MP_TEMP_REPORT(ParmValue,MeasureDT,FormatType) values(''''+cast(datediff(mi,@lastMeasureDT,'''+@endtime+N''')/60 as varchar)+'':''+cast(datediff(mi,@lastMeasureDT,'''+@endtime+N''')%60 as varchar)+'''',''2009-06-15'',''datetime'')'+char(10)
	end
	else
	begin
		SET @strSQL2  = @strSQL2 + N'insert into TB_MP_TEMP_REPORT(ParmValue,MeasureDT,FormatType) values(''-'',''2009-06-15'',''label'')'+char(10)
	end
end
if(not @mp2 is null)
begin
	if(@mp2='dur')
	begin
		SET @strSQL2  = @strSQL2 + N'insert into TB_MP_TEMP_REPORT(ParmValue,MeasureDT,FormatType) values(''''+cast(datediff(mi,@lastMeasureDT,'''+@endtime+N''')/60 as varchar)+'':''+cast(datediff(mi,@lastMeasureDT,'''+@endtime+N''')%60 as varchar)+'''',''2009-06-15'',''datetime'')'+char(10)
	end
	else
	begin
		SET @strSQL2  = @strSQL2 + N'insert into TB_MP_TEMP_REPORT(ParmValue,MeasureDT,FormatType) values(''-'',''2009-06-15'',''label'')'+char(10)
	end
end
if(not @mp3 is null)
begin
	if(@mp3='dur')
	begin
		SET @strSQL2  = @strSQL2 + N'insert into TB_MP_TEMP_REPORT(ParmValue,MeasureDT,FormatType) values(''''+cast(datediff(mi,@lastMeasureDT,'''+@endtime+N''')/60 as varchar)+'':''+cast(datediff(mi,@lastMeasureDT,'''+@endtime+N''')%60 as varchar)+'''',''2009-06-15'',''datetime'')'+char(10)
	end
	else
	begin
		SET @strSQL2  = @strSQL2 + N'insert into TB_MP_TEMP_REPORT(ParmValue,MeasureDT,FormatType) values(''-'',''2009-06-15'',''label'')'+char(10)
	end
end
if(not @mp4 is null)
begin
	if(@mp4='dur')
	begin
		SET @strSQL2  = @strSQL2 + N'insert into TB_MP_TEMP_REPORT(ParmValue,MeasureDT,FormatType) values(''''+cast(datediff(mi,@lastMeasureDT,'''+@endtime+N''')/60 as varchar)+'':''+cast(datediff(mi,@lastMeasureDT,'''+@endtime+N''')%60 as varchar)+'''',''2009-06-15'',''datetime'')'+char(10)
	end
	else
	begin
		SET @strSQL2  = @strSQL2 + N'insert into TB_MP_TEMP_REPORT(ParmValue,MeasureDT,FormatType) values(''-'',''2009-06-15'',''label'')'+char(10)
	end
end
if(not @mp5 is null)
begin
	if(@mp5='dur')
	begin
		SET @strSQL2  = @strSQL2 + N'insert into TB_MP_TEMP_REPORT(ParmValue,MeasureDT,FormatType) values(''''+cast(datediff(mi,@lastMeasureDT,'''+@endtime+N''')/60 as varchar)+'':''+cast(datediff(mi,@lastMeasureDT,'''+@endtime+N''')%60 as varchar)+'''',''2009-06-15'',''datetime'')'+char(10)
	end
	else
	begin
		SET @strSQL2  = @strSQL2 + N'insert into TB_MP_TEMP_REPORT(ParmValue,MeasureDT,FormatType) values(''-'',''2009-06-15'',''label'')'+char(10)
	end
end
if(not @mp6 is null)
begin
	if(@mp6='dur')
	begin
		SET @strSQL2  = @strSQL2 + N'insert into TB_MP_TEMP_REPORT(ParmValue,MeasureDT,FormatType) values(''''+cast(datediff(mi,@lastMeasureDT,'''+@endtime+N''')/60 as varchar)+'':''+cast(datediff(mi,@lastMeasureDT,'''+@endtime+N''')%60 as varchar)+'''',''2009-06-15'',''datetime'')'+char(10)
	end
	else
	begin
		SET @strSQL2  = @strSQL2 + N'insert into TB_MP_TEMP_REPORT(ParmValue,MeasureDT,FormatType) values(''-'',''2009-06-15'',''label'')'+char(10)
	end
end
SET @strSQL2  = @strSQL2 + N'end'+char(10)


SET @strSQL3 = N''
SET @strSQL3  = @strSQL3 + N'set @count = @count+1'+char(10)
SET @strSQL3  = @strSQL3 + N'while(@count<='+cast(@rownum as varchar)+N')'+char(10)
SET @strSQL3  = @strSQL3 + N'begin'+char(10)	
SET @strSQL3  = @strSQL3 + N'SET @str  =N''insert into TB_MP_TEMP_REPORT(ParmValue,MeasureDT,FormatType) values(''''-'''',''''2009-06-15'''',''''label'''')'''+char(10)	
if(not @mp1 is null) SET @strSQL3  = @strSQL3 + N'SET @str  = @str + N''insert into TB_MP_TEMP_REPORT(ParmValue,MeasureDT,FormatType) values(''''-'''',''''2009-06-15'''',''''label'''')'''+char(10)	
if(not @mp2 is null) SET @strSQL3  = @strSQL3 + N'SET @str  = @str + N''insert into TB_MP_TEMP_REPORT(ParmValue,MeasureDT,FormatType) values(''''-'''',''''2009-06-15'''',''''label'''')'''+char(10)	
if(not @mp3 is null) SET @strSQL3  = @strSQL3 + N'SET @str  = @str + N''insert into TB_MP_TEMP_REPORT(ParmValue,MeasureDT,FormatType) values(''''-'''',''''2009-06-15'''',''''label'''')'''+char(10)	
if(not @mp4 is null) SET @strSQL3  = @strSQL3 + N'SET @str  = @str + N''insert into TB_MP_TEMP_REPORT(ParmValue,MeasureDT,FormatType) values(''''-'''',''''2009-06-15'''',''''label'''')'''+char(10)	
if(not @mp5 is null) SET @strSQL3  = @strSQL3 + N'SET @str  = @str + N''insert into TB_MP_TEMP_REPORT(ParmValue,MeasureDT,FormatType) values(''''-'''',''''2009-06-15'''',''''label'''')'''+char(10)	
if(not @mp6 is null) SET @strSQL3  = @strSQL3 + N'SET @str  = @str + N''insert into TB_MP_TEMP_REPORT(ParmValue,MeasureDT,FormatType) values(''''-'''',''''2009-06-15'''',''''label'''')'''+char(10)	
SET @strSQL3  = @strSQL3 + N'exec(@str)'+char(10)
SET @strSQL3  = @strSQL3 + N'set @count = @count+1'+char(10)
SET @strSQL3  = @strSQL3 + N'end'+char(10)


print @strSQL1
print @strSQL2
print @strSQL3
exec ( @strSQL1+@strSQL2+@strSQL3)

end
else
	begin
		while(@i<=@colcount*@rownum)
		begin
		--print '-'
		insert into TB_MP_TEMP_REPORT(ParmValue,MeasureDT,FormatType) values('-',@starttime,'label')
		set @i = @i+1
		end
	end


GO

-- ----------------------------
-- Procedure structure for sp_report_year_01
-- ----------------------------
DROP PROCEDURE [dbo].[sp_report_year_01]
GO



CREATE PROCEDURE [dbo].[sp_report_year_01]
@stime varchar(10), @etime varchar(10), @starthour int,  @intval int, @mpstr varchar(5000),@rownum int,@userid varchar(50)
 AS
declare @starttime datetime
declare @endtime datetime
declare @tempdate datetime
declare @mptemp varchar(500)
declare @collectmode varchar(50)
declare @mp varchar(500)


Select @starttime=cast(@stime as datetime)
Select @starttime=DateAdd(hour,@starthour,@starttime)
-- Select @starttime=DateAdd(MONTH,-1,@starttime)
Select @starttime=DateAdd(MONTH,0,@starttime)
Select @endtime=cast(@etime as datetime)
Select @endtime=DateAdd(hour,@starthour,@endtime)
if(@endtime>dateadd(day,-1,getdate()))
begin
set @endtime=dateadd(day,0,getdate())
end

-- DROP TABLE TB_MP_TEMP_REPORT
-- 
-- CREATE TABLE [dbo].[TB_MP_TEMP_REPORT] (
-- 	[ItemID] [bigint] IDENTITY (1, 1) NOT NULL ,
-- 	[ParmValue] [varchar] (2000) COLLATE Chinese_PRC_CI_AS NOT NULL ,
-- 	[MeasureDT] [datetime] NOT NULL ,
-- 	[FormatType] [varchar] (50) COLLATE Chinese_PRC_CI_AS NOT NULL,
-- 	[Decimal] [int] 
-- ) ON [PRIMARY]

while(@starttime<@endtime)
begin
	set @tempdate = dateadd(month,@intval,@starttime)
	--if(DATEPART(dd,@starttime)=1)
	--begin
	--insert into TB_MP_TEMP_REPORT(ParmValue,MeasureDT,FormatType) values(cast(DATEPART(mm,@starttime) as varchar)+'月',@starttime,'label')
	--end
	--else
	--begin
	--insert into TB_MP_TEMP_REPORT(ParmValue,MeasureDT,FormatType) values(cast(DATEPART(mm,dateadd(mm,1,@starttime)) as varchar)+'月',@starttime,'label')
	--end
	
	DECLARE CRStatus cursor for 
	select * from dbo.FUN_split(@mpstr,'|')
	open CRStatus

	FETCH  CRStatus into @mptemp

	set @mp = SUBSTRING ( @mptemp , 0 , CHARINDEX(';',@mptemp) )
	set @collectmode = SUBSTRING ( @mptemp , CHARINDEX(';',@mptemp)+1, LEN ( @mptemp )-CHARINDEX(';',@mptemp) )

	while (@@fetch_status=0)
	begin
	print @starttime
	print @tempdate
	print @collectmode
	print @mp
	print @userid


        exec sp_report_getdata @starttime,@tempdate,@collectmode,@mp,@userid

	FETCH  CRStatus into @mptemp

	set @mp = SUBSTRING ( @mptemp , 0 , CHARINDEX(';',@mptemp) )
	set @collectmode = SUBSTRING ( @mptemp , CHARINDEX(';',@mptemp)+1, LEN ( @mptemp )-CHARINDEX(';',@mptemp) )

	end

	close CRStatus
	DEALLOCATE CRStatus
	
	set @starttime = @tempdate
end

Select * from TB_MP_TEMP_REPORT where userid=@userid order by itemid


GO

-- ----------------------------
-- Procedure structure for sp_runrecord
-- ----------------------------
DROP PROCEDURE [dbo].[sp_runrecord]
GO



CREATE PROCEDURE [dbo].[sp_runrecord] 
@starttime varchar(20),@endtime varchar(20),@type varchar(80),@mp varchar(800),@rownum int
as
Declare @strSQL1 nvarchar(4000)
Declare @strSQL2 nvarchar(4000)
Declare @strSQL3 nvarchar(4000)

declare @count int
declare @ParmValue money
declare @MeasureDT datetime
declare @lastParmValue money
declare @lastMeasureDT datetime
declare @colcount int
declare @i int

declare @idx int
declare @idxo int

declare @mainmp varchar(200)
declare @mp1 varchar(200)
declare @mp2 varchar(200)
declare @mp3 varchar(200)
declare @mp4 varchar(200)
declare @mp5 varchar(200)
declare @mp6 varchar(200)
declare @mp7 varchar(200)
declare @mp8 varchar(200)
declare @mp9 varchar(200)
declare @mp10 varchar(200)

--delete from tb_mp_temp_rpt
select @colcount = count(*) from dbo.FUN_split(@mp,',') 
set @i = 1

if(SUBSTRING(@mp,1,1)!='-')
	begin

set @idx = charindex(',',@mp,1)
if(@idx>0) 
begin
	set @mainmp = SUBSTRING(@mp,1,@idx-1)
	print @mainmp

	set @mp= SUBSTRING(@mp,@idx+1,len(@mp)) 
	print @mp

	set @idx = charindex(',',@mp,1)
	if(@idx>0)
	begin
		set @mp1 = SUBSTRING(@mp,1,@idx-1)
		print @mp1	


		set @mp= SUBSTRING(@mp,@idx+1,len(@mp)) 
		print @mp
	
		set @idx = charindex(',',@mp,1)
		if(@idx>0)
		begin
			set @mp2 = SUBSTRING(@mp,1,@idx-1)
			print @mp2	
	
			set @mp= SUBSTRING(@mp,@idx+1,len(@mp)) 
			print @mp


			set @idx = charindex(',',@mp,1)
			if(@idx>0)
			begin
				set @mp3 = SUBSTRING(@mp,1,@idx-1)
				print @mp3	
			
				set @mp= SUBSTRING(@mp,@idx+1,len(@mp)) 
				print @mp
	
				set @idx = charindex(',',@mp,1)
				if(@idx>0)
				begin
					set @mp4 = SUBSTRING(@mp,1,@idx-1)
					print @mp4	
			
					set @mp= SUBSTRING(@mp,@idx+1,len(@mp)) 
					print @mp

					set @idx = charindex(',',@mp,1)
					if(@idx>0)
					begin
						set @mp5 = SUBSTRING(@mp,1,@idx-1)
						print @mp5	
				
						set @mp= SUBSTRING(@mp,@idx+1,len(@mp)) 
						print @mp
	
						set @idx = charindex(',',@mp,1)
						if(@idx>0)
						begin
							set @mp6 = SUBSTRING(@mp,1,@idx-1)
							print @mp6	
						end 
						else
						begin
							set @mp6 = @mp
							print @mp6	
						end
	
					end
					else
					begin
						set @mp5 = @mp
						print @mp5	
					end
				end
				else
				begin
					set @mp4 = @mp
					print @mp4	
				end
			end
			else
			begin
				set @mp3 = @mp
				print @mp3	
			end
	
		end
		else
		begin
			set @mp2= @mp
			print @mp2	
		end

	end
	else
	begin
		set @mp1 = @mp
		print @mp1	
	end
end
else
begin
	set @mainmp=@mp
	print @mainmp
end



SET @strSQL1 = N''
SET @strSQL1 = @strSQL1+N'declare @count int'+char(10)
SET @strSQL1 = @strSQL1+N'declare @ParmValue money'+char(10)
SET @strSQL1 = @strSQL1+N'declare @MeasureDT datetime'+char(10)
SET @strSQL1 = @strSQL1+N'declare @lastParmValue money'+char(10)
SET @strSQL1 = @strSQL1+N'declare @lastMeasureDT datetime'+char(10)
SET @strSQL1 = @strSQL1+N'declare @str nvarchar(4000)'+char(10)

--寻找今天第一条记录
SET @strSQL1 = @strSQL1 + N'select top 1 @lastParmValue =ParmValue ,@lastMeasureDT=MeasureDT from TB_MP_'+@mainmp+N' where measuredt between '''+@starttime+N''' and ''' +@endtime+ N''' order by measuredt asc ' +char(10)
if(@type='+')
begin
	--如果第一天记录代表运行
	SET @strSQL1 = @strSQL1 + N'if(@lastParmValue=1)'+char(10)
	SET @strSQL1 = @strSQL1 + N'begin'+char(10)
	--寻找今天之前最后一条记录
	SET @strSQL1 = @strSQL1 + N'select top 1 @lastParmValue =ParmValue ,@lastMeasureDT=MeasureDT from TB_MP_'+@mainmp+N' where measuredt < '''+@starttime+N'''  order by measuredt desc' +char(10)
	--如果记录存在且为运行
	SET @strSQL1 = @strSQL1 + N'if(not @lastParmValue is null and @lastParmValue=1)'+char(10)
	SET @strSQL1 = @strSQL1 + N'begin'+char(10)
	--做一条假的开机记录
	SET @strSQL1 = @strSQL1 + N'set @lastMeasureDT='''+@starttime+''''+char(10)
	SET @strSQL1 = @strSQL1 + N'set @lastParmValue=1 '+char(10)
	SET @strSQL1  = @strSQL1 + N'end'+char(10)
	SET @strSQL1  = @strSQL1 + N'end'+char(10)
end
else
begin
	SET @strSQL1 = @strSQL1 + N'if(@lastParmValue=0)'+char(10)
	SET @strSQL1 = @strSQL1 + N'begin'+char(10)
	SET @strSQL1 = @strSQL1 + N'select top 1 @lastParmValue =ParmValue ,@lastMeasureDT=MeasureDT from TB_MP_'+@mainmp+N' where measuredt < '''+@starttime+N''' order by measuredt desc' +char(10)
	--如果记录存在且为运行
	SET @strSQL1 = @strSQL1 + N'if(not @lastParmValue is null and @lastParmValue=0)'+char(10)
	SET @strSQL1 = @strSQL1 + N'begin'+char(10)
	--做一条假的开机记录
	SET @strSQL1 = @strSQL1 + N'set @lastMeasureDT='''+@starttime+''''+char(10)
	SET @strSQL1 = @strSQL1 + N'set @lastParmValue=0 '+char(10)
	SET @strSQL1  = @strSQL1 + N'end'+char(10)
	SET @strSQL1  = @strSQL1 + N'end'+char(10)
end

SET @strSQL1 = @strSQL1 + N'set @count=0'+char(10)

SET @strSQL1 = @strSQL1 + N'DECLARE curRunStatus cursor for select ParmValue,MeasureDT from TB_MP_' + @mainmp+N' ' +char(10)
SET @strSQL1 = @strSQL1 + N'where measuredt between ''' +cast(@starttime as varchar(20)) +N''' and '''+cast(@endtime as varchar(20))+N''' order by MeasureDT asc'+char(10)
SET @strSQL1 = @strSQL1 + N'open curRunStatus'+char(10)
SET @strSQL1 = @strSQL1 + N'FETCH curRunStatus into @ParmValue,@MeasureDT'+char(10)
SET @strSQL1 = @strSQL1 + N'while (@@fetch_status=0)'+char(10)
SET @strSQL1 = @strSQL1 + N'begin'+char(10)
SET @strSQL1 = @strSQL1 + N'if(@lastParmValue is null)'+char(10)
SET @strSQL1 = @strSQL1 + N'begin'+char(10)
SET @strSQL1 = @strSQL1 + N'set @lastParmValue = @ParmValue'+char(10)	
SET @strSQL1 = @strSQL1 + N'set @lastMeasureDT = @MeasureDT'+char(10)	
SET @strSQL1 = @strSQL1 + N'end'+char(10)

SET @strSQL1 = @strSQL1 + N'if(@ParmValue<>@lastParmValue)'+char(10)
SET @strSQL1 = @strSQL1 + N'begin'+char(10)
SET @strSQL1 = @strSQL1 + N'set  @count=@count+1'+char(10)	

if(@type='+')
SET @strSQL1  = @strSQL1 + N'if(@ParmValue=0)'++char(10)	
else
SET @strSQL1  = @strSQL1 + N'if(@ParmValue=1)'++char(10)	

SET @strSQL1  = @strSQL1 + N'begin'+char(10)
SET @strSQL1  = @strSQL1 + N'insert into tb_mp_temp_rpt(ParmValue,MeasureDT) values(''停机'',''2009-06-15'')'+char(10)

if(not @mp1 is null)
begin
	if(@mp1='MeasureDT')
	begin
		SET @strSQL1  = @strSQL1 + N'insert into tb_mp_temp_rpt(ParmValue,MeasureDT) values(''''+cast(datepart(hh,@MeasureDT) as varchar)+'':''+cast(datepart(mi,@MeasureDT) as varchar)+'''',''2009-06-15'')'+char(10)	
	end
	if(@mp1='dur')
	begin

		SET @strSQL1  = @strSQL1 + N'insert into tb_mp_temp_rpt(ParmValue,MeasureDT) values(''''+cast(datediff(mi,@lastMeasureDT,@MeasureDT)/60 as varchar)+'':''+cast(datediff(mi,@lastMeasureDT,@MeasureDT)%60 as varchar)+'''',''2009-06-15'')'+char(10)
	end
	if(@mp1<>'dur' and @mp1<>'MeasureDT')
	begin
		SET @strSQL1  = @strSQL1 + N'SET @str = N'''''+char(10)
		SET @strSQL1  = @strSQL1 + N'SET @str = @str + N''if exists( select ParmValue , MeasureDT from tb_mp_'+@mp1+' where measuredt>=''''''+cast(@MeasureDT as varchar)+'''''') insert into tb_mp_temp_rpt(ParmValue,MeasureDT) select top 1 cast(ParmValue as varchar), MeasureDT from tb_mp_'+@mp1+' where measuredt>=''''''+cast(@MeasureDT as varchar)+'''''' order by measuredt '''+char(10)
		SET @strSQL1  = @strSQL1 + N'SET @str = @str + N'' else   insert into tb_mp_temp_rpt(ParmValue,MeasureDT)  values(''''-'''',''''2009-06-15'''')'''+char(10)
		--SET @strSQl  = @strSQL + N'print @str'+char(10)
		SET @strSQL1  = @strSQL1 + N'exec(@str)'+char(10)
	end
end	
if(not @mp2 is null)
begin
	if(@mp2='MeasureDT')
	begin
		SET @strSQL1  = @strSQL1 + N'insert into tb_mp_temp_rpt(ParmValue,MeasureDT) values(''''+cast(datepart(hh,@MeasureDT) as varchar)+'':''+cast(datepart(mi,@MeasureDT) as varchar)+'''',''2009-06-15'')'+char(10)	
	end
	if(@mp2='dur')
	begin
		SET @strSQL1  = @strSQL1 + N'insert into tb_mp_temp_rpt(ParmValue,MeasureDT) values(''''+cast(datediff(mi,@lastMeasureDT,@MeasureDT)/60 as varchar)+'':''+cast(datediff(mi,@lastMeasureDT,@MeasureDT)%60 as varchar)+'''',''2009-06-15'')'+char(10)
	end
	if(@mp2<>'dur' and @mp2<>'MeasureDT')
	begin
		SET @strSQL1  = @strSQL1 + N'SET @str = N'''''+char(10)
		SET @strSQL1  = @strSQL1 + N'SET @str = @str + N''if exists( select ParmValue , MeasureDT from tb_mp_'+@mp2+' where measuredt>=''''''+cast(@MeasureDT as varchar)+'''''') insert into tb_mp_temp_rpt(ParmValue,MeasureDT) select top 1 cast(ParmValue as varchar), MeasureDT from tb_mp_'+@mp2+' where measuredt>=''''''+cast(@MeasureDT as varchar)+'''''' order by measuredt '''+char(10)
		SET @strSQL1  = @strSQL1 + N'SET @str = @str + N'' else   insert into tb_mp_temp_rpt(ParmValue,MeasureDT)  values(''''-'''',''''2009-06-15'''')'''+char(10)
		--SET @strSQl  = @strSQL + N'print @str'+char(10)
		SET @strSQL1  = @strSQL1 + N'exec(@str)'+char(10)
	end
end	
if(not @mp3 is null)
begin
	if(@mp3='MeasureDT')
	begin
		SET @strSQL1  = @strSQL1 + N'insert into tb_mp_temp_rpt(ParmValue,MeasureDT) values(''''+cast(datepart(hh,@MeasureDT) as varchar)+'':''+cast(datepart(mi,@MeasureDT) as varchar)+'''',''2009-06-15'')'+char(10)	
	end
	if(@mp3='dur')
	begin
		SET @strSQL1  = @strSQL1 + N'insert into tb_mp_temp_rpt(ParmValue,MeasureDT) values(''''+cast(datediff(mi,@lastMeasureDT,@MeasureDT)/60 as varchar)+'':''+cast(datediff(mi,@lastMeasureDT,@MeasureDT)%60 as varchar)+'''',''2009-06-15'')'+char(10)
	end
	if(@mp3<>'dur' and @mp3<>'MeasureDT')
	begin
		SET @strSQL1  = @strSQL1 + N'SET @str = N'''''+char(10)
		SET @strSQL1  = @strSQL1 + N'SET @str = @str + N''if exists( select ParmValue , MeasureDT from tb_mp_'+@mp3+' where measuredt>=''''''+cast(@MeasureDT as varchar)+'''''') insert into tb_mp_temp_rpt(ParmValue,MeasureDT) select top 1 cast(ParmValue as varchar), MeasureDT from tb_mp_'+@mp3+' where measuredt>=''''''+cast(@MeasureDT as varchar)+'''''' order by measuredt '''+char(10)
		SET @strSQL1  = @strSQL1 + N'SET @str = @str + N'' else   insert into tb_mp_temp_rpt(ParmValue,MeasureDT)  values(''''-'''',''''2009-06-15'''')'''+char(10)
		--SET @strSQl  = @strSQL + N'print @str'+char(10)
		SET @strSQL1  = @strSQL1 + N'exec(@str)'+char(10)
	end
end	
if(not @mp4 is null)
begin
	if(@mp4='MeasureDT')
	begin
		SET @strSQL1  = @strSQL1 + N'insert into tb_mp_temp_rpt(ParmValue,MeasureDT) values(''''+cast(datepart(hh,@MeasureDT) as varchar)+'':''+cast(datepart(mi,@MeasureDT) as varchar)+'''',''2009-06-15'')'+char(10)	
	end
	if(@mp4='dur')
	begin
		SET @strSQL1  = @strSQL1 + N'insert into tb_mp_temp_rpt(ParmValue,MeasureDT) values(''''+cast(datediff(mi,@lastMeasureDT,@MeasureDT)/60 as varchar)+'':''+cast(datediff(mi,@lastMeasureDT,@MeasureDT)%60 as varchar)+'''',''2009-06-15'')'+char(10)
	end
	if(@mp4<>'dur' and @mp4<>'MeasureDT')
	begin
		SET @strSQL1  = @strSQL1 + N'SET @str = N'''''+char(10)
		SET @strSQL1  = @strSQL1 + N'SET @str = @str + N''if exists( select ParmValue , MeasureDT from tb_mp_'+@mp4+' where measuredt>=''''''+cast(@MeasureDT as varchar)+'''''') insert into tb_mp_temp_rpt(ParmValue,MeasureDT) select top 1 cast(ParmValue as varchar), MeasureDT from tb_mp_'+@mp4+' where measuredt>=''''''+cast(@MeasureDT as varchar)+'''''' order by measuredt '''+char(10)
		SET @strSQL1  = @strSQL1 + N'SET @str = @str + N'' else   insert into tb_mp_temp_rpt(ParmValue,MeasureDT)  values(''''-'''',''''2009-06-15'''')'''+char(10)
		--SET @strSQl  = @strSQL + N'print @str'+char(10)
		SET @strSQL1  = @strSQL1 + N'exec(@str)'+char(10)
	end
end	
if(not @mp5 is null)
begin
	if(@mp5='MeasureDT')
	begin
		SET @strSQL1  = @strSQL1 + N'insert into tb_mp_temp_rpt(ParmValue,MeasureDT) values(''''+cast(datepart(hh,@MeasureDT) as varchar)+'':''+cast(datepart(mi,@MeasureDT) as varchar)+'''',''2009-06-15'')'+char(10)	
	end
	if(@mp5='dur')
	begin
		SET @strSQL1  = @strSQL1 + N'insert into tb_mp_temp_rpt(ParmValue,MeasureDT) values(''''+cast(datediff(mi,@lastMeasureDT,@MeasureDT)/60 as varchar)+'':''+cast(datediff(mi,@lastMeasureDT,@MeasureDT)%60 as varchar)+'''',''2009-06-15'')'+char(10)
	end
	if(@mp5<>'dur' and @mp5<>'MeasureDT')
	begin
		SET @strSQL1  = @strSQL1 + N'SET @str = N'''''+char(10)
		SET @strSQL1  = @strSQL1 + N'SET @str = @str + N''if exists( select ParmValue , MeasureDT from tb_mp_'+@mp5+' where measuredt>=''''''+cast(@MeasureDT as varchar)+'''''') insert into tb_mp_temp_rpt(ParmValue,MeasureDT) select top 1 cast(ParmValue as varchar), MeasureDT from tb_mp_'+@mp5+' where measuredt>=''''''+cast(@MeasureDT as varchar)+'''''' order by measuredt '''+char(10)
		SET @strSQL1  = @strSQL1 + N'SET @str = @str + N'' else   insert into tb_mp_temp_rpt(ParmValue,MeasureDT)  values(''''-'''',''''2009-06-15'''')'''+char(10)
		--SET @strSQl  = @strSQL + N'print @str'+char(10)
		SET @strSQL1  = @strSQL1 + N'exec(@str)'+char(10)
	end
end	
if(not @mp6 is null)
begin
	if(@mp6='MeasureDT')
	begin
		SET @strSQL1  = @strSQL1 + N'insert into tb_mp_temp_rpt(ParmValue,MeasureDT) values(''''+cast(datepart(hh,@MeasureDT) as varchar)+'':''+cast(datepart(mi,@MeasureDT) as varchar)+'''',''2009-06-15'')'+char(10)	
	end
	if(@mp6='dur')
	begin
		SET @strSQL1  = @strSQL1 + N'insert into tb_mp_temp_rpt(ParmValue,MeasureDT) values(''''+cast(datediff(mi,@lastMeasureDT,@MeasureDT)/60 as varchar)+'':''+cast(datediff(mi,@lastMeasureDT,@MeasureDT)%60 as varchar)+'''',''2009-06-15'')'+char(10)
	end
	if(@mp6<>'dur' and @mp6<>'MeasureDT')
	begin
		SET @strSQL1  = @strSQL1 + N'SET @str = N'''''+char(10)
		SET @strSQL1  = @strSQL1 + N'SET @str = @str + N''if exists( select ParmValue , MeasureDT from tb_mp_'+@mp6+' where measuredt>=''''''+cast(@MeasureDT as varchar)+'''''') insert into tb_mp_temp_rpt(ParmValue,MeasureDT) select top 1 cast(ParmValue as varchar), MeasureDT from tb_mp_'+@mp6+' where measuredt>=''''''+cast(@MeasureDT as varchar)+'''''' order by measuredt '''+char(10)
		SET @strSQL1  = @strSQL1 + N'SET @str = @str + N'' else   insert into tb_mp_temp_rpt(ParmValue,MeasureDT)  values(''''-'''',''''2009-06-15'''')'''+char(10)
		--SET @strSQl  = @strSQL + N'print @str'+char(10)
		SET @strSQL1  = @strSQL1 + N'exec(@str)'+char(10)
	end
end	
SET @strSQL1  = @strSQL1 + N'end'+char(10)

SET @strSQL2 = N''
if(@type='+')	
SET @strSQL2  = @strSQL2 + N'if(@ParmValue=1)'+char(10)	
else
SET @strSQL2  = @strSQL2 + N'if(@ParmValue=0)'+char(10)	

SET @strSQL2  = @strSQL2 + N'begin'+char(10)
SET @strSQL2  = @strSQL2 + N'insert into tb_mp_temp_rpt(ParmValue,MeasureDT) values(''开机'',''2009-06-15'')'+char(10)	
if(not @mp1 is null)
begin
	if(@mp1='MeasureDT')
	begin
		SET @strSQL2  = @strSQL2 + N'insert into tb_mp_temp_rpt(ParmValue,MeasureDT) values(''''+cast(datepart(hh,@MeasureDT) as varchar)+'':''+cast(datepart(mi,@MeasureDT) as varchar)+'''',''2009-06-15'')'+char(10)	
	end
	if(@mp1='dur')
	begin
		SET @strSQL2  = @strSQL2 + N'insert into tb_mp_temp_rpt(ParmValue,MeasureDT) values(''-'',''2009-06-15'')'+char(10)
	end
	if(@mp1<>'dur' and @mp1<>'MeasureDT')
	begin
		SET @strSQL2  = @strSQL2 + N'SET @str = N'''''+char(10)
		SET @strSQL2  = @strSQL2 + N'SET @str = @str + N''if exists( select ParmValue , MeasureDT from tb_mp_'+@mp1+' where measuredt>=''''''+cast(@MeasureDT as varchar)+'''''') insert into tb_mp_temp_rpt(ParmValue,MeasureDT) select top 1 cast(ParmValue as varchar), MeasureDT from tb_mp_'+@mp1+' where measuredt>=''''''+cast(@MeasureDT as varchar)+'''''' order by measuredt '''+char(10)
		SET @strSQL2  = @strSQL2 + N'SET @str = @str + N'' else   insert into tb_mp_temp_rpt(ParmValue,MeasureDT)  values(''''-'''',''''2009-06-15'''')'''+char(10)
		--SET @strSQl  = @strSQL + N'print @str'+char(10)
		SET @strSQL2  = @strSQL2 + N'exec(@str)'+char(10)
	end
end	
if(not @mp2 is null)
begin
	if(@mp2='MeasureDT')
	begin
		SET @strSQL2  = @strSQL2 + N'insert into tb_mp_temp_rpt(ParmValue,MeasureDT) values(''''+cast(datepart(hh,@MeasureDT) as varchar)+'':''+cast(datepart(mi,@MeasureDT) as varchar)+'''',''2009-06-15'')'+char(10)	
	end
	if(@mp2='dur')
	begin
		SET @strSQL2  = @strSQL2 + N'insert into tb_mp_temp_rpt(ParmValue,MeasureDT) values(''-'',''2009-06-15'')'+char(10)
	end
	if(@mp2<>'dur' and @mp2<>'MeasureDT')
	begin
		SET @strSQL2  = @strSQL2 + N'SET @str = N'''''+char(10)
		SET @strSQL2  = @strSQL2 + N'SET @str = @str + N''if exists( select ParmValue , MeasureDT from tb_mp_'+@mp2+' where measuredt>=''''''+cast(@MeasureDT as varchar)+'''''') insert into tb_mp_temp_rpt(ParmValue,MeasureDT) select top 1 cast(ParmValue as varchar), MeasureDT from tb_mp_'+@mp2+' where measuredt>=''''''+cast(@MeasureDT as varchar)+'''''' order by measuredt '''+char(10)
		SET @strSQL2  = @strSQL2 + N'SET @str = @str + N'' else   insert into tb_mp_temp_rpt(ParmValue,MeasureDT)  values(''''-'''',''''2009-06-15'''')'''+char(10)
		--SET @strSQl  = @strSQL + N'print @str'+char(10)
		SET @strSQL2  = @strSQL2 + N'exec(@str)'+char(10)
	end
end	
if(not @mp3 is null)
begin
	if(@mp3='MeasureDT')
	begin
		SET @strSQL2  = @strSQL2 + N'insert into tb_mp_temp_rpt(ParmValue,MeasureDT) values(''''+cast(datepart(hh,@MeasureDT) as varchar)+'':''+cast(datepart(mi,@MeasureDT) as varchar)+'''',''2009-06-15'')'+char(10)	
	end
	if(@mp3='dur')
	begin
		SET @strSQL2  = @strSQL2 + N'insert into tb_mp_temp_rpt(ParmValue,MeasureDT) values(''-'',''2009-06-15'')'+char(10)
	end
	if(@mp3<>'dur' and @mp3<>'MeasureDT')
	begin
		SET @strSQL2  = @strSQL2 + N'SET @str = N'''''+char(10)
		SET @strSQL2  = @strSQL2 + N'SET @str = @str + N''if exists( select ParmValue , MeasureDT from tb_mp_'+@mp3+' where measuredt>=''''''+cast(@MeasureDT as varchar)+'''''') insert into tb_mp_temp_rpt(ParmValue,MeasureDT) select top 1 cast(ParmValue as varchar), MeasureDT from tb_mp_'+@mp3+' where measuredt>=''''''+cast(@MeasureDT as varchar)+'''''' order by measuredt '''+char(10)
		SET @strSQL2  = @strSQL2 + N'SET @str = @str + N'' else   insert into tb_mp_temp_rpt(ParmValue,MeasureDT)  values(''''-'''',''''2009-06-15'''')'''+char(10)
		--SET @strSQl  = @strSQL + N'print @str'+char(10)
		SET @strSQL2  = @strSQL2 + N'exec(@str)'+char(10)
	end
end	
if(not @mp4 is null)
begin
	if(@mp4='MeasureDT')
	begin
		SET @strSQL2  = @strSQL2 + N'insert into tb_mp_temp_rpt(ParmValue,MeasureDT) values(''''+cast(datepart(hh,@MeasureDT) as varchar)+'':''+cast(datepart(mi,@MeasureDT) as varchar)+'''',''2009-06-15'')'+char(10)	
	end
	if(@mp4='dur')
	begin
		SET @strSQL2  = @strSQL2 + N'insert into tb_mp_temp_rpt(ParmValue,MeasureDT) values(''-'',''2009-06-15'')'+char(10)
	end
	if(@mp4<>'dur' and @mp4<>'MeasureDT')
	begin
		SET @strSQL2  = @strSQL2 + N'SET @str = N'''''+char(10)
		SET @strSQL2  = @strSQL2 + N'SET @str = @str + N''if exists( select ParmValue , MeasureDT from tb_mp_'+@mp4+' where measuredt>=''''''+cast(@MeasureDT as varchar)+'''''') insert into tb_mp_temp_rpt(ParmValue,MeasureDT) select top 1 cast(ParmValue as varchar), MeasureDT from tb_mp_'+@mp4+' where measuredt>=''''''+cast(@MeasureDT as varchar)+'''''' order by measuredt '''+char(10)
		SET @strSQL2  = @strSQL2 + N'SET @str = @str + N'' else   insert into tb_mp_temp_rpt(ParmValue,MeasureDT)  values(''''-'''',''''2009-06-15'''')'''+char(10)
		--SET @strSQl  = @strSQL + N'print @str'+char(10)
		SET @strSQL2  = @strSQL2 + N'exec(@str)'+char(10)
	end
end	
if(not @mp5 is null)
begin
	if(@mp5='MeasureDT')
	begin
		SET @strSQL2  = @strSQL2 + N'insert into tb_mp_temp_rpt(ParmValue,MeasureDT) values(''''+cast(datepart(hh,@MeasureDT) as varchar)+'':''+cast(datepart(mi,@MeasureDT) as varchar)+'''',''2009-06-15'')'+char(10)	
	end
	if(@mp5='dur')
	begin
		SET @strSQL2  = @strSQL2 + N'insert into tb_mp_temp_rpt(ParmValue,MeasureDT) values(''-'',''2009-06-15'')'+char(10)
	end
	if(@mp5<>'dur' and @mp5<>'MeasureDT')
	begin
		SET @strSQL2  = @strSQL2 + N'SET @str = N'''''+char(10)
		SET @strSQL2  = @strSQL2 + N'SET @str = @str + N''if exists( select ParmValue , MeasureDT from tb_mp_'+@mp5+' where measuredt>=''''''+cast(@MeasureDT as varchar)+'''''') insert into tb_mp_temp_rpt(ParmValue,MeasureDT) select top 1 cast(ParmValue as varchar), MeasureDT from tb_mp_'+@mp5+' where measuredt>=''''''+cast(@MeasureDT as varchar)+'''''' order by measuredt '''+char(10)
		SET @strSQL2  = @strSQL2 + N'SET @str = @str + N'' else   insert into tb_mp_temp_rpt(ParmValue,MeasureDT)  values(''''-'''',''''2009-06-15'''')'''+char(10)
		--SET @strSQl  = @strSQL + N'print @str'+char(10)
		SET @strSQL2  = @strSQL2 + N'exec(@str)'+char(10)
	end
end	
if(not @mp6 is null)
begin
	if(@mp6='MeasureDT')
	begin
		SET @strSQL2  = @strSQL2 + N'insert into tb_mp_temp_rpt(ParmValue,MeasureDT) values(''''+cast(datepart(hh,@MeasureDT) as varchar)+'':''+cast(datepart(mi,@MeasureDT) as varchar)+'''',''2009-06-15'')'+char(10)	
	end
	if(@mp6='dur')
	begin
		SET @strSQL2  = @strSQL2 + N'insert into tb_mp_temp_rpt(ParmValue,MeasureDT) values(''-'',''2009-06-15'')'+char(10)
	end
	if(@mp6<>'dur' and @mp6<>'MeasureDT')
	begin
		SET @strSQL2  = @strSQL2 + N'SET @str = N'''''+char(10)
		SET @strSQL2  = @strSQL2 + N'SET @str = @str + N''if exists( select ParmValue , MeasureDT from tb_mp_'+@mp6+' where measuredt>=''''''+cast(@MeasureDT as varchar)+'''''') insert into tb_mp_temp_rpt(ParmValue,MeasureDT) select top 1 cast(ParmValue as varchar), MeasureDT from tb_mp_'+@mp6+' where measuredt>=''''''+cast(@MeasureDT as varchar)+'''''' order by measuredt '''+char(10)
		SET @strSQL2  = @strSQL2 + N'SET @str = @str + N'' else   insert into tb_mp_temp_rpt(ParmValue,MeasureDT)  values(''''-'''',''''2009-06-15'''')'''+char(10)
		--SET @strSQl  = @strSQL + N'print @str'+char(10)
		SET @strSQL2  = @strSQL2 + N'exec(@str)'+char(10)
	end
end	
SET @strSQL2  = @strSQL2 + N'end'+char(10)
SET @strSQL2  = @strSQL2 + N'set @lastParmValue = @ParmValue'+char(10)	
SET @strSQL2  = @strSQL2 + N'set @lastMeasureDT = @MeasureDT'+char(10)	
SET @strSQL2  = @strSQL2 + N'end'+char(10)



SET @strSQL2  = @strSQL2 + N'FETCH curRunStatus into @ParmValue,@MeasureDT'+char(10)	
SET @strSQL2  = @strSQL2 + N'end'+char(10)


SET @strSQL2  = @strSQL2 + N'close curRunStatus'+char(10)
SET @strSQL2  = @strSQL2 + N'DEALLOCATE curRunStatus'+char(10)

---------------------------------
SET @strSQL2 = @strSQL2 + N'if EXISTS( select *  from TB_MP_'+@mainmp+N' where measuredt between '''+@starttime+N''' and ''' +@endtime+ N''' ) ' +char(10)
SET @strSQL2  = @strSQL2 + N'begin'+char(10)

if(@type='+')
SET @strSQL2 = @strSQL2 + N'if NOT EXISTS( select *  from TB_MP_'+@mainmp+N' where measuredt between '''+@starttime+N''' and ''' +@endtime+ N''' and ParmValue=0) ' +char(10)
else
SET @strSQL2 = @strSQL2 + N'if NOT EXISTS( select *  from TB_MP_'+@mainmp+N' where measuredt between '''+@starttime+N''' and ''' +@endtime+ N''' and ParmValue=1) ' +char(10)

SET @strSQL2  = @strSQL2 + N'begin'+char(10)

SET @strSQL2  = @strSQL2 + N'set @count = @count+1'+char(10)
SET @strSQL2  = @strSQL2 + N'insert into tb_mp_temp_rpt(ParmValue,MeasureDT) values(''未停机'',''2009-06-15'')'+char(10)

if(not @mp1 is null)
SET @strSQL2  = @strSQL2 + N'insert into tb_mp_temp_rpt(ParmValue,MeasureDT) values(''-'',''2009-06-15'')'+char(10)

if(not @mp2 is null)
SET @strSQL2  = @strSQL2 + N'insert into tb_mp_temp_rpt(ParmValue,MeasureDT) values(''-'',''2009-06-15'')'+char(10)

if(not @mp3 is null)
SET @strSQL2  = @strSQL2 + N'insert into tb_mp_temp_rpt(ParmValue,MeasureDT) values(''-'',''2009-06-15'')'+char(10)

if(not @mp4 is null)
SET @strSQL2  = @strSQL2 + N'insert into tb_mp_temp_rpt(ParmValue,MeasureDT) values(''-'',''2009-06-15'')'+char(10)

if(not @mp5 is null)
SET @strSQL2  = @strSQL2 + N'insert into tb_mp_temp_rpt(ParmValue,MeasureDT) values(''-'',''2009-06-15'')'+char(10)

if(not @mp6 is null)
SET @strSQL2  = @strSQL2 + N'insert into tb_mp_temp_rpt(ParmValue,MeasureDT) values(''-'',''2009-06-15'')'+char(10)

SET @strSQL2  = @strSQL2 + N'end'+char(10)

if(@type='+')
SET @strSQL2 = @strSQL2 + N'if NOT EXISTS( select *  from TB_MP_'+@mainmp+N' where measuredt between '''+@starttime+N''' and ''' +@endtime+ N''' and ParmValue=1) ' +char(10)
else
SET @strSQL2 = @strSQL2 + N'if NOT EXISTS( select *  from TB_MP_'+@mainmp+N' where measuredt between '''+@starttime+N''' and ''' +@endtime+ N''' and ParmValue=0) ' +char(10)

SET @strSQL2  = @strSQL2 + N'begin'+char(10)

SET @strSQL2  = @strSQL2 + N'set @count = @count+1'+char(10)
SET @strSQL2  = @strSQL2 + N'insert into tb_mp_temp_rpt(ParmValue,MeasureDT) values(''未开机'',''2009-06-15'')'+char(10)

if(not @mp1 is null)
SET @strSQL2  = @strSQL2 + N'insert into tb_mp_temp_rpt(ParmValue,MeasureDT) values(''-'',''2009-06-15'')'+char(10)

if(not @mp2 is null)
SET @strSQL2  = @strSQL2 + N'insert into tb_mp_temp_rpt(ParmValue,MeasureDT) values(''-'',''2009-06-15'')'+char(10)

if(not @mp3 is null)
SET @strSQL2  = @strSQL2 + N'insert into tb_mp_temp_rpt(ParmValue,MeasureDT) values(''-'',''2009-06-15'')'+char(10)

if(not @mp4 is null)
SET @strSQL2  = @strSQL2 + N'insert into tb_mp_temp_rpt(ParmValue,MeasureDT) values(''-'',''2009-06-15'')'+char(10)

if(not @mp5 is null)
SET @strSQL2  = @strSQL2 + N'insert into tb_mp_temp_rpt(ParmValue,MeasureDT) values(''-'',''2009-06-15'')'+char(10)

if(not @mp6 is null)
SET @strSQL2  = @strSQL2 + N'insert into tb_mp_temp_rpt(ParmValue,MeasureDT) values(''-'',''2009-06-15'')'+char(10)

SET @strSQL2  = @strSQL2 + N'end'+char(10)

SET @strSQL2  = @strSQL2 + N'end'+char(10)


---------------------------------
if(@type='+')
SET @strSQL2  = @strSQL2 + N'if (@lastParmValue=1 and @ParmValue=1)'+char(10)
else
SET @strSQL2  = @strSQL2 + N'if (@lastParmValue=0 and @ParmValue=0)'+char(10)
SET @strSQL2  = @strSQL2 + N'begin'+char(10)
SET @strSQL2  = @strSQL2 + N'set @count = @count+1'+char(10)
--SET @strSQL2  = @strSQL2 + N'insert into tb_mp_temp_rpt(ParmValue,MeasureDT) values(''持续运行'',''2009-06-15'')'+char(10)
SET @strSQL2  = @strSQL2 + N'insert into tb_mp_temp_rpt(ParmValue,MeasureDT) values(''-'',''2009-06-15'')'+char(10)
if(not @mp1 is null)
begin
	if(@mp1='dur')
	begin
		SET @strSQL2  = @strSQL2 + N'insert into tb_mp_temp_rpt(ParmValue,MeasureDT) values(''''+cast(datediff(mi,@lastMeasureDT,'''+@endtime+N''')/60 as varchar)+'':''+cast(datediff(mi,@lastMeasureDT,'''+@endtime+N''')%60 as varchar)+'''',''2009-06-15'')'+char(10)
	end
	else
	begin
		SET @strSQL2  = @strSQL2 + N'insert into tb_mp_temp_rpt(ParmValue,MeasureDT) values(''-'',''2009-06-15'')'+char(10)
	end
end
if(not @mp2 is null)
begin
	if(@mp2='dur')
	begin
		SET @strSQL2  = @strSQL2 + N'insert into tb_mp_temp_rpt(ParmValue,MeasureDT) values(''''+cast(datediff(mi,@lastMeasureDT,'''+@endtime+N''')/60 as varchar)+'':''+cast(datediff(mi,@lastMeasureDT,'''+@endtime+N''')%60 as varchar)+'''',''2009-06-15'')'+char(10)
	end
	else
	begin
		SET @strSQL2  = @strSQL2 + N'insert into tb_mp_temp_rpt(ParmValue,MeasureDT) values(''-'',''2009-06-15'')'+char(10)
	end
end
if(not @mp3 is null)
begin
	if(@mp3='dur')
	begin
		SET @strSQL2  = @strSQL2 + N'insert into tb_mp_temp_rpt(ParmValue,MeasureDT) values(''''+cast(datediff(mi,@lastMeasureDT,'''+@endtime+N''')/60 as varchar)+'':''+cast(datediff(mi,@lastMeasureDT,'''+@endtime+N''')%60 as varchar)+'''',''2009-06-15'')'+char(10)
	end
	else
	begin
		SET @strSQL2  = @strSQL2 + N'insert into tb_mp_temp_rpt(ParmValue,MeasureDT) values(''-'',''2009-06-15'')'+char(10)
	end
end
if(not @mp4 is null)
begin
	if(@mp4='dur')
	begin
		SET @strSQL2  = @strSQL2 + N'insert into tb_mp_temp_rpt(ParmValue,MeasureDT) values(''''+cast(datediff(mi,@lastMeasureDT,'''+@endtime+N''')/60 as varchar)+'':''+cast(datediff(mi,@lastMeasureDT,'''+@endtime+N''')%60 as varchar)+'''',''2009-06-15'')'+char(10)
	end
	else
	begin
		SET @strSQL2  = @strSQL2 + N'insert into tb_mp_temp_rpt(ParmValue,MeasureDT) values(''-'',''2009-06-15'')'+char(10)
	end
end
if(not @mp5 is null)
begin
	if(@mp5='dur')
	begin
		SET @strSQL2  = @strSQL2 + N'insert into tb_mp_temp_rpt(ParmValue,MeasureDT) values(''''+cast(datediff(mi,@lastMeasureDT,'''+@endtime+N''')/60 as varchar)+'':''+cast(datediff(mi,@lastMeasureDT,'''+@endtime+N''')%60 as varchar)+'''',''2009-06-15'')'+char(10)
	end
	else
	begin
		SET @strSQL2  = @strSQL2 + N'insert into tb_mp_temp_rpt(ParmValue,MeasureDT) values(''-'',''2009-06-15'')'+char(10)
	end
end
if(not @mp6 is null)
begin
	if(@mp6='dur')
	begin
		SET @strSQL2  = @strSQL2 + N'insert into tb_mp_temp_rpt(ParmValue,MeasureDT) values(''''+cast(datediff(mi,@lastMeasureDT,'''+@endtime+N''')/60 as varchar)+'':''+cast(datediff(mi,@lastMeasureDT,'''+@endtime+N''')%60 as varchar)+'''',''2009-06-15'')'+char(10)
	end
	else
	begin
		SET @strSQL2  = @strSQL2 + N'insert into tb_mp_temp_rpt(ParmValue,MeasureDT) values(''-'',''2009-06-15'')'+char(10)
	end
end
SET @strSQL2  = @strSQL2 + N'end'+char(10)


SET @strSQL3 = N''
SET @strSQL3  = @strSQL3 + N'set @count = @count+1'+char(10)
SET @strSQL3  = @strSQL3 + N'while(@count<='+cast(@rownum as varchar)+N')'+char(10)
SET @strSQL3  = @strSQL3 + N'begin'+char(10)	
SET @strSQL3  = @strSQL3 + N'SET @str  =N''insert into tb_mp_temp_rpt(ParmValue,MeasureDT) values(''''-'''',''''2009-06-15'''')'''+char(10)	
if(not @mp1 is null) SET @strSQL3  = @strSQL3 + N'SET @str  = @str + N''insert into tb_mp_temp_rpt(ParmValue,MeasureDT) values(''''-'''',''''2009-06-15'''')'''+char(10)	
if(not @mp2 is null) SET @strSQL3  = @strSQL3 + N'SET @str  = @str + N''insert into tb_mp_temp_rpt(ParmValue,MeasureDT) values(''''-'''',''''2009-06-15'''')'''+char(10)	
if(not @mp3 is null) SET @strSQL3  = @strSQL3 + N'SET @str  = @str + N''insert into tb_mp_temp_rpt(ParmValue,MeasureDT) values(''''-'''',''''2009-06-15'''')'''+char(10)	
if(not @mp4 is null) SET @strSQL3  = @strSQL3 + N'SET @str  = @str + N''insert into tb_mp_temp_rpt(ParmValue,MeasureDT) values(''''-'''',''''2009-06-15'''')'''+char(10)	
if(not @mp5 is null) SET @strSQL3  = @strSQL3 + N'SET @str  = @str + N''insert into tb_mp_temp_rpt(ParmValue,MeasureDT) values(''''-'''',''''2009-06-15'''')'''+char(10)	
if(not @mp6 is null) SET @strSQL3  = @strSQL3 + N'SET @str  = @str + N''insert into tb_mp_temp_rpt(ParmValue,MeasureDT) values(''''-'''',''''2009-06-15'''')'''+char(10)	
SET @strSQL3  = @strSQL3 + N'exec(@str)'+char(10)
SET @strSQL3  = @strSQL3 + N'set @count = @count+1'+char(10)
SET @strSQL3  = @strSQL3 + N'end'+char(10)


print @strSQL1
print @strSQL2
print @strSQL3
exec ( @strSQL1+@strSQL2+@strSQL3)

end
else
	begin
		while(@i<=@colcount*@rownum)
		begin
		print '-'
		insert into TB_MP_TEMP_RPT(ParmValue,MeasureDT) values('-',@starttime)
		set @i = @i+1
		end
	end


GO

-- ----------------------------
-- Procedure structure for sp_runrecordold
-- ----------------------------
DROP PROCEDURE [dbo].[sp_runrecordold]
GO



CREATE PROCEDURE [dbo].[sp_runrecordold]
@starttime varchar(20),@endtime varchar(20),@type varchar(80),@mp varchar(800),@rownum int
as
Declare @strSQL1 nvarchar(4000)
Declare @strSQL2 nvarchar(4000)
Declare @strSQL3 nvarchar(4000)

declare @count int
declare @ParmValue money
declare @MeasureDT datetime
declare @lastParmValue money
declare @lastMeasureDT datetime
declare @colcount int
declare @i int

declare @idx int
declare @idxo int

declare @mainmp varchar(200)
declare @mp1 varchar(200)
declare @mp2 varchar(200)
declare @mp3 varchar(200)
declare @mp4 varchar(200)
declare @mp5 varchar(200)
declare @mp6 varchar(200)
declare @mp7 varchar(200)
declare @mp8 varchar(200)
declare @mp9 varchar(200)
declare @mp10 varchar(200)

--delete from tb_mp_temp_rpt
select @colcount = count(*) from dbo.FUN_split(@mp,',') 
set @i = 1

if(SUBSTRING(@mp,1,1)!='-')
	begin

set @idx = charindex(',',@mp,1)
if(@idx>0) 
begin
	set @mainmp = SUBSTRING(@mp,1,@idx-1)
	print @mainmp

	set @mp= SUBSTRING(@mp,@idx+1,len(@mp)) 
	print @mp

	set @idx = charindex(',',@mp,1)
	if(@idx>0)
	begin
		set @mp1 = SUBSTRING(@mp,1,@idx-1)
		print @mp1	


		set @mp= SUBSTRING(@mp,@idx+1,len(@mp)) 
		print @mp
	
		set @idx = charindex(',',@mp,1)
		if(@idx>0)
		begin
			set @mp2 = SUBSTRING(@mp,1,@idx-1)
			print @mp2	
	
			set @mp= SUBSTRING(@mp,@idx+1,len(@mp)) 
			print @mp


			set @idx = charindex(',',@mp,1)
			if(@idx>0)
			begin
				set @mp3 = SUBSTRING(@mp,1,@idx-1)
				print @mp3	
			
				set @mp= SUBSTRING(@mp,@idx+1,len(@mp)) 
				print @mp
	
				set @idx = charindex(',',@mp,1)
				if(@idx>0)
				begin
					set @mp4 = SUBSTRING(@mp,1,@idx-1)
					print @mp4	
			
					set @mp= SUBSTRING(@mp,@idx+1,len(@mp)) 
					print @mp

					set @idx = charindex(',',@mp,1)
					if(@idx>0)
					begin
						set @mp5 = SUBSTRING(@mp,1,@idx-1)
						print @mp5	
				
						set @mp= SUBSTRING(@mp,@idx+1,len(@mp)) 
						print @mp
	
						set @idx = charindex(',',@mp,1)
						if(@idx>0)
						begin
							set @mp6 = SUBSTRING(@mp,1,@idx-1)
							print @mp6	
						end 
						else
						begin
							set @mp6 = @mp
							print @mp6	
						end
	
					end
					else
					begin
						set @mp5 = @mp
						print @mp5	
					end
				end
				else
				begin
					set @mp4 = @mp
					print @mp4	
				end
			end
			else
			begin
				set @mp3 = @mp
				print @mp3	
			end
	
		end
		else
		begin
			set @mp2= @mp
			print @mp2	
		end

	end
	else
	begin
		set @mp1 = @mp
		print @mp1	
	end
end
else
begin
	set @mainmp=@mp
	print @mainmp
end



SET @strSQL1 = N''
SET @strSQL1 = @strSQL1+N'declare @count int'+char(10)
SET @strSQL1 = @strSQL1+N'declare @ParmValue money'+char(10)
SET @strSQL1 = @strSQL1+N'declare @MeasureDT datetime'+char(10)
SET @strSQL1 = @strSQL1+N'declare @lastParmValue money'+char(10)
SET @strSQL1 = @strSQL1+N'declare @lastMeasureDT datetime'+char(10)
SET @strSQL1 = @strSQL1+N'declare @str nvarchar(4000)'+char(10)

--寻找今天第一条记录
SET @strSQL1 = @strSQL1 + N'select top 1 @lastParmValue =ParmValue ,@lastMeasureDT=MeasureDT from TB_MP_'+@mainmp+N' where measuredt between '''+@starttime+N''' and ''' +@endtime+ N''' order by measuredt asc ' +char(10)
if(@type='+')
begin
	--如果第一天记录代表运行
	SET @strSQL1 = @strSQL1 + N'if(@lastParmValue=1)'+char(10)
	SET @strSQL1 = @strSQL1 + N'begin'+char(10)
	--寻找今天之前最后一条记录
	SET @strSQL1 = @strSQL1 + N'select top 1 @lastParmValue =ParmValue ,@lastMeasureDT=MeasureDT from TB_MP_'+@mainmp+N' where measuredt < '''+@starttime+N'''  order by measuredt desc' +char(10)
	--如果记录存在且为运行
	SET @strSQL1 = @strSQL1 + N'if(not @lastParmValue is null and @lastParmValue=1)'+char(10)
	SET @strSQL1 = @strSQL1 + N'begin'+char(10)
	--做一条假的开机记录
	SET @strSQL1 = @strSQL1 + N'set @lastMeasureDT='''+@starttime+''''+char(10)
	SET @strSQL1 = @strSQL1 + N'set @lastParmValue=1 '+char(10)
	SET @strSQL1  = @strSQL1 + N'end'+char(10)
	SET @strSQL1  = @strSQL1 + N'end'+char(10)
end
else
begin
	SET @strSQL1 = @strSQL1 + N'if(@lastParmValue=0)'+char(10)
	SET @strSQL1 = @strSQL1 + N'begin'+char(10)
	SET @strSQL1 = @strSQL1 + N'select top 1 @lastParmValue =ParmValue ,@lastMeasureDT=MeasureDT from TB_MP_'+@mainmp+N' where measuredt < '''+@starttime+N''' order by measuredt desc' +char(10)
	--如果记录存在且为运行
	SET @strSQL1 = @strSQL1 + N'if(not @lastParmValue is null and @lastParmValue=0)'+char(10)
	SET @strSQL1 = @strSQL1 + N'begin'+char(10)
	--做一条假的开机记录
	SET @strSQL1 = @strSQL1 + N'set @lastMeasureDT='''+@starttime+''''+char(10)
	SET @strSQL1 = @strSQL1 + N'set @lastParmValue=0 '+char(10)
	SET @strSQL1  = @strSQL1 + N'end'+char(10)
	SET @strSQL1  = @strSQL1 + N'end'+char(10)
end

SET @strSQL1 = @strSQL1 + N'set @count=0'+char(10)

SET @strSQL1 = @strSQL1 + N'DECLARE curRunStatus cursor for select ParmValue,MeasureDT from TB_MP_' + @mainmp+N' ' +char(10)
SET @strSQL1 = @strSQL1 + N'where measuredt between ''' +cast(@starttime as varchar(20)) +N''' and '''+cast(@endtime as varchar(20))+N''' order by MeasureDT asc'+char(10)
SET @strSQL1 = @strSQL1 + N'open curRunStatus'+char(10)
SET @strSQL1 = @strSQL1 + N'FETCH curRunStatus into @ParmValue,@MeasureDT'+char(10)
SET @strSQL1 = @strSQL1 + N'while (@@fetch_status=0)'+char(10)
SET @strSQL1 = @strSQL1 + N'begin'+char(10)
SET @strSQL1 = @strSQL1 + N'if(@lastParmValue is null)'+char(10)
SET @strSQL1 = @strSQL1 + N'begin'+char(10)
SET @strSQL1 = @strSQL1 + N'set @lastParmValue = @ParmValue'+char(10)	
SET @strSQL1 = @strSQL1 + N'set @lastMeasureDT = @MeasureDT'+char(10)	
SET @strSQL1 = @strSQL1 + N'end'+char(10)

SET @strSQL1 = @strSQL1 + N'if(@ParmValue<>@lastParmValue)'+char(10)
SET @strSQL1 = @strSQL1 + N'begin'+char(10)
SET @strSQL1 = @strSQL1 + N'set  @count=@count+1'+char(10)	

if(@type='+')
SET @strSQL1  = @strSQL1 + N'if(@ParmValue=0)'++char(10)	
else
SET @strSQL1  = @strSQL1 + N'if(@ParmValue=1)'++char(10)	

SET @strSQL1  = @strSQL1 + N'begin'+char(10)
SET @strSQL1  = @strSQL1 + N'insert into tb_mp_temp_rpt(ParmValue,MeasureDT) values(''停止'',''2009-06-15'')'+char(10)

if(not @mp1 is null)
begin
	if(@mp1='MeasureDT')
	begin
		SET @strSQL1  = @strSQL1 + N'insert into tb_mp_temp_rpt(ParmValue,MeasureDT) values(''''+cast(datepart(hh,@MeasureDT) as varchar)+'':''+cast(datepart(mi,@MeasureDT) as varchar)+'''',''2009-06-15'')'+char(10)	
	end
	if(@mp1='dur')
	begin

		SET @strSQL1  = @strSQL1 + N'insert into tb_mp_temp_rpt(ParmValue,MeasureDT) values(''''+cast(datediff(mi,@lastMeasureDT,@MeasureDT)/60 as varchar)+'':''+cast(datediff(mi,@lastMeasureDT,@MeasureDT)%60 as varchar)+'''',''2009-06-15'')'+char(10)
	end
	if(@mp1<>'dur' and @mp1<>'MeasureDT')
	begin
		SET @strSQL1  = @strSQL1 + N'SET @str = N'''''+char(10)
		SET @strSQL1  = @strSQL1 + N'SET @str = @str + N''if exists( select ParmValue , MeasureDT from tb_mp_'+@mp1+' where measuredt>=''''''+cast(@MeasureDT as varchar)+'''''') insert into tb_mp_temp_rpt(ParmValue,MeasureDT) select top 1 cast(ParmValue as varchar), MeasureDT from tb_mp_'+@mp1+' where measuredt>=''''''+cast(@MeasureDT as varchar)+'''''' order by measuredt '''+char(10)
		SET @strSQL1  = @strSQL1 + N'SET @str = @str + N'' else   insert into tb_mp_temp_rpt(ParmValue,MeasureDT)  values(''''-'''',''''2009-06-15'''')'''+char(10)
		--SET @strSQl  = @strSQL + N'print @str'+char(10)
		SET @strSQL1  = @strSQL1 + N'exec(@str)'+char(10)
	end
end	
if(not @mp2 is null)
begin
	if(@mp2='MeasureDT')
	begin
		SET @strSQL1  = @strSQL1 + N'insert into tb_mp_temp_rpt(ParmValue,MeasureDT) values(''''+cast(datepart(hh,@MeasureDT) as varchar)+'':''+cast(datepart(mi,@MeasureDT) as varchar)+'''',''2009-06-15'')'+char(10)	
	end
	if(@mp2='dur')
	begin
		SET @strSQL1  = @strSQL1 + N'insert into tb_mp_temp_rpt(ParmValue,MeasureDT) values(''''+cast(datediff(mi,@lastMeasureDT,@MeasureDT)/60 as varchar)+'':''+cast(datediff(mi,@lastMeasureDT,@MeasureDT)%60 as varchar)+'''',''2009-06-15'')'+char(10)
	end
	if(@mp2<>'dur' and @mp2<>'MeasureDT')
	begin
		SET @strSQL1  = @strSQL1 + N'SET @str = N'''''+char(10)
		SET @strSQL1  = @strSQL1 + N'SET @str = @str + N''if exists( select ParmValue , MeasureDT from tb_mp_'+@mp2+' where measuredt>=''''''+cast(@MeasureDT as varchar)+'''''') insert into tb_mp_temp_rpt(ParmValue,MeasureDT) select top 1 cast(ParmValue as varchar), MeasureDT from tb_mp_'+@mp2+' where measuredt>=''''''+cast(@MeasureDT as varchar)+'''''' order by measuredt '''+char(10)
		SET @strSQL1  = @strSQL1 + N'SET @str = @str + N'' else   insert into tb_mp_temp_rpt(ParmValue,MeasureDT)  values(''''-'''',''''2009-06-15'''')'''+char(10)
		--SET @strSQl  = @strSQL + N'print @str'+char(10)
		SET @strSQL1  = @strSQL1 + N'exec(@str)'+char(10)
	end
end	
if(not @mp3 is null)
begin
	if(@mp3='MeasureDT')
	begin
		SET @strSQL1  = @strSQL1 + N'insert into tb_mp_temp_rpt(ParmValue,MeasureDT) values(''''+cast(datepart(hh,@MeasureDT) as varchar)+'':''+cast(datepart(mi,@MeasureDT) as varchar)+'''',''2009-06-15'')'+char(10)	
	end
	if(@mp3='dur')
	begin
		SET @strSQL1  = @strSQL1 + N'insert into tb_mp_temp_rpt(ParmValue,MeasureDT) values(''''+cast(datediff(mi,@lastMeasureDT,@MeasureDT)/60 as varchar)+'':''+cast(datediff(mi,@lastMeasureDT,@MeasureDT)%60 as varchar)+'''',''2009-06-15'')'+char(10)
	end
	if(@mp3<>'dur' and @mp3<>'MeasureDT')
	begin
		SET @strSQL1  = @strSQL1 + N'SET @str = N'''''+char(10)
		SET @strSQL1  = @strSQL1 + N'SET @str = @str + N''if exists( select ParmValue , MeasureDT from tb_mp_'+@mp3+' where measuredt>=''''''+cast(@MeasureDT as varchar)+'''''') insert into tb_mp_temp_rpt(ParmValue,MeasureDT) select top 1 cast(ParmValue as varchar), MeasureDT from tb_mp_'+@mp3+' where measuredt>=''''''+cast(@MeasureDT as varchar)+'''''' order by measuredt '''+char(10)
		SET @strSQL1  = @strSQL1 + N'SET @str = @str + N'' else   insert into tb_mp_temp_rpt(ParmValue,MeasureDT)  values(''''-'''',''''2009-06-15'''')'''+char(10)
		--SET @strSQl  = @strSQL + N'print @str'+char(10)
		SET @strSQL1  = @strSQL1 + N'exec(@str)'+char(10)
	end
end	
if(not @mp4 is null)
begin
	if(@mp4='MeasureDT')
	begin
		SET @strSQL1  = @strSQL1 + N'insert into tb_mp_temp_rpt(ParmValue,MeasureDT) values(''''+cast(datepart(hh,@MeasureDT) as varchar)+'':''+cast(datepart(mi,@MeasureDT) as varchar)+'''',''2009-06-15'')'+char(10)	
	end
	if(@mp4='dur')
	begin
		SET @strSQL1  = @strSQL1 + N'insert into tb_mp_temp_rpt(ParmValue,MeasureDT) values(''''+cast(datediff(mi,@lastMeasureDT,@MeasureDT)/60 as varchar)+'':''+cast(datediff(mi,@lastMeasureDT,@MeasureDT)%60 as varchar)+'''',''2009-06-15'')'+char(10)
	end
	if(@mp4<>'dur' and @mp4<>'MeasureDT')
	begin
		SET @strSQL1  = @strSQL1 + N'SET @str = N'''''+char(10)
		SET @strSQL1  = @strSQL1 + N'SET @str = @str + N''if exists( select ParmValue , MeasureDT from tb_mp_'+@mp4+' where measuredt>=''''''+cast(@MeasureDT as varchar)+'''''') insert into tb_mp_temp_rpt(ParmValue,MeasureDT) select top 1 cast(ParmValue as varchar), MeasureDT from tb_mp_'+@mp4+' where measuredt>=''''''+cast(@MeasureDT as varchar)+'''''' order by measuredt '''+char(10)
		SET @strSQL1  = @strSQL1 + N'SET @str = @str + N'' else   insert into tb_mp_temp_rpt(ParmValue,MeasureDT)  values(''''-'''',''''2009-06-15'''')'''+char(10)
		--SET @strSQl  = @strSQL + N'print @str'+char(10)
		SET @strSQL1  = @strSQL1 + N'exec(@str)'+char(10)
	end
end	
if(not @mp5 is null)
begin
	if(@mp5='MeasureDT')
	begin
		SET @strSQL1  = @strSQL1 + N'insert into tb_mp_temp_rpt(ParmValue,MeasureDT) values(''''+cast(datepart(hh,@MeasureDT) as varchar)+'':''+cast(datepart(mi,@MeasureDT) as varchar)+'''',''2009-06-15'')'+char(10)	
	end
	if(@mp5='dur')
	begin
		SET @strSQL1  = @strSQL1 + N'insert into tb_mp_temp_rpt(ParmValue,MeasureDT) values(''''+cast(datediff(mi,@lastMeasureDT,@MeasureDT)/60 as varchar)+'':''+cast(datediff(mi,@lastMeasureDT,@MeasureDT)%60 as varchar)+'''',''2009-06-15'')'+char(10)
	end
	if(@mp5<>'dur' and @mp5<>'MeasureDT')
	begin
		SET @strSQL1  = @strSQL1 + N'SET @str = N'''''+char(10)
		SET @strSQL1  = @strSQL1 + N'SET @str = @str + N''if exists( select ParmValue , MeasureDT from tb_mp_'+@mp5+' where measuredt>=''''''+cast(@MeasureDT as varchar)+'''''') insert into tb_mp_temp_rpt(ParmValue,MeasureDT) select top 1 cast(ParmValue as varchar), MeasureDT from tb_mp_'+@mp5+' where measuredt>=''''''+cast(@MeasureDT as varchar)+'''''' order by measuredt '''+char(10)
		SET @strSQL1  = @strSQL1 + N'SET @str = @str + N'' else   insert into tb_mp_temp_rpt(ParmValue,MeasureDT)  values(''''-'''',''''2009-06-15'''')'''+char(10)
		--SET @strSQl  = @strSQL + N'print @str'+char(10)
		SET @strSQL1  = @strSQL1 + N'exec(@str)'+char(10)
	end
end	
if(not @mp6 is null)
begin
	if(@mp6='MeasureDT')
	begin
		SET @strSQL1  = @strSQL1 + N'insert into tb_mp_temp_rpt(ParmValue,MeasureDT) values(''''+cast(datepart(hh,@MeasureDT) as varchar)+'':''+cast(datepart(mi,@MeasureDT) as varchar)+'''',''2009-06-15'')'+char(10)	
	end
	if(@mp6='dur')
	begin
		SET @strSQL1  = @strSQL1 + N'insert into tb_mp_temp_rpt(ParmValue,MeasureDT) values(''''+cast(datediff(mi,@lastMeasureDT,@MeasureDT)/60 as varchar)+'':''+cast(datediff(mi,@lastMeasureDT,@MeasureDT)%60 as varchar)+'''',''2009-06-15'')'+char(10)
	end
	if(@mp6<>'dur' and @mp6<>'MeasureDT')
	begin
		SET @strSQL1  = @strSQL1 + N'SET @str = N'''''+char(10)
		SET @strSQL1  = @strSQL1 + N'SET @str = @str + N''if exists( select ParmValue , MeasureDT from tb_mp_'+@mp6+' where measuredt>=''''''+cast(@MeasureDT as varchar)+'''''') insert into tb_mp_temp_rpt(ParmValue,MeasureDT) select top 1 cast(ParmValue as varchar), MeasureDT from tb_mp_'+@mp6+' where measuredt>=''''''+cast(@MeasureDT as varchar)+'''''' order by measuredt '''+char(10)
		SET @strSQL1  = @strSQL1 + N'SET @str = @str + N'' else   insert into tb_mp_temp_rpt(ParmValue,MeasureDT)  values(''''-'''',''''2009-06-15'''')'''+char(10)
		--SET @strSQl  = @strSQL + N'print @str'+char(10)
		SET @strSQL1  = @strSQL1 + N'exec(@str)'+char(10)
	end
end	
SET @strSQL1  = @strSQL1 + N'end'+char(10)

SET @strSQL2 = N''
if(@type='+')	
SET @strSQL2  = @strSQL2 + N'if(@ParmValue=1)'+char(10)	
else
SET @strSQL2  = @strSQL2 + N'if(@ParmValue=0)'+char(10)	

SET @strSQL2  = @strSQL2 + N'begin'+char(10)
SET @strSQL2  = @strSQL2 + N'insert into tb_mp_temp_rpt(ParmValue,MeasureDT) values(''启动'',''2009-06-15'')'+char(10)	
if(not @mp1 is null)
begin
	if(@mp1='MeasureDT')
	begin
		SET @strSQL2  = @strSQL2 + N'insert into tb_mp_temp_rpt(ParmValue,MeasureDT) values(''''+cast(datepart(hh,@MeasureDT) as varchar)+'':''+cast(datepart(mi,@MeasureDT) as varchar)+'''',''2009-06-15'')'+char(10)	
	end
	if(@mp1='dur')
	begin
		SET @strSQL2  = @strSQL2 + N'insert into tb_mp_temp_rpt(ParmValue,MeasureDT) values(''-'',''2009-06-15'')'+char(10)
	end
	if(@mp1<>'dur' and @mp1<>'MeasureDT')
	begin
		SET @strSQL2  = @strSQL2 + N'SET @str = N'''''+char(10)
		SET @strSQL2  = @strSQL2 + N'SET @str = @str + N''if exists( select ParmValue , MeasureDT from tb_mp_'+@mp1+' where measuredt>=''''''+cast(@MeasureDT as varchar)+'''''') insert into tb_mp_temp_rpt(ParmValue,MeasureDT) select top 1 cast(ParmValue as varchar), MeasureDT from tb_mp_'+@mp1+' where measuredt>=''''''+cast(@MeasureDT as varchar)+'''''' order by measuredt '''+char(10)
		SET @strSQL2  = @strSQL2 + N'SET @str = @str + N'' else   insert into tb_mp_temp_rpt(ParmValue,MeasureDT)  values(''''-'''',''''2009-06-15'''')'''+char(10)
		--SET @strSQl  = @strSQL + N'print @str'+char(10)
		SET @strSQL2  = @strSQL2 + N'exec(@str)'+char(10)
	end
end	
if(not @mp2 is null)
begin
	if(@mp2='MeasureDT')
	begin
		SET @strSQL2  = @strSQL2 + N'insert into tb_mp_temp_rpt(ParmValue,MeasureDT) values(''''+cast(datepart(hh,@MeasureDT) as varchar)+'':''+cast(datepart(mi,@MeasureDT) as varchar)+'''',''2009-06-15'')'+char(10)	
	end
	if(@mp2='dur')
	begin
		SET @strSQL2  = @strSQL2 + N'insert into tb_mp_temp_rpt(ParmValue,MeasureDT) values(''-'',''2009-06-15'')'+char(10)
	end
	if(@mp2<>'dur' and @mp2<>'MeasureDT')
	begin
		SET @strSQL2  = @strSQL2 + N'SET @str = N'''''+char(10)
		SET @strSQL2  = @strSQL2 + N'SET @str = @str + N''if exists( select ParmValue , MeasureDT from tb_mp_'+@mp2+' where measuredt>=''''''+cast(@MeasureDT as varchar)+'''''') insert into tb_mp_temp_rpt(ParmValue,MeasureDT) select top 1 cast(ParmValue as varchar), MeasureDT from tb_mp_'+@mp2+' where measuredt>=''''''+cast(@MeasureDT as varchar)+'''''' order by measuredt '''+char(10)
		SET @strSQL2  = @strSQL2 + N'SET @str = @str + N'' else   insert into tb_mp_temp_rpt(ParmValue,MeasureDT)  values(''''-'''',''''2009-06-15'''')'''+char(10)
		--SET @strSQl  = @strSQL + N'print @str'+char(10)
		SET @strSQL2  = @strSQL2 + N'exec(@str)'+char(10)
	end
end	
if(not @mp3 is null)
begin
	if(@mp3='MeasureDT')
	begin
		SET @strSQL2  = @strSQL2 + N'insert into tb_mp_temp_rpt(ParmValue,MeasureDT) values(''''+cast(datepart(hh,@MeasureDT) as varchar)+'':''+cast(datepart(mi,@MeasureDT) as varchar)+'''',''2009-06-15'')'+char(10)	
	end
	if(@mp3='dur')
	begin
		SET @strSQL2  = @strSQL2 + N'insert into tb_mp_temp_rpt(ParmValue,MeasureDT) values(''-'',''2009-06-15'')'+char(10)
	end
	if(@mp3<>'dur' and @mp3<>'MeasureDT')
	begin
		SET @strSQL2  = @strSQL2 + N'SET @str = N'''''+char(10)
		SET @strSQL2  = @strSQL2 + N'SET @str = @str + N''if exists( select ParmValue , MeasureDT from tb_mp_'+@mp3+' where measuredt>=''''''+cast(@MeasureDT as varchar)+'''''') insert into tb_mp_temp_rpt(ParmValue,MeasureDT) select top 1 cast(ParmValue as varchar), MeasureDT from tb_mp_'+@mp3+' where measuredt>=''''''+cast(@MeasureDT as varchar)+'''''' order by measuredt '''+char(10)
		SET @strSQL2  = @strSQL2 + N'SET @str = @str + N'' else   insert into tb_mp_temp_rpt(ParmValue,MeasureDT)  values(''''-'''',''''2009-06-15'''')'''+char(10)
		--SET @strSQl  = @strSQL + N'print @str'+char(10)
		SET @strSQL2  = @strSQL2 + N'exec(@str)'+char(10)
	end
end	
if(not @mp4 is null)
begin
	if(@mp4='MeasureDT')
	begin
		SET @strSQL2  = @strSQL2 + N'insert into tb_mp_temp_rpt(ParmValue,MeasureDT) values(''''+cast(datepart(hh,@MeasureDT) as varchar)+'':''+cast(datepart(mi,@MeasureDT) as varchar)+'''',''2009-06-15'')'+char(10)	
	end
	if(@mp4='dur')
	begin
		SET @strSQL2  = @strSQL2 + N'insert into tb_mp_temp_rpt(ParmValue,MeasureDT) values(''-'',''2009-06-15'')'+char(10)
	end
	if(@mp4<>'dur' and @mp4<>'MeasureDT')
	begin
		SET @strSQL2  = @strSQL2 + N'SET @str = N'''''+char(10)
		SET @strSQL2  = @strSQL2 + N'SET @str = @str + N''if exists( select ParmValue , MeasureDT from tb_mp_'+@mp4+' where measuredt>=''''''+cast(@MeasureDT as varchar)+'''''') insert into tb_mp_temp_rpt(ParmValue,MeasureDT) select top 1 cast(ParmValue as varchar), MeasureDT from tb_mp_'+@mp4+' where measuredt>=''''''+cast(@MeasureDT as varchar)+'''''' order by measuredt '''+char(10)
		SET @strSQL2  = @strSQL2 + N'SET @str = @str + N'' else   insert into tb_mp_temp_rpt(ParmValue,MeasureDT)  values(''''-'''',''''2009-06-15'''')'''+char(10)
		--SET @strSQl  = @strSQL + N'print @str'+char(10)
		SET @strSQL2  = @strSQL2 + N'exec(@str)'+char(10)
	end
end	
if(not @mp5 is null)
begin
	if(@mp5='MeasureDT')
	begin
		SET @strSQL2  = @strSQL2 + N'insert into tb_mp_temp_rpt(ParmValue,MeasureDT) values(''''+cast(datepart(hh,@MeasureDT) as varchar)+'':''+cast(datepart(mi,@MeasureDT) as varchar)+'''',''2009-06-15'')'+char(10)	
	end
	if(@mp5='dur')
	begin
		SET @strSQL2  = @strSQL2 + N'insert into tb_mp_temp_rpt(ParmValue,MeasureDT) values(''-'',''2009-06-15'')'+char(10)
	end
	if(@mp5<>'dur' and @mp5<>'MeasureDT')
	begin
		SET @strSQL2  = @strSQL2 + N'SET @str = N'''''+char(10)
		SET @strSQL2  = @strSQL2 + N'SET @str = @str + N''if exists( select ParmValue , MeasureDT from tb_mp_'+@mp5+' where measuredt>=''''''+cast(@MeasureDT as varchar)+'''''') insert into tb_mp_temp_rpt(ParmValue,MeasureDT) select top 1 cast(ParmValue as varchar), MeasureDT from tb_mp_'+@mp5+' where measuredt>=''''''+cast(@MeasureDT as varchar)+'''''' order by measuredt '''+char(10)
		SET @strSQL2  = @strSQL2 + N'SET @str = @str + N'' else   insert into tb_mp_temp_rpt(ParmValue,MeasureDT)  values(''''-'''',''''2009-06-15'''')'''+char(10)
		--SET @strSQl  = @strSQL + N'print @str'+char(10)
		SET @strSQL2  = @strSQL2 + N'exec(@str)'+char(10)
	end
end	
if(not @mp6 is null)
begin
	if(@mp6='MeasureDT')
	begin
		SET @strSQL2  = @strSQL2 + N'insert into tb_mp_temp_rpt(ParmValue,MeasureDT) values(''''+cast(datepart(hh,@MeasureDT) as varchar)+'':''+cast(datepart(mi,@MeasureDT) as varchar)+'''',''2009-06-15'')'+char(10)	
	end
	if(@mp6='dur')
	begin
		SET @strSQL2  = @strSQL2 + N'insert into tb_mp_temp_rpt(ParmValue,MeasureDT) values(''-'',''2009-06-15'')'+char(10)
	end
	if(@mp6<>'dur' and @mp6<>'MeasureDT')
	begin
		SET @strSQL2  = @strSQL2 + N'SET @str = N'''''+char(10)
		SET @strSQL2  = @strSQL2 + N'SET @str = @str + N''if exists( select ParmValue , MeasureDT from tb_mp_'+@mp6+' where measuredt>=''''''+cast(@MeasureDT as varchar)+'''''') insert into tb_mp_temp_rpt(ParmValue,MeasureDT) select top 1 cast(ParmValue as varchar), MeasureDT from tb_mp_'+@mp6+' where measuredt>=''''''+cast(@MeasureDT as varchar)+'''''' order by measuredt '''+char(10)
		SET @strSQL2  = @strSQL2 + N'SET @str = @str + N'' else   insert into tb_mp_temp_rpt(ParmValue,MeasureDT)  values(''''-'''',''''2009-06-15'''')'''+char(10)
		--SET @strSQl  = @strSQL + N'print @str'+char(10)
		SET @strSQL2  = @strSQL2 + N'exec(@str)'+char(10)
	end
end	
SET @strSQL2  = @strSQL2 + N'end'+char(10)
SET @strSQL2  = @strSQL2 + N'set @lastParmValue = @ParmValue'+char(10)	
SET @strSQL2  = @strSQL2 + N'set @lastMeasureDT = @MeasureDT'+char(10)	
SET @strSQL2  = @strSQL2 + N'end'+char(10)



SET @strSQL2  = @strSQL2 + N'FETCH curRunStatus into @ParmValue,@MeasureDT'+char(10)	
SET @strSQL2  = @strSQL2 + N'end'+char(10)


SET @strSQL2  = @strSQL2 + N'close curRunStatus'+char(10)
SET @strSQL2  = @strSQL2 + N'DEALLOCATE curRunStatus'+char(10)

---------------------------------
if(@type='+')
SET @strSQL2  = @strSQL2 + N'if (@lastParmValue=1 and @ParmValue=1)'+char(10)
else
SET @strSQL2  = @strSQL2 + N'if (@lastParmValue=0 and @ParmValue=0)'+char(10)
SET @strSQL2  = @strSQL2 + N'begin'+char(10)
SET @strSQL2  = @strSQL2 + N'set @count = @count+1'+char(10)
SET @strSQL2  = @strSQL2 + N'insert into tb_mp_temp_rpt(ParmValue,MeasureDT) values(''占位'',''2009-06-15'')'+char(10)
if(not @mp1 is null)
begin
	if(@mp1='dur')
	begin
		SET @strSQL2  = @strSQL2 + N'insert into tb_mp_temp_rpt(ParmValue,MeasureDT) values(''''+cast(datediff(mi,@lastMeasureDT,'''+@endtime+N''')/60 as varchar)+'':''+cast(datediff(mi,@lastMeasureDT,'''+@endtime+N''')%60 as varchar)+'''',''2009-06-15'')'+char(10)
	end
	else
	begin
		SET @strSQL2  = @strSQL2 + N'insert into tb_mp_temp_rpt(ParmValue,MeasureDT) values(''-'',''2009-06-15'')'+char(10)
	end
end
if(not @mp2 is null)
begin
	if(@mp2='dur')
	begin
		SET @strSQL2  = @strSQL2 + N'insert into tb_mp_temp_rpt(ParmValue,MeasureDT) values(''''+cast(datediff(mi,@lastMeasureDT,'''+@endtime+N''')/60 as varchar)+'':''+cast(datediff(mi,@lastMeasureDT,'''+@endtime+N''')%60 as varchar)+'''',''2009-06-15'')'+char(10)
	end
	else
	begin
		SET @strSQL2  = @strSQL2 + N'insert into tb_mp_temp_rpt(ParmValue,MeasureDT) values(''-'',''2009-06-15'')'+char(10)
	end
end
if(not @mp3 is null)
begin
	if(@mp3='dur')
	begin
		SET @strSQL2  = @strSQL2 + N'insert into tb_mp_temp_rpt(ParmValue,MeasureDT) values(''''+cast(datediff(mi,@lastMeasureDT,'''+@endtime+N''')/60 as varchar)+'':''+cast(datediff(mi,@lastMeasureDT,'''+@endtime+N''')%60 as varchar)+'''',''2009-06-15'')'+char(10)
	end
	else
	begin
		SET @strSQL2  = @strSQL2 + N'insert into tb_mp_temp_rpt(ParmValue,MeasureDT) values(''-'',''2009-06-15'')'+char(10)
	end
end
if(not @mp4 is null)
begin
	if(@mp4='dur')
	begin
		SET @strSQL2  = @strSQL2 + N'insert into tb_mp_temp_rpt(ParmValue,MeasureDT) values(''''+cast(datediff(mi,@lastMeasureDT,'''+@endtime+N''')/60 as varchar)+'':''+cast(datediff(mi,@lastMeasureDT,'''+@endtime+N''')%60 as varchar)+'''',''2009-06-15'')'+char(10)
	end
	else
	begin
		SET @strSQL2  = @strSQL2 + N'insert into tb_mp_temp_rpt(ParmValue,MeasureDT) values(''-'',''2009-06-15'')'+char(10)
	end
end
if(not @mp5 is null)
begin
	if(@mp5='dur')
	begin
		SET @strSQL2  = @strSQL2 + N'insert into tb_mp_temp_rpt(ParmValue,MeasureDT) values(''''+cast(datediff(mi,@lastMeasureDT,'''+@endtime+N''')/60 as varchar)+'':''+cast(datediff(mi,@lastMeasureDT,'''+@endtime+N''')%60 as varchar)+'''',''2009-06-15'')'+char(10)
	end
	else
	begin
		SET @strSQL2  = @strSQL2 + N'insert into tb_mp_temp_rpt(ParmValue,MeasureDT) values(''-'',''2009-06-15'')'+char(10)
	end
end
if(not @mp6 is null)
begin
	if(@mp6='dur')
	begin
		SET @strSQL2  = @strSQL2 + N'insert into tb_mp_temp_rpt(ParmValue,MeasureDT) values(''''+cast(datediff(mi,@lastMeasureDT,'''+@endtime+N''')/60 as varchar)+'':''+cast(datediff(mi,@lastMeasureDT,'''+@endtime+N''')%60 as varchar)+'''',''2009-06-15'')'+char(10)
	end
	else
	begin
		SET @strSQL2  = @strSQL2 + N'insert into tb_mp_temp_rpt(ParmValue,MeasureDT) values(''-'',''2009-06-15'')'+char(10)
	end
end
SET @strSQL2  = @strSQL2 + N'end'+char(10)


SET @strSQL3 = N''
SET @strSQL3  = @strSQL3 + N'set @count = @count+1'+char(10)
SET @strSQL3  = @strSQL3 + N'while(@count<='+cast(@rownum as varchar)+N')'+char(10)
SET @strSQL3  = @strSQL3 + N'begin'+char(10)	
SET @strSQL3  = @strSQL3 + N'SET @str  =N''insert into tb_mp_temp_rpt(ParmValue,MeasureDT) values(''''-'''',''''2009-06-15'''')'''+char(10)	
if(not @mp1 is null) SET @strSQL3  = @strSQL3 + N'SET @str  = @str + N''insert into tb_mp_temp_rpt(ParmValue,MeasureDT) values(''''-'''',''''2009-06-15'''')'''+char(10)	
if(not @mp2 is null) SET @strSQL3  = @strSQL3 + N'SET @str  = @str + N''insert into tb_mp_temp_rpt(ParmValue,MeasureDT) values(''''-'''',''''2009-06-15'''')'''+char(10)	
if(not @mp3 is null) SET @strSQL3  = @strSQL3 + N'SET @str  = @str + N''insert into tb_mp_temp_rpt(ParmValue,MeasureDT) values(''''-'''',''''2009-06-15'''')'''+char(10)	
if(not @mp4 is null) SET @strSQL3  = @strSQL3 + N'SET @str  = @str + N''insert into tb_mp_temp_rpt(ParmValue,MeasureDT) values(''''-'''',''''2009-06-15'''')'''+char(10)	
if(not @mp5 is null) SET @strSQL3  = @strSQL3 + N'SET @str  = @str + N''insert into tb_mp_temp_rpt(ParmValue,MeasureDT) values(''''-'''',''''2009-06-15'''')'''+char(10)	
if(not @mp6 is null) SET @strSQL3  = @strSQL3 + N'SET @str  = @str + N''insert into tb_mp_temp_rpt(ParmValue,MeasureDT) values(''''-'''',''''2009-06-15'''')'''+char(10)	
SET @strSQL3  = @strSQL3 + N'exec(@str)'+char(10)
SET @strSQL3  = @strSQL3 + N'set @count = @count+1'+char(10)
SET @strSQL3  = @strSQL3 + N'end'+char(10)


print @strSQL1
print @strSQL2
print @strSQL3
exec ( @strSQL1+@strSQL2+@strSQL3)

end
else
	begin
		while(@i<=@colcount*@rownum)
		begin
		print '-'
		insert into TB_MP_TEMP_RPT(ParmValue,MeasureDT) values('-',@starttime)
		set @i = @i+1
		end
	end


GO

-- ----------------------------
-- Procedure structure for sp_xlstoeip
-- ----------------------------
DROP PROCEDURE [dbo].[sp_xlstoeip]
GO



CREATE PROCEDURE [dbo].[sp_xlstoeip] @starttime varchar(20),@endtime varchar(20)
 as 
declare @strsql nvarchar(4000)
declare @mptable as varchar(500)
declare @i as int
declare mp cursor for select name from syscolumns where id =OBJECT_ID('TB_INMPVALUE') and name not in ('MeasureDT')  
open mp
fetch next from mp into @mptable
while @@fetch_status=0
begin
	begin
		set @strsql=N'delete from dbo.tb_mp_'+@mptable+N' where measuredt   >= ''' +cast(@starttime as varchar(20)) +N''' and measuredt   <= '''+cast(@endtime as varchar(20))+N''''
		print @strsql
		print 'go '

		set @strsql=N'insert dbo.tb_mp_'+@mptable+N'(measuredt,parmvalue) select MeasureDT,'+@mptable+N' from dbo.TB_INMPVALUE where measuredt   >= ''' +cast(@starttime as varchar(20)) +N''' and measuredt   <= '''+cast(@endtime as varchar(20))+N''' and measuredt is not null and '+@mptable+N' is not null '
		print @strsql
		print 'go '

	end
fetch from mp into @mptable	
end
close mp
deallocate mp


GO

-- ----------------------------
-- Procedure structure for txtbacktodb1
-- ----------------------------
DROP PROCEDURE [dbo].[txtbacktodb1]
GO



CREATE PROCEDURE [dbo].[txtbacktodb1]

as


Declare @msg varchar(255)
Declare @st varchar(50)
Declare @et varchar(50)
Declare @tablename1 varchar(50)
Declare @tablename2 varchar(50)
Declare @MPointCode varchar(50)
Declare @ParmValue varchar(50)
Declare @MeasureDT varchar(50)
Declare @sql varchar(8000) --sql


BEGIN TRANSACTION

set @st = '2012-12-25 06:28:21'
set @et = '2012-12-27 11:23:03'

DECLARE curRunStatus cursor for 
select Col001,Col002,Col003 from [2012-12-27] where Col003 > @st and Col003 < @et order by Col003
open curRunStatus

FETCH curRunStatus into @MPointCode,@ParmValue,@MeasureDT

while (@@fetch_status=0)
begin

select @tablename1='TB_MP_'+@MPointCode


SET @sql = ''
--SET @sql = @sql + 'use EIP_PRD_NBNQ20120824' + char(10)

SET @sql = @sql + 'if not exists (select * ' + char(10)
SET @sql = @sql + ' from  '+@tablename1 + char(10)
SET @sql = @sql + ' where  measuredt = '''+ @MeasureDT +''')'+ char(10)
SET @sql = @sql + 'begin'+char(10)

SET @sql = @sql + ' insert into '+@tablename1+' (parmvalue,measuredt) '+ char(10)
SET @sql = @sql + ' values ( '+ @ParmValue  +','''+ @MeasureDT  +''')'+ char(10)

SET @sql = @sql + 'end'+char(10)

print @sql

EXECUTE(@sql)

FETCH curRunStatus into @MPointCode,@ParmValue,@MeasureDT

end

close curRunStatus
DEALLOCATE curRunStatus



			
If @@ERROR <> 0
Begin
Set @Msg = ''
Goto ErrorHandle
End


ErrorHandle:

IF @msg IS null
    Commit transaction
ELSE
BEGIN
Rollback transaction
RAISERROR (@Msg, 16, 1)
END


GO

-- ----------------------------
-- Procedure structure for txtbacktodb2
-- ----------------------------
DROP PROCEDURE [dbo].[txtbacktodb2]
GO



CREATE PROCEDURE [dbo].[txtbacktodb2]

as


Declare @msg varchar(255)
Declare @st varchar(50)
Declare @et varchar(50)
Declare @tablename1 varchar(50)
Declare @tablename2 varchar(50)
Declare @MPointCode varchar(50)
Declare @ParmValue varchar(50)
Declare @MeasureDT varchar(50)
Declare @sql varchar(8000) --sql


BEGIN TRANSACTION

--set @st = '2011-10-09 11:10:00'
--set @et = '2011-10-10 08:30:00'

DECLARE curRunStatus cursor for 
select Col001,Col002,Col003 from [2012-08-14] order by Col003
open curRunStatus

FETCH curRunStatus into @MPointCode,@ParmValue,@MeasureDT

while (@@fetch_status=0)
begin

select @tablename1='TB_MP_'+@MPointCode


SET @sql = ''
--SET @sql = @sql + 'use EIP_PRD_NBNQ20120824' + char(10)

SET @sql = @sql + 'if not exists (select * ' + char(10)
SET @sql = @sql + ' from  '+@tablename1 + char(10)
SET @sql = @sql + ' where  measuredt = '''+ @MeasureDT +''')'+ char(10)
SET @sql = @sql + 'begin'+char(10)

SET @sql = @sql + ' insert into '+@tablename1+' (parmvalue,measuredt) '+ char(10)
SET @sql = @sql + ' values ( '+ @ParmValue  +','''+ @MeasureDT  +''')'+ char(10)

SET @sql = @sql + 'end'+char(10)

print @sql

EXECUTE(@sql)

FETCH curRunStatus into @MPointCode,@ParmValue,@MeasureDT

end

close curRunStatus
DEALLOCATE curRunStatus



			
If @@ERROR <> 0
Begin
Set @Msg = ''
Goto ErrorHandle
End


ErrorHandle:

IF @msg IS null
    Commit transaction
ELSE
BEGIN
Rollback transaction
RAISERROR (@Msg, 16, 1)
END


GO

-- ----------------------------
-- Procedure structure for Update_Unit
-- ----------------------------
DROP PROCEDURE [dbo].[Update_Unit]
GO



CREATE PROCEDURE [dbo].[Update_Unit]
AS
BEGIN

	select * from [dbo].[TB_MeasurePoint]
	UPDATE [dbo].[TB_MeasurePoint] 
SET Unit = ( CASE 

WHEN parmName LIKE '%瞬时流量%' THEN 'm3/s' 

WHEN parmName LIKE '%流量累计%' and parmName not like '%总%'  THEN 'm3' 

WHEN parmName LIKE '%总流量累计%' THEN '万m3' 

WHEN parmName like '%cod%'  and parmName not like '%状态%' THEN 'mg/l' 

WHEN parmName like '%氨氮%' and parmName not like '%状态%' then 'mg/l'

WHEN parmName LIKE '%TP%' THEN 'mg/l' 

WHEN parmName LIKE '%TN%' THEN 'mg/l' 

WHEN parmName LIKE '%SS%' THEN 'mg/l' 

WHEN parmName LIKE '%DO%' THEN 'mg/l' 

WHEN parmName LIKE '%mlss%' THEN 'mg/l' 

WHEN parmName LIKE '%orp%' THEN 'mg/l' 

WHEN parmName like '%液位%' and ParmName not like 	'%失败%'  and ParmName not like 	'%状态%'    THEN 'm(mm)' 

WHEN parmName LIKE '%h2s%'  and ParmName not like 	'%失败%'  and ParmName not like 	'%状态%'    THEN 'PPM' 

WHEN parmName LIKE '%ch4%'  and ParmName not like 	'%失败%'  and ParmName not like 	'%状态%'    THEN 'PPM' 

WHEN parmName like '%电压%' and ParmName not like 	'%故障%'  THEN 'V' 

WHEN parmName like '%电流%' THEN 'A' 

WHEN parmName like '%功率%' THEN 'KW' 

WHEN parmName like '%用电量%' THEN 'KW.H' 

end
)
END


GO

-- ----------------------------
-- Procedure structure for UPLOAD_JS1C
-- ----------------------------
DROP PROCEDURE [dbo].[UPLOAD_JS1C]
GO



CREATE PROCEDURE [dbo].[UPLOAD_JS1C] 
 as 
declare @strsql varchar(4000)
declare @mpointcode as varchar(500)
declare @parmvalue as varchar(500)
declare @measuredt as datetime
declare @measuredt1 as VARCHAR(500)

declare @i as int
declare mp cursor for select mpointcode,convert(varchar(500),parmvalue),measuredt from dbo.TB_JS1C
open mp
fetch next from mp into @mpointcode,@parmvalue,@measuredt
while @@fetch_status=0
begin
	begin
		--set @strsql=N'CREATE TABLE dbo.TB_MP_'+@mptable+N'_day([ItemID] [bigint] IDENTITY(1,1) NOT NULL,[ParmValue] [money] NOT NULL,[MeasureDT] [datetime] NOT NULL,[memotype] [varchar](100) NULL,[memo] [varchar](500) NULL,[userid] [varchar](50) NULL,[insdt] [varchar](50) NULL)'

    update tb_measurepoint set ParmValue  = @parmvalue, MeasureDT = @measuredt where mpointcode = @mpointcode
    --PRINT @mpointcode
    --PRINT @parmvalue
    --PRINT @measuredt
    
    SET @measuredt1 = CONVERT (VARCHAR(500),@measuredt,120)
    --PRINT @MEASUREDT1 
	SET @strsql=N'INSERT INTO TB_MP_'+@mpointcode +N'(PARMVALUE,MEASUREDT) VALUES(' + @parmvalue +N','''+@measuredt1 +N''')'	
	--PRINT @strsql
	exec (@strsql)

 

		
	end
fetch from mp into 	@mpointcode,@parmvalue,@measuredt
end
close mp
deallocate mp


GO

-- ----------------------------
-- Procedure structure for UPLOAD_JS2C
-- ----------------------------
DROP PROCEDURE [dbo].[UPLOAD_JS2C]
GO



CREATE PROCEDURE [dbo].[UPLOAD_JS2C] 
 as 
declare @strsql varchar(4000)
declare @mpointcode as varchar(500)
declare @parmvalue as varchar(500)
declare @measuredt as datetime
declare @measuredt1 as VARCHAR(500)

declare @i as int
declare mp cursor for select mpointcode,convert(varchar(500),parmvalue),measuredt from dbo.TB_JS2C
open mp
fetch next from mp into @mpointcode,@parmvalue,@measuredt
while @@fetch_status=0
begin
	begin
		--set @strsql=N'CREATE TABLE dbo.TB_MP_'+@mptable+N'_day([ItemID] [bigint] IDENTITY(1,1) NOT NULL,[ParmValue] [money] NOT NULL,[MeasureDT] [datetime] NOT NULL,[memotype] [varchar](100) NULL,[memo] [varchar](500) NULL,[userid] [varchar](50) NULL,[insdt] [varchar](50) NULL)'

    update tb_measurepoint set ParmValue  = @parmvalue, MeasureDT = @measuredt where mpointcode = @mpointcode
    --PRINT @mpointcode
    --PRINT @parmvalue
    --PRINT @measuredt
    
    SET @measuredt1 = CONVERT (VARCHAR(500),@measuredt,120)
    --PRINT @MEASUREDT1 
	SET @strsql=N'INSERT INTO TB_MP_'+@mpointcode +N'(PARMVALUE,MEASUREDT) VALUES(' + @parmvalue +N','''+@measuredt1 +N''')'	
	--PRINT @strsql
	exec (@strsql)

 

		
	end
fetch from mp into 	@mpointcode,@parmvalue,@measuredt
end
close mp
deallocate mp


GO

-- ----------------------------
-- Procedure structure for UPLOAD_JSBZ
-- ----------------------------
DROP PROCEDURE [dbo].[UPLOAD_JSBZ]
GO



CREATE PROCEDURE [dbo].[UPLOAD_JSBZ] 
 as 
declare @strsql varchar(4000)
declare @mpointcode as varchar(500)
declare @parmvalue as varchar(500)
declare @measuredt as datetime
declare @measuredt1 as VARCHAR(500)

declare @i as int
declare mp cursor for select mpointcode,convert(varchar(500),parmvalue),measuredt from dbo.TB_JSBZ
open mp
fetch next from mp into @mpointcode,@parmvalue,@measuredt
while @@fetch_status=0
begin
	begin
		--set @strsql=N'CREATE TABLE dbo.TB_MP_'+@mptable+N'_day([ItemID] [bigint] IDENTITY(1,1) NOT NULL,[ParmValue] [money] NOT NULL,[MeasureDT] [datetime] NOT NULL,[memotype] [varchar](100) NULL,[memo] [varchar](500) NULL,[userid] [varchar](50) NULL,[insdt] [varchar](50) NULL)'

    update tb_measurepoint set ParmValue  = @parmvalue, MeasureDT = @measuredt where mpointcode = @mpointcode
    --PRINT @mpointcode
    --PRINT @parmvalue
    --PRINT @measuredt
    
    SET @measuredt1 = CONVERT (VARCHAR(500),@measuredt,120)
    --PRINT @MEASUREDT1 
	SET @strsql=N'INSERT INTO TB_MP_'+@mpointcode +N'(PARMVALUE,MEASUREDT) VALUES(' + @parmvalue +N','''+@measuredt1 +N''')'	
	--PRINT @strsql
	exec (@strsql)

 

		
	end
fetch from mp into 	@mpointcode,@parmvalue,@measuredt
end
close mp
deallocate mp


GO

-- ----------------------------
-- Procedure structure for USP_SendSysSJ_Hour
-- ----------------------------
DROP PROCEDURE [dbo].[USP_SendSysSJ_Hour]
GO



CREATE PROCEDURE [dbo].[USP_SendSysSJ_Hour]
AS
declare @ServiceUrl as varchar(8000) 
declare @UrlAddress varchar(500)

declare @disname varchar(500)
declare @Value varchar(500)
declare @mpCODE varchar(5000)
declare @mp varchar(500)
declare @sendtm varchar(500)
Declare @starttime datetime
Declare @endtime datetime
Declare @strSQL nvarchar(4000)
declare @v money
declare @s money
declare @v1 money
declare @v2 money


declare @bizname varchar(500)
declare @todaydt varchar(10)


declare @FunName varchar(50)
--WebService中调用的方法名：例如'getMobileCodeInfo'
set @FunName = 'sendData'     

--以下参数对应WebService中的[参数名]
declare @P1 varchar(800),@P2 varchar(800)

set @P1 = 'text'
set @p2 = 'factoryname'

declare @P1_Value varchar(8000),@P2_Value varchar(800)
                  
Declare @Object as Int
Declare @ResponseText as Varchar(8000)
set @P1_Value = ''
set @P2_Value = 'HQWS'
   
--以参数对应WebService中的[参数名]

set @endtime = getdate();

--set @endtime = dateadd(ss,-datepart(ss,@endtime),@endtime)
--set @endtime = dateadd(mi,-datepart(mi,@endtime),@endtime)
--set @endtime = dateadd(ms,-datepart(ms,@endtime),@endtime)


set @starttime = dateadd(day,-1,@endtime)
set @UrlAddress = 'http://116.236.176.247:10100/SIPAI_HIS/services/SendDataService_Hour/'

DECLARE CRStatus cursor for 
	--select * from dbo.FUN_split(@mpstr,'|')
	SELECT MPointCode,parmvalue FROM TB_upload   order by Mpointid
	open CRStatus

	FETCH  CRStatus into @mpCODE,@Value

	while (@@fetch_status=0)
	begin
			
	set @P1_Value=@P1_Value+@mpCODE+'|'+@Value+ ','

	if(len(@P1_Value)>3500)
		
	begin			
	    set @sendtm=CONVERT(varchar(50),@endtime, 120)
	    set @P1_Value=@sendtm+','+@P1_Value
	    set @ServiceUrl = @UrlAddress + @FunName + '?' + @P1 + '=' + @P1_Value +'&' + @P2 + '=' + @P2_Value                  
        Exec sp_OACreate 'Msxml2.ServerXMLHTTP.3.0', @Object OUT;
	    Exec sp_OAMethod @Object, 'open', NULL, 'get',@ServiceUrl,'false'
	    Exec sp_OAMethod @Object, 'send'
	    Exec sp_OAMethod @Object, 'responseText', @ResponseText OUTPUT
     
	    Select @ResponseText   
	    Exec sp_OADestroy @Object
			print   @ServiceUrl
			set @P1_Value= ''
			set @ServiceUrl= ''
	end

	
	FETCH  CRStatus into @mpCODE,@Value
	end
	close CRStatus
	DEALLOCATE CRStatus

--WebService地址：以http开头，结尾带斜杠，例如'http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx/' 



set @sendtm=CONVERT(varchar(50),@endtime, 120)
--将时间格式转换标准

set @P1_Value=@sendtm+','+@P1_Value


set @ServiceUrl = @UrlAddress + @FunName + '?' + @P1 + '=' + @P1_Value +'&' + @P2 + '=' + @P2_Value                  

                  
Exec sp_OACreate 'Msxml2.ServerXMLHTTP.3.0', @Object OUT;
Exec sp_OAMethod @Object, 'open', NULL, 'get',@ServiceUrl,'false'
Exec sp_OAMethod @Object, 'send'
Exec sp_OAMethod @Object, 'responseText', @ResponseText OUTPUT
     
Select @ResponseText     
Exec sp_OADestroy @Object

print @ServiceUrl


GO

-- ----------------------------
-- Procedure structure for xlstoeip
-- ----------------------------
DROP PROCEDURE [dbo].[xlstoeip]
GO



CREATE PROCEDURE [dbo].[xlstoeip] @starttime varchar(20),@endtime varchar(20)
 as 
declare @strsql nvarchar(4000)
declare @mptable as varchar(500)
declare @i as int
declare mp cursor for select name from syscolumns where id =OBJECT_ID('Sheet1') and name not in ('datetime')  
open mp
fetch next from mp into @mptable
while @@fetch_status=0
begin
	begin
		set @strsql=N'delete from dbo.tb_mp_'+@mptable+N' where measuredt   between ''' +cast(@starttime as varchar(20)) +N''' and '''+cast(@endtime as varchar(20))+N''''
		print @strsql
		print 'go '

		set @strsql=N'insert dbo.tb_mp_'+@mptable+N'(measuredt,parmvalue) select datetime,'+@mptable+N' from dbo.Sheet1 where datetime   between ''' +cast(@starttime as varchar(20)) +N''' and '''+cast(@endtime as varchar(20))+N''' and datetime is not null and '+@mptable+N' is not null '
		print @strsql
		print 'go '

	end
fetch from mp into @mptable	
end
close mp
deallocate mp


GO

-- ----------------------------
-- Function structure for FUN_mtohTime
-- ----------------------------
DROP FUNCTION [dbo].[FUN_mtohTime]
GO



CREATE FUNCTION [dbo].[FUN_mtohTime] (@time_in int)  
RETURNS money AS  
BEGIN 
	--DECLARE @time_in 	money				--input time typed money
	--DECLARE @time_in_str 	varchar(50)			--input time typed varchar
	--DECLARE @part_int		varchar(50)			--int part of time
	--DECLARE @part_float	varchar(50)			--float part of time
	--DECLARE @iStart		int
	--DECLARE @iLen		int
	--DECLARE @a money

	DECLARE @time_in_mm 	int
	DECLARE @time_in_h	int
	DECLARE @time_in_m 	int
	DECLARE @time_in_hstr 	varchar(50)
	DECLARE @time_in_mstr 	varchar(50)
	set @time_in_mm = @time_in
	set @time_in_h = @time_in_mm/60
	set @time_in_m = @time_in_mm%60

	set @time_in_hstr 	= CONVERT(varchar(50), @time_in_h)--convert input time to varchar
	set @time_in_mstr 	= CONVERT(varchar(50), @time_in_m)--convert input time to varchar
	set @time_in_hstr 	= RTRIM(LTRIM(@time_in_hstr))
	set @time_in_mstr 	= RTRIM(LTRIM(@time_in_mstr))
	IF (len(@time_in_mstr) = 1)
	set @time_in_mstr = '0' + @time_in_mstr
	--set @iStart	= CHARINDEX( '.' ,@time_in_hstr )
	--set @iLen		= LEN(@time_in_str)
	--set @part_int	= SUBSTRING(@time_in_str,0,@iStart)
	--set @part_float	= SUBSTRING(@time_in_str,@iStart + 1,@iLEN - @iStart)
	--set @part_float 	= CONVERT(varchar(50),CONVERT(money, @part_float) * 0.6)
	--set @part_float	= RTRIM(LTRIM(@part_float))
	--set @iStart	= CHARINDEX( '.' ,@part_float )
	--set @iLen		= LEN(@part_float)
	--set @part_float	= SUBSTRING(@part_float,0,@iStart)
	--IF (len(@part_float) = 1)
		--set @part_float = '0' + @part_float
	RETURN CONVERT(money,@time_in_hstr + '.' + @time_in_mstr)
END


GO

-- ----------------------------
-- Indexes structure for table Alarm
-- ----------------------------

-- ----------------------------
-- Primary Key structure for table Alarm
-- ----------------------------
ALTER TABLE [dbo].[Alarm] ADD PRIMARY KEY ([IOServerName], [TagName], [EventTime], [EventType])
GO

-- ----------------------------
-- Indexes structure for table dtproperties
-- ----------------------------

-- ----------------------------
-- Primary Key structure for table dtproperties
-- ----------------------------
ALTER TABLE [dbo].[dtproperties] ADD PRIMARY KEY ([id], [property])
GO

-- ----------------------------
-- Indexes structure for table MZ_DF11_C_01_1006_02_COD02
-- ----------------------------

-- ----------------------------
-- Primary Key structure for table MZ_DF11_C_01_1006_02_COD02
-- ----------------------------
ALTER TABLE [dbo].[MZ_DF11_C_01_1006_02_COD02] ADD PRIMARY KEY ([ItemID])
GO

-- ----------------------------
-- Indexes structure for table TB_Alarm_col
-- ----------------------------

-- ----------------------------
-- Primary Key structure for table TB_Alarm_col
-- ----------------------------
ALTER TABLE [dbo].[TB_Alarm_col] ADD PRIMARY KEY ([id])
GO

-- ----------------------------
-- Indexes structure for table TB_DATATRANS_BADMP
-- ----------------------------

-- ----------------------------
-- Primary Key structure for table TB_DATATRANS_BADMP
-- ----------------------------
ALTER TABLE [dbo].[TB_DATATRANS_BADMP] ADD PRIMARY KEY ([ID])
GO

-- ----------------------------
-- Indexes structure for table TB_DATATRANS_Record
-- ----------------------------

-- ----------------------------
-- Primary Key structure for table TB_DATATRANS_Record
-- ----------------------------
ALTER TABLE [dbo].[TB_DATATRANS_Record] ADD PRIMARY KEY ([ID])
GO

-- ----------------------------
-- Indexes structure for table TB_MeasurePoint_copy1
-- ----------------------------

-- ----------------------------
-- Primary Key structure for table TB_MeasurePoint_copy1
-- ----------------------------
ALTER TABLE [dbo].[TB_MeasurePoint_copy1] ADD PRIMARY KEY ([MPointID])
GO

-- ----------------------------
-- Triggers structure for table TB_MeasurePoint_copy1
-- ----------------------------
DROP TRIGGER [dbo].[UTR_MPInsert_copy1]
GO
CREATE TRIGGER [dbo].[UTR_MPInsert_copy1]
ON [dbo].[TB_MeasurePoint_copy1]
AFTER INSERT
AS
DECLARE @msg  varchar(255),
		@strSQL varchar(8000),
		@strMPointCode varchar(50)

SET NOCOUNT ON

SELECT @strMPointCode = ISNULL(MPointCode,'') FROM inserted;
IF (@strMPointCode <> '')
	BEGIN
		SET @strSQL = ''
		SET @strSQL = @strSQL + 'if exists (select 1 ' + char(10)
		SET @strSQL = @strSQL + '            from  sysobjects ' + char(10)
		SET @strSQL = @strSQL + '           where  id = object_id(''dbo.TB_MP_' + @strMPointCode + ''') ' + char(10)
		SET @strSQL = @strSQL + '            and   type = ''U'') ' + char(10)
		SET @strSQL = @strSQL + '   drop table dbo.[TB_MP_' + @strMPointCode + ']; ' + char(10) + char(10)
		
		SET @strSQL = @strSQL + ' create table dbo.[TB_MP_' + @strMPointCode + '] ( ' + char(10)
		SET @strSQL = @strSQL + '   ItemID               bigint               identity, ' + char(10)
		SET @strSQL = @strSQL + '   ParmValue            money                not null default 0, ' + char(10)
		SET @strSQL = @strSQL + '   MeasureDT            datetime             not null default getdate(), ' + char(10)
		SET @strSQL = @strSQL + '   userid            varchar(50)            , ' + char(10)
		SET @strSQL = @strSQL + '   insdt            varchar(50) 	default getdate() ,          ' + char(10)
		SET @strSQL = @strSQL + '   constraint [PK_TB_MP_' + @strMPointCode + '] primary key  (ItemID) ' + char(10)
		SET @strSQL = @strSQL + ') ' + char(10) + char(10)

		EXECUTE(@strSQL)
		--Print @strSQL
		IF @@ERROR <> 0
		BEGIN
			SET @msg =  '创建TB_MP_' + @strMPointCode + '表时出现错误'
			GOTO ExitHandle
		END

	END

ExitHandle:
    IF @msg IS NULL
		COMMIT TRANSACTION
    ELSE
    BEGIN
        ROLLBACK TRANSACTION
        RAISERROR (@Msg, 16, 1)
    END

	SET NOCOUNT OFF

GO
ALTER TABLE [dbo].[TB_MeasurePoint_copy1] DISABLE TRIGGER [UTR_MPInsert_copy1]
GO

-- ----------------------------
-- Indexes structure for table TB_MeasurePoint_PropSource
-- ----------------------------

-- ----------------------------
-- Primary Key structure for table TB_MeasurePoint_PropSource
-- ----------------------------
ALTER TABLE [dbo].[TB_MeasurePoint_PropSource] ADD PRIMARY KEY ([id])
GO

-- ----------------------------
-- Indexes structure for table tb_model_cal_info
-- ----------------------------

-- ----------------------------
-- Primary Key structure for table tb_model_cal_info
-- ----------------------------
ALTER TABLE [dbo].[tb_model_cal_info] ADD PRIMARY KEY ([id])
GO

-- ----------------------------
-- Indexes structure for table tb_PersonalMpCollection
-- ----------------------------

-- ----------------------------
-- Primary Key structure for table tb_PersonalMpCollection
-- ----------------------------
ALTER TABLE [dbo].[tb_PersonalMpCollection] ADD PRIMARY KEY ([id])
GO

-- ----------------------------
-- Indexes structure for table TB_PRI
-- ----------------------------

-- ----------------------------
-- Primary Key structure for table TB_PRI
-- ----------------------------
ALTER TABLE [dbo].[TB_PRI] ADD PRIMARY KEY ([id])
GO

-- ----------------------------
-- Indexes structure for table TB_RTConsole
-- ----------------------------

-- ----------------------------
-- Primary Key structure for table TB_RTConsole
-- ----------------------------
ALTER TABLE [dbo].[TB_RTConsole] ADD PRIMARY KEY ([ConsoleID])
GO

-- ----------------------------
-- Indexes structure for table tb_scada_alarm
-- ----------------------------

-- ----------------------------
-- Primary Key structure for table tb_scada_alarm
-- ----------------------------
ALTER TABLE [dbo].[tb_scada_alarm] ADD PRIMARY KEY ([id])
GO
