SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[spDlyHIHMasterMCRYTDRRMCDC]
AS
BEGIN
--IF EXISTS DROP TABLE TEST1
--IF EXISTS (SELECT * FROM sysobjects WHERE id = object_id(N'[dbo].[tablename]')
--AND OBJECTPROPERTY(id, N'IsUserTable') = 1)
--DROP TABLE [dbo].[TEST1];
--CREATE TABLE [dbo].[tablename] ( columns specification );
DROP TABLE TEST1
--BEGIN
CREATE TABLE TEST1 ([TDATE] datetime, MCRRRMCDC smallint,MCRYTDRRMCDC smallint)
CREATE CLUSTERED INDEX ndxTDate on TEST1([TDATE])
;WITH data AS ( SELECT CONVERT(VARCHAR(4), dbo.fiscalyear([TDATE]), 120) ym, [TDATE], MCRRRMCDC, ROW_NUMBER() OVER ( PARTITION BY dbo.fiscalyear([TDATE]) ORDER BY [TDATE]) r
FROM dbo.tblHIHMaster)
insert into TEST1
SELECT data.[TDATE], data.MCRRRMCDC
, (SELECT SUM(x.MCRRRMCDC) FROM data x WHERE x.ym = data.ym AND x.r <= data.r ) MCRYTDRRMCDC
FROM data
UPDATE tblHIHMaster SET MCRYTDRRMCDC = ISNULL(TEST1.MCRYTDRRMCDC,0) FROM TEST1 WHERE tblHIHMaster.TDATE = TEST1.TDATE
END
GO
|