1

我有一个tbl_Customers有两列的表,CustomerID并且CustomerGroupID. CustomerID是标识列。添加新记录时,我希望该CustomerGroupID值自动默认为该CustomerID值,除非提供了参数值,在这种情况下应将其设置为参数值。

这是我目前的存储过程(不包括CustomerGroupID列)

非常感谢 SQL Server 新手的任何帮助!

吉姆

ALTER PROCEDURE [dbo].[sp_AddCustomerUpdateCar] 
            @CarReg varchar(10),
            @CustTitle varchar(8),  
            @CustFName   varchar(15),   
            @CustSName varchar(20),
            @CustCompany varchar(30),   
            @CustTel1   varchar(15),    
            @CustTel2   varchar(15),    
            @CustAddress1 varchar(50),  
            @CustAddress2 varchar(50),  
            @CustAddress3 varchar(50),  
            @CustAddress4 varchar(50),  
            @CustPostCode varchar(9),   
            @CustEmail varchar(50),
            @CustCredit smallmoney,
            @CustCreditExpires datetime,
            @CustAccountID int,
            @CustSource varchar(50)

AS

INSERT INTO tbl_Customers 
            (CustTitle,
            CustFName,
            CustSName,
            CustCompany,
            CustTel1,
            CustTel2,
            CustAddress1,
            CustAddress2,
            CustAddress3,
            CustAddress4,
            CustPostCode,
            CustEmail,
            CustCredit,
            CustCreditExpires,
            CustAccountID,
            CustSource)

VALUES      (@CustTitle,
        @CustFName,
        @CustSName,
        @CustCompany,
        @CustTel1,
        @CustTel2,
        @CustAddress1,
        @CustAddress2,
        @CustAddress3,
        @CustAddress4,
        @CustPostCode,
        @CustEmail,
        @CustCredit,
        @CustCreditExpires,
        @CustAccountID,
        @CustSource)
4

3 回答 3

1

您可以创建一个在插入后设置默认值的触发器:

create trigger defaultGroupId on t after insert as
update t set groupID = id where id in (select id from inserted);

这是一个展示它的小提琴:http ://sqlfiddle.com/#!3/4e5b2/1

于 2013-07-26T00:41:08.197 回答
0

谢谢约翰,

我想我需要更多地了解触发器!

与此同时,我设法解决了我自己的问题,在我的 sp.末尾添加了一个 UPDATE。对于其他有类似问题的人,这里是代码:

set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go




ALTER PROCEDURE [dbo].[sp_AddCustomerUpdateCar] @CarReg varchar(10),
                                                @CustTitle  varchar(8), 
                                                @CustFName  varchar(15),    
                                                @CustSName  varchar(20),
                                                @CustCompany    varchar(30),    
                                                @CustTel1   varchar(15),    
                                                @CustTel2   varchar(15),    
                                                @CustAddress1   varchar(50),    
                                                @CustAddress2   varchar(50),    
                                                @CustAddress3   varchar(50),    
                                                @CustAddress4   varchar(50),    
                                                @CustPostCode   varchar(9), 
                                                @CustEmail  varchar(50),
                                                @CustCredit smallmoney,
                                                @CustCreditExpires datetime,
                                                @CustAccountID  int,
                                                @CustSource varchar(50),
                                                @CustomerGroupID int

AS

BEGIN TRAN

INSERT INTO tbl_Customers (CustTitle,
                            CustFName,
                            CustSName,
                            CustCompany,
                            CustTel1,
                            CustTel2,
                            CustAddress1,
                            CustAddress2,
                            CustAddress3,
                            CustAddress4,
                            CustPostCode,
                            CustEmail,
                            CustCredit,
                            CustCreditExpires,
                            CustAccountID,
                            CustSource)
VALUES (@CustTitle,
        @CustFName,
        @CustSName,
        @CustCompany,
        @CustTel1,
        @CustTel2,
        @CustAddress1,
        @CustAddress2,
        @CustAddress3,
        @CustAddress4,
        @CustPostCode,
        @CustEmail,
        @CustCredit,
        @CustCreditExpires,
        @CustAccountID,
        @CustSource)

IF @customerGroupID = 0 
BEGIN
    UPDATE tbl_Customers
    SET CustomerGroupID = SCOPE_IDENTITY()
    WHERE CustomerID = SCOPE_IDENTITY()
END

COMMIT TRAN
于 2013-07-29T15:07:01.833 回答
0

对有用的@John Tsend 答案的一个小改进:如果它是默认值,则可以在 INSERT 时将其设置为不同的值,并且不应覆盖该值。
所以我会将触发器更改为:

create trigger defaultGroupId on t after insert as
update t set groupID = id where id in 
      (select id from inserted where groupId is null);
于 2015-01-28T16:23:27.940 回答