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

Nested If Statement with Insert Statements in SQL

所以我正在为网页编写一个存储过程,它将从网页中提取 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 存储到哪个部门。到目前为止,该过程运行但未将日期存储在正确的行中。它将创建一个新行并将批准日期插入到该新行中。能否请你帮忙。谢谢!

根据您的描述,您似乎更愿意更新一行而不是插入一行。

尝试将插入内容更改为:

更新dob.PMRInformation
SET = @AssignDate
其中 PMRID = @PMRID

完全不清楚您要在这里做什么,但是您所有的 IF ELSE IF THEN 逻辑都可以大大简化为单个插入语句。

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