2013-04-29 68 views
-2

一位同事給了我這個存儲過程,並要求幫助根據其結果創建一個sql INSERT語句,但對某些方法有些困惑。例如,我從來沒有在sql中看過CASE語句。我查了一下,但用法與我所得到的不同。如何使用此SQL存儲過程來創建INSERT語句?

這裏是存儲過程

if @ScheduleType Is Null 
SELECT Lu_Schedule_Types.ScheduleType, 
SUM(CASE WHEN InductionProduction = 1 THEN CASE WHEN (LEFT(DATENAME(Month, 
        3)) = 'JAN' THEN ItemQty END END) AS I01, 
SUM(CASE WHEN InductionProduction = 1 THEN CASE WHEN (LEFT(DATENAME(Month,     
        Item_Schedule.ItemScheduleDate), 3)) 
        = 'FEB' THEN ItemQty END END) AS I02, 
SUM(CASE WHEN InductionProduction = 2 THEN ItemQty ELSE 0 END) AS PRD, 
LmpProjectInfo.PlannedQty, 
LmpProjectInfo.AuthorizedQty, 
LmpProjectInfo.WbsElementID 

FROM Item_Schedule 
INNER JOIN Lu_Schedule_Types 
      ON Item_Schedule.ItemScheduleType = Lu_Schedule_Types.ScheduleTypeId 
RIGHT OUTER JOIN LmpProjectInfo 
      ON Item_Schedule.ItemWbsElement = LmpProjectInfo.WbsElementID 
WHERE 
    (Item_Schedule.IsActive = 1) 
    AND (Item_Schedule.ItemWbsElement = @WbsElement) 
    AND (Item_Schedule.ItemScheduleDate < DATEADD(d, 1, @EndDate)) AND 
    (Item_Schedule.ItemScheduleDate >= @StartDate) 
GROUP BY Lu_Schedule_Types.ScheduleType 
    , Lu_Schedule_Types.ScheduleTypeId 
    , LmpProjectInfo.PlannedQty 
    , LmpProjectInfo.AuthorizedQty 
    , LmpProjectInfo.WbsElementID 
ORDER BY Lu_Schedule_Types.ScheduleTypeId 

我應該可以幫助他了,但我目前數據庫嚮導是,和我一點點在這裏我的深度。我真的很感激幫助/建議/方向。

非常感謝!

+7

告訴他們你是你的深度。 – 2013-04-29 14:25:24

+0

我做了嘿嘿。它的底部。 – 2013-04-29 14:26:24

+2

嗯,我不確定你如何從這個過程的'結果'中得到某些東西,因爲沒有結果,只有一個插入。 (結果最常被稱爲是什麼從選擇返回) – DrCopyPaste 2013-04-29 14:31:51

回答

2

這是可能適用於您的快速示例。這假定你想要插入數據的表取得了從SELECT聲明返回的所有值,並且它們是相同的類型。

請注意,您可能對CASE語句有點困惑的原因可能是由於在SQL中使用它們有兩種主要方式。 CASE WHEN...像你這裏和CASE #value# WHEN #result# THEN...。在網絡上稍微搜索一下會給你帶來一些很好的例子。例如this one

INSERT INTO TABLE_NAME 
(
    ScheduleType, 
    I01, 
    I02, 
    PRD, 
    PlannedQty, 
    AuthorizedQty, 
    WbsElementID 
) 
SELECT 
Lu_Schedule_Types.ScheduleType, 
SUM(CASE WHEN InductionProduction = 1 THEN CASE WHEN (LEFT(DATENAME(Month, 
        3)) = 'JAN' THEN ItemQty END END) AS I01, 
SUM(CASE WHEN InductionProduction = 1 THEN CASE WHEN (LEFT(DATENAME(Month,     Item_Schedule.ItemScheduleDate), 3)) 
        = 'FEB' THEN ItemQty END END) AS I02, 
SUM(CASE WHEN InductionProduction = 2 THEN ItemQty ELSE 0 END) AS PRD, 
LmpProjectInfo.PlannedQty, 
LmpProjectInfo.AuthorizedQty, 
LmpProjectInfo.WbsElementID 

FROM 
    Item_Schedule INNER JOIN 
    Lu_Schedule_Types ON Item_Schedule.ItemScheduleType = Lu_Schedule_Types.ScheduleTypeId RIGHT OUTER JOIN 
    LmpProjectInfo ON Item_Schedule.ItemWbsElement = LmpProjectInfo.WbsElementID 
WHERE 
    (Item_Schedule.IsActive = 1) AND (Item_Schedule.ItemWbsElement = @WbsElement) AND 
    (Item_Schedule.ItemScheduleDate < DATEADD(d, 1, @EndDate)) AND 
    (Item_Schedule.ItemScheduleDate >= @StartDate) 
GROUP BY Lu_Schedule_Types.ScheduleType, Lu_Schedule_Types.ScheduleTypeId, 
    LmpProjectInfo.PlannedQty, LmpProjectInfo.AuthorizedQty, 
    LmpProjectInfo.WbsElementID 
ORDER BY Lu_Schedule_Types.ScheduleTypeId 
+0

謝謝大家。我很感激。這非常有幫助。 – 2013-04-30 13:32:35

1

嘗試這一個 -

INSERT INTO dbo.table1 -- insert in table 
(
     ScheduleType 
    , I01 
    , I02 
    , PRD 
    , PlannedQty 
    , AuthorizedQty 
    , WbsElementID 
) 
SELECT 
     t.ScheduleType 
    , I01 = SUM(CASE WHEN InductionProduction = 1 AND MONTH(s.ItemScheduleDate) = 1 THEN ItemQty END) 
    , I02 = SUM(CASE WHEN InductionProduction = 1 AND MONTH(s.ItemScheduleDate) = 2 THEN ItemQty END) 
    , PRD = SUM(CASE WHEN InductionProduction = 2 THEN ItemQty END) 
    , i.PlannedQty 
    , i.AuthorizedQty 
    , i.WbsElementID 
--INTO #temp_table -- or insert in temp table 
FROM dbo.Item_Schedule s 
JOIN dbo.Lu_Schedule_Types t ON s.ItemScheduleType = t.ScheduleTypeId 
RIGHT JOIN dbo.LmpProjectInfo i ON s.ItemWbsElement = i.WbsElementID 
WHERE s.IsActive = 1 
    AND s.ItemWbsElement = @WbsElement 
    AND s.ItemScheduleDate < DATEADD(d, 1, @EndDate)) 
    AND s.ItemScheduleDate >= @StartDate 
GROUP BY 
     t.ScheduleType 
    , t.ScheduleTypeId 
    , i.PlannedQty 
    , i.AuthorizedQty 
    , i.WbsElementID 
ORDER BY t.ScheduleTypeId 
相關問題