请高手帮忙分析下 行转列的存储过程(附源码)
CREATE PROCEDURE DUMP_GDDATA
(
@Daily smalldatetime
)
AS
BEGIN
CREATE TABLE #TEMP(
MeasureSetID nvarchar (4) NOT NULL ,
CollectDateTime smalldatetime NOT NULL ,
InceptDateTime smalldatetime NOT NULL ,
RawData9010 numeric(18, 4) NULL ,
RawData9020 numeric(18, 4) NULL ,
RawData9110 numeric(18, 4) NULL ,
RawData9120 numeric(18, 4) NULL ,
)
DECLARE @MEASURESETID NVARCHAR(4)
DECLARE @COLLECTDATETIME DATETIME
DECLARE @INCEPTDATETIME DATETIME
DECLARE @DATADICTIONARYSYMBOL NVARCHAR(4)
DECLARE @RAWDATA NUMERIC(18,4)
DECLARE authors_cursor CURSOR FOR
SELECT CAST(MEASURESETID AS nvarchar) AS MEASURESETID,COLLECTDATETIME,INCEPTDATETIME,DATADICTIONARYSYMBOL,RAWDATA
FROM DBO.MEASURESETRAWDATA
WHERE DATEDIFF(DAY,COLLECTDATETIME,@Daily)=0
--WHERE DATEDIFF(DAY,COLLECTDATETIME,@DATETIME)=0
ORDER BY MEASURESETID,COLLECTDATETIME
OPEN authors_cursor
FETCH NEXT FROM authors_cursor
INTO @MEASURESETID, @COLLECTDATETIME,@INCEPTDATETIME,@DATADICTIONARYSYMBOL,@RAWDATA
WHILE @@FETCH_STATUS = 0
BEGIN
IF @DATADICTIONARYSYMBOL= '9010 '
INSERT INTO #TEMP(MeasureSetID,CollectDateTime,INCEPTDATETIME,RawData9010)
VALUES(@MEASURESETID,@COLLECTDATETIME,@INCEPTDATETIME,@RAWDATA)
IF @DATADICTIONARYSYMBOL= '9020 '
INSERT INTO #TEMP(MeasureSetID,CollectDateTime,INCEPTDATETIME,RawData9020)
VALUES(@MEASURESETID,@COLLECTDATETIME,@INCEPTDATETIME,@RAWDATA)
IF @DATADICTIONARYSYMBOL= '9110 '
INSERT INTO #TEMP(MeasureSetID,CollectDateTime,INCEPTDATETIME,RawData9110)
VALUES(@MEASURESETID,@COLLECTDATETIME,@INCEPTDATETIME,@RAWDATA)
IF @DATADICTIONARYSYMBOL= '9120 '
INSERT INTO #TEMP(MeasureSetID,CollectDateTime,INCEPTDATETIME,RawData9120)
VALUES(@MEASURESETID,@COLLECTDATETIME,@INCEPTDATETIME,@RAWDATA)
FETCH NEXT FROM authors_cursor
INTO @MEASURESETID, @COLLECTDATETIME,@INCEPTDATETIME, @DATADICTIONARYSYMBOL,@RAWDATA
END
CLOSE authors_cursor
INSERT INTO dbo.measureSetRaw
SELECT * FROM #TEMP