0

我有一个必须插入 3 个表格的表格。我创建了一个存储过程(这是不正确的)。

3 个表:
1. dbo.Client (ClientID, ClientName, AddressID )
2. dbo.Contact (AddressID, Addressline1, Add2, CityID ) CityID 来自下拉列表。
3. dbo.PointOfCContact (POCID, FirstName, ...)

我的存储过程..

Create PROCEDURE sp_Clientdetails(
    @ClientName varchar(50),

    @Addressline1   varchar(50),
    @Addressline2   varchar(50),
    @CityID INT,

    @Firstname  varchar(50),
    @LastName   varchar(50),
    @Middlename  varchar(50),
    @Mob1 numeric(10,0),
    @Mob2 numeric(10,0),
    @Email1 varchar(50),
    @Email2 varchar(50),
    @phone varchar(50),
    @Extention INT  
    )
AS
DECLARE @AddressID int, @ClientID int, @POCID int;

Set NOCOUNT OFF

BEGIN TRANSACTION

IF NOT EXISTS(Select 1 FROM dbo.Contact Where AddressID=@AddressID)
BEGIN
    INSERT INTO dbo.Contact VALUES (@Addressline1, @Addressline2, @CityID);
    SET @AddressID = SCOPE_IDENTITY();
    SET @CityID = SCOPE_IDENTITY();
END
ELSE
    Select AddressID = @AddressID From dbo.Contact Where AddressID=@AddressID;

IF NOT EXISTS(Select 1 FROM dbo.Client Where ClientID=@ClientID)
BEGIN
    INSERT INTO dbo.Client VALUES (@ClientID, @AddressID, @CityID);
    SET @ClientID= SCOPE_IDENTITY();
END
ELSE
    Select ClientID = @ClientID From dbo.Client Where ClientID = @ClientID;

Insert into dbo.PointOfCContact VALUES (@Firstname, @LastName, @Middlename, @Mob1, @Mob2,
 @Email1, @Email2, @phone, @Extention);
SET @POCID= SCOPE_IDENTITY();

COMMIT

出现错误:列名或提供的值的数量与表定义不匹配。

INSERT INTO dbo.Contact VALUES (@Addressline1, @Addressline2, @CityID);

4

2 回答 2

1

您应该指定列名

INSERT INTO dbo.Contact (Address1, Adddres2, CityID) 
VALUES (@Addressline1, @Addressline2, @CityID);

....

INSERT INTO dbo.Client (ClientID, AddressID, CityID)
VALUES (@ClientID, @AddressID, @CityID);  

ETC

更好的是,您可以使用MERGE语法 http://technet.microsoft.com/en-us/library/bb510625.aspx

于 2012-08-10T14:49:28.203 回答
0

您必须添加结构表;向表中添加列

INSERT INTO dbo.Contact (Address1, Adddres2, CityID) VALUES (@Addressline1, @Addressline2, @CityID);
于 2012-08-10T14:49:51.357 回答