触发器,一条SQL语句不能对多个表进行数据操作吧

解决方案 »

  1.   

    给你写个我的:CREATE PROCEDURE [dbo].[User_Insert]@FirstName nvarchar(50),
    @LastName nvarchar(50),
    @AddressLine nvarchar(50),
    @AddressLine2 nvarchar(50),
    @City nvarchar(50),
    @State nvarchar(50),
    @PostalCode nvarchar(50),
    @Phone nvarchar(50),
    @Phone2 nvarchar(50),
    @Fax nvarchar(50),
    @Email nvarchar(50),
    @EndUserTypeID int,
    @Password nvarchar(50),
    @IsSubscribed bitAS--Start the transaction
    BEGIN TRANSACTIONDECLARE @AddressID int
    DECLARE @ContactInformationID int INSERT INTO Address
    (AddressLine,
     AddressLine2,
     City,
     State,
     PostalCode)
    VALUES
    (@AddressLine,
     @AddressLine2,
     @City,
     @State,
     @PostalCode)-- Rollback the transaction if there were any errors
    IF @@ERROR <> 0
     BEGIN
        -- Rollback the transaction
        ROLLBACK    -- Raise an error and return
        RAISERROR ('Error INSERT INTO Address.', 16, 1)
        RETURN
     END SET @AddressID = @@IDENTITY INSERT INTO ContactInformation
    (Phone,
     Phone2,
     Fax,
     Email)
    VALUES
    (@Phone, 
     @Phone2, 
     @Fax, 
     @Email)  -- Rollback the transaction if there were any errors
    IF @@ERROR <> 0
     BEGIN
        -- Rollback the transaction
        ROLLBACK    -- Raise an error and return
        RAISERROR ('Error INSERT INTO ContactInformation', 16, 1)
        RETURN
     ENDSET @ContactInformationID = @@IDENTITY
    -- Next Step
    INSERT INTO EndUser
               (EndUserTypeID,
                FirstName,
                LastName,
                AddressID,
                ContactInformationID,
                Password,
                IsSubscribed)
         VALUES
               (@EndUserTypeID,
                @FirstName,
                @LastName,
                @AddressID,
                @ContactInformationID,
                @Password,
                @IsSubscribed)-- Rollback the transaction if there were any errors
    IF @@ERROR <> 0
     BEGIN
        -- Rollback the transaction
        ROLLBACK    -- Raise an error and return
        RAISERROR ('Error INSERT INTO EndUser', 16, 1)
        RETURN
     ENDSELECT @@IDENTITYCOMMIT