添加rowguid列会破坏此存储过程吗?

Ref*_*din 1 sql t-sql sql-server replication stored-procedures

以下存储过程将不允许我将其添加到修改它.尝试修改它时,我收到以下错误 - >

Msg 213, Level 16, State 1, Procedure spPersonRelationshipAddOpposing, Line 51 Insert Error: Column name or number of supplied values does not match table definition.

此外,由于DB已设置为Merge Rep(已添加rowguid列),因此此存储过程现在不再正常工作.

我是否需要更改列的列出方式?设置Merge Rep时的一个警告是 - >

Adding Guid Column MAY Cause INSERT Statements without column lists to Fail

那是什么意思?我该如何解决?

USE [Connect]
GO
/****** Object:  StoredProcedure [dbo].[spPersonRelationshipAddOpposing]    Script Date: 07/15/2009 08:14:35 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[spPersonRelationshipAddOpposing]
@ExistingRelationshipID INT 
AS
BEGIN
--Declare local variables
DECLARE @PersonID INT  --PersonID of established relarionship
DECLARE @RelatedID INT  --RelatedID of established relarionship
DECLARE @Relationship VARCHAR(4)  --Established relarionship
DECLARE @RelatedSex as VARCHAR(1)  
DECLARE @OpposingRelationship VARCHAR(4)
DECLARE @OpposingRelationshipID INT
--Fill variables from existing relationship
SELECT @PersonID = PersonID, @RelatedID = RelatedID, @Relationship=PersonRelationshipTypeID
FROM tblPersonRelationship where PersonRelationshipID = @ExistingRelationshipID
--Get gender of relative for finding opposing relationship type
SELECT @RelatedSex = (SELECT Gender FROM tblPerson WHERE PersonID = @PersonID)
--get opposing relationship types
IF (@RelatedSex='M')
    BEGIN
    SELECT @OpposingRelationship = (SELECT OpposingMaleRelationship 
                                    From tblAdminPersonRelationshipType 
                                    WHERE PersonRelationshipTypeID = @Relationship)
    END
ELSE IF (@RelatedSex='F')
    BEGIN
    SELECT @OpposingRelationship = (SELECT OpposingFemaleRelationship 
                                    From tblAdminPersonRelationshipType 
                                    WHERE PersonRelationshipTypeID = @Relationship)
    END
--check for existing opposing relationship
SELECT @OpposingRelationshipID = (SELECT MAX(PersonRelationshipID) FROM tblPersonRelationship WHERE PersonID = @RelatedID AND RelatedID = @PersonID)
--if an opposing relationship was found

IF (@OpposingRelationship IS NOT NULL)
    BEGIN
--if there is a relationship, update it
    IF ISNUMERIC(@OpposingRelationshipID)=1 
        BEGIN
            UPDATE tblPersonRelationship
            SET PersonRelationshipTypeID = @OpposingRelationship,
                MarriageDate = (SELECT MarriageDate FROM tblPersonRelationship WHERE PersonRelationshipID = @ExistingRelationshipID),
                ResidesWithPersonFlag = (SELECT ResidesWithPersonFlag FROM tblPersonRelationship WHERE PersonRelationshipID = @ExistingRelationshipID),
                UpdateDateTime = (SELECT UpdateDateTime FROM tblPersonRelationship WHERE PersonRelationshipID = @ExistingRelationshipID),
                UpdateProgram = (SELECT UpdateProgram FROM tblPersonRelationship WHERE PersonRelationshipID = @ExistingRelationshipID),
                UpdateUserID = (SELECT UpdateUserID FROM tblPersonRelationship WHERE PersonRelationshipID = @ExistingRelationshipID) 
            WHERE PersonRelationshipID = @OpposingRelationshipID
        END
--otherwise add record
    ELSE IF (@OpposingRelationship IS NOT NULL)
        BEGIN
            INSERT INTO tblPersonRelationship 
                SELECT @RelatedID, @OpposingRelationship, @PersonID,
                       MarriageDate, NULL, NULL, 
                       ResidesWithPersonFlag, NULL, UpdateDateTime, UpdateProgram,
                       UpdateUserID, UpdateDateTime, UpdateProgram, 
                       UpdateUserID, NULL FROM tblPersonRelationship WHERE PersonRelationshipID = @ExistingRelationshipID
        END
    END
END
Run Code Online (Sandbox Code Playgroud)

Vla*_*adV 11

在执行INSERT时,应始终明确指定列的列表.像这样重写你的代码:

INSERT INTO tblPersonRelationship (RelatedID, PersonRelationshipID, PersonID, ...)
SELECT @RelatedID, @OpposingRelationship, @PersonID, ...
Run Code Online (Sandbox Code Playgroud)

隐式添加了一个guid列以支持合并复制,这就是为什么你得到有关列列表不匹配的错误.

  • 不,您不必显式列出具有默认值的列,因为SQL会将它们默认为值.但是,请确保您没有列出已启用自动增量的IDENTITY列. (4认同)