轉置SQL及stored procedure foreach

ALTER PROCEDURE [dbo].[zToNewSalesFeat]
AS
BEGIN
 
TRUNCATE TABLE newsalesfeat   --先把要INSERT的TABLE清空

DECLARE @oneid INT
DECLARE the_cursor CURSOR FAST_FORWARD

FOR select code from MAN_Data where [Date] between '2017/1/1' and '2017/12/31' order by BGCode,[Date]

OPEN the_cursor
FETCH NEXT FROM the_cursor INTO @oneid


WHILE @@FETCH_STATUS = 0
BEGIN
INSERT  newSalesFeat
select man_code,[201701],[201702],[201703],[201704],[201705],[201706],[201707],[201708],[201709],[201710],[201711],[201712]
from (
select man_code,Period,FYA from SS_Feat where Man_Code=@oneid and ct in (1,100) and Period between 201701 and 201712
)
as s pivot
( sum(fya) for period in ([201701],[201702],[201703],[201704],[201705],[201706],[201707],[201708],[201709],[201710],[201711],[201712])
) as p

FETCH NEXT FROM the_cursor INTO @oneid
END

CLOSE the_cursor
DEALLOCATE the_cursor

END

留言

這個網誌中的熱門文章

java BigDecimal 加減乘除,四捨五入,四則運算及比較

MS sqlServer資料庫移轉至MySQL-->利用MySQL WorkBench

java 數字轉字串 字串轉數字