如何使用此SQL存储过程创建INSERT语句?

时间:2013-04-29 14:19:39

标签: sql sql-server stored-procedures sql-insert

一位同事给了我这个存储过程并请求帮助根据其结果创建一个sql INSERT语句,但是对某些方法有点困惑。例如,我从未在sql中看到过CASE语句。我查了一下,但用法与我给出的不同。

这是存储过程

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

我应该帮助他,但我到目前为止是一个数据库向导,而且我的深度有点偏离。我非常感谢帮助/建议/指导。

非常感谢!

2 个答案:

答案 0 :(得分: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

答案 1 :(得分: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