0

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

4

2 回答 2

0

根据您的描述,听起来您宁愿更新一行而不是插入一行。

尝试将您的插入更改为:

更新 dob.PMRInformation
SET = @AssignDate
WHERE PMRID = @PMRID

于 2015-06-25T13:56:20.343 回答
0

完全不清楚您要在这里做什么,但是您的所有 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 
于 2015-06-25T13:56:29.807 回答