INSERT INTO语句中的案例

时间:2016-11-04 15:42:23

标签: sql sql-server stored-procedures

我尝试在未提供ActionReq时将此存储过程的ActionReq列设置为Expiration + AdvancedCancel的值。 ActionReq和Expiration是日期时间,AdvancedCancel是int。我怎么办?我的SQL技能非常基础。

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

4 个答案:

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

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

答案 2 :(得分:0)

CASE的语法必须具备以下条件:

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

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