我有一个必须插入 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);