嵌套If语句中带有插入语句的语句

时间:2015-06-25 13:44:50

标签: sql sql-server sql-server-2008 sql-insert nested-if

所以我正在为网页编写一个存储过程,该网页将从网页中提取3个参数,然后根据其他2的值存储一个。

ALTER  PROCEDURE [dbo].[PMRAssignDate]

                @PMRID int,
                @Department varchar(255),
                @AssignDate date
AS
BEGIN
    IF EXISTS(SELECT * FROM [ProductInformation].[dbo].[PMRInformation] WHERE PMRID = @PMRID)

        Begin
        IF @Department='Engineering'
            Begin
            Insert INTO
                    [dbo].[PMRInformation]
                    (EngineeringApprovalDate)
            Values
                    (@AssignDate)
            End

        Else IF (@Department='Operations')
            Begin
            Insert INTO
                    [dbo].[PMRInformation]
                    (OperationsApprovalDate)
            Values
                    (@AssignDate)
                End
        Else IF (@Department='AME')
            Begin
            Insert INTO
                    [dbo].[PMRInformation]
                    (AMEApprovalDate)
            Values
                    (@AssignDate)
            End
        Else IF @Department='Finance'
            Begin
            Insert INTO
                    [dbo].[PMRInformation]
                    (FinanceApprovalDate)
            Values
                    (@AssignDate)
                    End
        Else IF @Department='Marketing'
        Begin
            Insert INTO
                    [dbo].[PMRInformation]
                    (MarketingApprovalDate)
            Values
                    (@AssignDate)
        End
        Else IF @Department='Tester'
            Begin
            Insert INTO 
                    [dbo].[PMRInformation]
                    (EngineeringApprovalDate, MarketingApprovalDate, AMEApprovalDate, FinanceApprovalDate, OperationsApprovalDate)
            Values
                    (@AssignDate,@AssignDate,@AssignDate,@AssignDate,@AssignDate)
    End     

    End End

因此,此存储过程必须找到PMRID等于来自网页的已发送@PMRID的行。然后,它必须使用@Department变量来确定将批准日期@AssignDate存储到哪个部门。到目前为止,该过程运行但它没有将日期存储在正确的行中。它将创建一个新行并将批准日期插入该新行。能否请你帮忙。谢谢!

2 个答案:

答案 0 :(得分:0)

从你的描述来看,听起来你宁愿UPDATE一行而不是INSERT一行。

尝试将插入更改为:

更新dob.PMRInformation
SET = @AssignDate
在哪里PMRID = @PMRID

答案 1 :(得分:0)

你在这里尝试做什么并不清楚,但你所有的IF如果那么逻辑可以大大简化为单个插入语句。

Insert INTO [dbo].[PMRInformation] (EngineeringApprovalDate, OperationsApprovalDate, AMEApprovalDate, FinanceApprovalDate, MarketingApprovalDate)
select 
    case @Department
        when 'Engineering' then @AssignDate
        when 'Tester' then @AssignDate
        else NULL
    end , 
    case @Department
        when 'Operations' then @AssignDate
        when 'Tester' then @AssignDate
        else NULL
    end , 
    case @Department
        when 'AME' then @AssignDate
        when 'Tester' then @AssignDate
        else NULL
    end , 
    case @Department
        when 'Finance' then @AssignDate
        when 'Tester' then @AssignDate
        else NULL
    end , 
    case @Department
        when 'Marketing' then @AssignDate
        when 'Tester' then @AssignDate
        else NULL
    end