34,590
社区成员
发帖
与我相关
我的任务
分享
ALTER PROCEDURE [test].[SP_t001]
--以下为输入参数
@IndicatorParameterID INT, --输入指标参数ID
@Period NVARCHAR(10), --输入周期
@Condition NVARCHAR(MAX) --输入条件(需要传入动态条件)
AS
DECLARE @TimeGranularity NVARCHAR(5),
@Year NVARCHAR(4),
@TimeSliceStart DATETIME,
@TimeSliceEnd DATETIME,
@Performance DECIMAL(10,2),
@SqlSelect NVARCHAR(4000),
@SqlWhere NVARCHAR(4000)
BEGIN
--获取时间粒度中文值
SELECT @TimeGranularity = KPI.TimeGranularity.TimeGranularity, @Year = KPI.IndicatorParameter.Year
FROM HyperCubeODS.KPI.TimeGranularity
INNER JOIN KPI.IndicatorParameter ON TimeGranularity.TimeGranularityID = IndicatorParameter.TimeGranularityID
WHERE IndicatorParameter.TimeGranularityID = @IndicatorParameterID;
--获取时间范围开始值和结束值
EXEC KPI.SP_GenerateTimeSlice @Year, @TimeGranularity, @Period, @TimeSliceStart OUT, @TimeSliceEnd OUT
--运行查询语句查询绩效结果
SET @SqlSelect = N'SELECT @Performance=SUM(CASE [SizeCode] WHEN ''20'' THEN 1 WHEN ''40'' THEN 2 ELSE 2.25 END) AS number FROM tablename'
SET @SqlWhere = 'WHERE OperationTime >= @TimeSliceStart
AND OperationTime <= @TimeSliceEnd AND '
SET @SqlSelect = @SqlSelect + @SqlWhere + '@Condition'
EXECUTE sp_executesql @SqlSelect, N'@TimeSliceStart datetime,@TimeSliceEnd datetime,@Performance DECIMAL(10,2) OUTPUT',@TimeSliceStart,@TimeSliceEnd,@Performance OUTPUT;
--判断Performance表是否存在记录
MERGE INTO Performance T1
USING (SELECT @IndicatorParameterID, @Period, @Performance) AS T2(IndicatorParameterID, Period, Performance) ON T1.IndicatorParameterID = T2.IndicatorParameterID AND T1.Period = T2.Period
WHEN MATCHED THEN
--存在则更新字段
UPDATE SET T1.Performance = T2.Performance
WHEN NOT MATCHED THEN
--不存在则插入数据
INSERT(IndicatorParameterID, Period, Performance, Creater,CreateTime)VALUES(@IndicatorParameterID, @Period, @Performance, 'admin', GETDATE());
END
GO
ALTER PROCEDURE [test].[SP_t001]
--以下为输入参数
@IndicatorParameterID INT, --输入指标参数ID
@Period NVARCHAR(10), --输入周期
@Condition NVARCHAR(MAX) --输入条件(需要传入动态条件)
AS
DECLARE @TimeGranularity NVARCHAR(5),
@Year NVARCHAR(4),
@TimeSliceStart DATETIME,
@TimeSliceEnd DATETIME,
@Performance DECIMAL(10,2),
@SqlSelect NVARCHAR(500),
@SqlWhere NVARCHAR(200)
BEGIN
--获取时间粒度中文值
SELECT @TimeGranularity = KPI.TimeGranularity.TimeGranularity, @Year = KPI.IndicatorParameter.Year
FROM HyperCubeODS.KPI.TimeGranularity
INNER JOIN KPI.IndicatorParameter ON TimeGranularity.TimeGranularityID = IndicatorParameter.TimeGranularityID
WHERE IndicatorParameter.TimeGranularityID = @IndicatorParameterID;
--获取时间范围开始值和结束值
EXEC KPI.SP_GenerateTimeSlice @Year, @TimeGranularity, @Period, @TimeSliceStart OUT, @TimeSliceEnd OUT
--运行查询语句查询绩效结果
SET @SqlSelect = N'SELECT @Performance=SUM(CASE [SizeCode] WHEN ''20'' THEN 1 WHEN ''40'' THEN 2 ELSE 2.25 END) AS number FROM tablename'
SET @SqlWhere = 'WHERE OperationTime >= @TimeSliceStart
AND OperationTime <= @TimeSliceEnd AND '
SET @Performance = @SqlSelect + @SqlWhere + '@Condition'
EXECUTE sp_executesql @Performance, N'@TimeSliceStart datetime,@TimeSliceEnd datetime,@Performance DECIMAL(10,2) OUTPUT',@TimeSliceStart,@TimeSliceEnd,@Performance OUTPUT;
--判断Performance表是否存在记录
MERGE INTO Performance T1
USING (SELECT @IndicatorParameterID, @Period, @Performance) AS T2(IndicatorParameterID, Period, Performance) ON T1.IndicatorParameterID = T2.IndicatorParameterID AND T1.Period = T2.Period
WHEN MATCHED THEN
--存在则更新字段
UPDATE SET T1.Performance = T2.Performance
WHEN NOT MATCHED THEN
--不存在则插入数据
INSERT(IndicatorParameterID, Period, Performance, Creater,CreateTime)VALUES(@IndicatorParameterID, @Period, @Performance, 'admin', GETDATE());
END
GO
SET @SqlSelect = N'SELECT * FROM tablename '
SET @SqlWhere = 'WHERE OperationTime >= '+@TimeSliceStart +' AND OperationTime <= '+@TimeSliceEnd
SET @Performance = @SqlSelect + @SqlWhere + 'AND '+@Condition
EXECUTE sp_executesql @SqlSelect,@SqlWhere,@Performance;
DECLARE @Code VARCHAR(100),@Type VARCHAR(100)
SELECT * FROM t1 WHERE t1.Code=ISNULL(@Code,t1.Code) AND t1.Type=ISNULL(@Type,t1.Type)
-- 大概这个样子
--
EXECUTE sp_executesql @Performance , N'@starttime varchar(10),@endtime varchar(20)', @starttime=N'1111' ,@endtime = N'ccc'
ALTER PROCEDURE [test].[SP_t001]
--以下为输入参数
@IndicatorParameterID INT, --输入指标参数ID
@Period NVARCHAR(10), --输入周期
@Condition NVARCHAR(MAX) --输入条件(需要传入动态条件)
AS
DECLARE @TimeGranularity NVARCHAR(5),
@Year NVARCHAR(4),
@TimeSliceStart DATETIME,
@TimeSliceEnd DATETIME,
@Performance DECIMAL(30,2),
@SqlSelect NVARCHAR(4000),
@SqlWhere NVARCHAR(4000)
BEGIN
--获取时间粒度中文值
SELECT @TimeGranularity = KPI.TimeGranularity.TimeGranularity, @Year = KPI.IndicatorParameter.Year
FROM HyperCubeODS.KPI.TimeGranularity
INNER JOIN KPI.IndicatorParameter ON TimeGranularity.TimeGranularityID = IndicatorParameter.TimeGranularityID
WHERE IndicatorParameter.TimeGranularityID = @IndicatorParameterID;
--获取时间范围开始值和结束值
EXEC KPI.SP_GenerateTimeSlice @Year, @TimeGranularity, @Period, @TimeSliceStart OUT, @TimeSliceEnd OUT
--运行查询语句查询绩效结果
SET @SqlSelect = N'SELECT @Performance=SUM(CASE [SizeCode] WHEN ''20'' THEN 1 WHEN ''40'' THEN 2 ELSE 2.25 END) FROM tablename'
SET @SqlWhere = 'WHERE OperationTime >= @TimeSliceStart
AND OperationTime <= @TimeSliceEnd AND '
SET @SqlSelect = @SqlSelect + @SqlWhere + '@Condition'
EXECUTE sp_executesql @SqlSelect, N'@TimeSliceStart datetime,@TimeSliceEnd datetime,@Performance DECIMAL(10,2) OUTPUT',@TimeSliceStart,@TimeSliceEnd,@Performance OUTPUT;
--判断Performance表是否存在记录
MERGE INTO Performance T1
USING (SELECT @IndicatorParameterID, @Period, @Performance) AS T2(IndicatorParameterID, Period, Performance) ON T1.IndicatorParameterID = T2.IndicatorParameterID AND T1.Period = T2.Period
WHEN MATCHED THEN
--存在则更新字段
UPDATE SET T1.Performance = T2.Performance
WHEN NOT MATCHED THEN
--不存在则插入数据
INSERT(IndicatorParameterID, Period, Performance, Creater,CreateTime)VALUES(@IndicatorParameterID, @Period, @Performance, 'admin', GETDATE());
END
GO