2016-11-04 82 views
1

我試圖設置此存儲過程的ActionReq列爲未提供ActionReq時的Expiration + AdvancedCancel的值。 ActionReq和Expiration是日期時間,AdvancedCancel是一個int。我該怎麼做?我的SQL技能非常基礎。INSERT INTO語句中的情況

ALTER PROCEDURE [dbo].[insertIntoGrid] 
    @Vendor nvarchar(max), 
    @Product nvarchar(max), 
    @Type nvarchar(max), 
    @AccountCode nvarchar(max), 
    @2016AccCode nvarchar(max), 
    @BusinessUnit nvarchar(max), 
    @Group nvarchar(max), 
    @TIAYTD nvarchar(max), 
    @ContractPeriod nvarchar(max), 
    @RenewPeriod nvarchar(max), 
    @Expiration datetime, 
    @AdvancedCancel nvarchar(max), 
    @ActionReq datetime, 
    @Notes nvarchar(max), 
    @Division nvarchar(max) 
AS 
BEGIN 
    SET NOCOUNT ON; 

    -- Insert statements for procedure here 
    INSERT INTO TestRedo3(Vendor, [Product/Service], [Type], AccountCode, 
          [2016AccCode], BusinessUnit, [Group], TIAYTD, 
          ContractPeriod, RenewPeriod, Expiration, AdvancedCancel, 
          case [ActionReq] is null THEN dateadd(day, @AdvancedCancel, @Expiration) 
          else ActionReq 
          end as ActionReq, Notes, Division) 
    VALUES (@Vendor, @Product, @Type, @AccountCode, 
      @2016AccCode, @BusinessUnit, @Group, @TIAYTD, 
      @ContractPeriod, @RenewPeriod, @Expiration, @AdvancedCancel, 
      @ActionReq, @Notes, @Division) 
END 
+1

你真的有每一個列定義爲爲nvarchar(最大)? –

+0

你不能只是決定插入什麼列,就像你在做什麼。您需要將您的插入語句更改爲select語句。然後,您可以使用案例表達式來決定所需的列。 –

回答

4

您將需要您的插入改變這樣的事情。我不完全確定你在做什麼,但這應該是接近的。

INSERT INTO TestRedo3 
(
    Vendor, 
    [Product/Service], 
    [Type], 
    AccountCode, 
    [2016AccCode], 
    BusinessUnit, 
    [Group], 
    TIAYTD, 
    ContractPeriod, 
    RenewPeriod, 
    Expiration, 
    AdvancedCancel, 
    ActionReq, 
    Notes, 
    Division 
) 
select 
    @Vendor, 
    @Product, 
    @Type, 
    @AccountCode, 
    @2016AccCode, 
    @BusinessUnit, 
    @Group, 
    @TIAYTD, 
    @ContractPeriod, 
    @RenewPeriod, 
    @Expiration, 
    @AdvancedCancel, 
    case when @ActionReq is null THEN dateadd(day, @AdvancedCancel, @Expiration) 
     else @ActionReq 
    end, 
    @Notes, 
    @Division 
+0

非常感謝!爲什麼必須選擇並且不能使用值(@ parameter1 ...)? – HelixTitan

+0

您不必使用select語句,但case表達式必須是值的一部分。你可以切換這個,並使用值列表。 –

0

一個CASE的語法必須具備時,可以脫穎而出條件:

CASE [columnOptional] 
    WHEN .. 
     THEN .. 
     ELSE[optional] .. 
END AS 
+0

這應該是評論,而不是答案。 –

+0

爲什麼?這是他的問題的答案,給他充分的解決方案不會讓他學到一些東西。 –

+0

同意。但是OP在列表中有表達式的情況,所以這僅僅是部分答案。 –

0

試試這個:

ALTER PROCEDURE [dbo].[insertIntoGrid] 
-- Add the parameters for the stored procedure here 
@Vendor nvarchar(max), 
@Product nvarchar(max), 
@Type nvarchar(max), 
@AccountCode nvarchar(max), 
@2016AccCode nvarchar(max), 
@BusinessUnit nvarchar(max), 
@Group nvarchar(max), 
@TIAYTD nvarchar(max), 
@ContractPeriod nvarchar(max), 
@RenewPeriod nvarchar(max), 
@Expiration datetime, 
@AdvancedCancel nvarchar(max), 
@ActionReq datetime, 
@Notes nvarchar(max), 
@Division nvarchar(max) 
AS 
BEGIN 
-- SET NOCOUNT ON added to prevent extra result sets from 
-- interfering with SELECT statements. 
SET NOCOUNT ON; 

-- Insert statements for procedure here 
INSERT INTO TestRedo3(Vendor,[Product/Service],[Type],AccountCode, 
[2016AccCode],BusinessUnit,[Group],TIAYTD,ContractPeriod,RenewPeriod, 
Expiration,AdvancedCancel,[ActionReq],Notes,Division) VALUES (@Vendor, 
@Product,@Type,@AccountCode,@2016AccCode,@BusinessUnit,@Group,@TIAYTD, 
@ContractPeriod,@RenewPeriod,@Expiration,@AdvancedCancel, 
case when @ActionReq is null THEN dateadd(day, @AdvancedCancel, @Expiration) 
else @ActionReq 
end,@Notes,@Division) 
END 
1

嘗試是這樣的

INSERT INTO TestRedo3(
Vendor, 
[Product/Service], 
[Type], 
AccountCode, 
[2016AccCode], 
BusinessUnit, 
[Group], 
TIAYTD, 
ContractPeriod, 
RenewPeriod, 
Expiration, 
AdvancedCancel, 
ActionReq, 
Notes, 
Division 
) VALUES (
@Vendor, 
@Product, 
@Type, 
@AccountCode 
@2016AccCode, 
@BusinessUnit, 
@Group, 
@TIAYTD, 
@ContractPeriod, 
@RenewPeriod, 
@Expiration, 
@AdvancedCancel, 
case @ActionReq is null THEN dateadd(day, @AdvancedCancel, @Expiration) 
else @ActionReq 
end , 
@Notes, 
@Division 
)