我目前正在处理我的小型数据库项目,但我已经遇到了问题。我有两个表,生成的脚本发布在下面,现在我正在尝试编写一个程序,该程序将首先将记录添加到较小的记录中,然后使用第一个记录中的 ID 添加到第二个记录。但是我收到一个错误:“列名或提供的值的数量与表定义不匹配。” 在尝试将默认值添加到表 Klienci 的行中。我不知道发生了什么,因为没有列或参数,所以应该没有问题,至少像这个一样。
第一张表:
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[KlienciIndywidualni](
[IDKlientaIndywidualnego] [int] IDENTITY(1,1) NOT NULL,
[IDKlienta] [int] NOT NULL,
[Imie] [nvarchar](50) NOT NULL,
[Nazwisko] [nvarchar](50) NOT NULL,
[NumerLegitymacji] [int] NULL,
[Adres] [nvarchar](50) NOT NULL,
[Miasto] [nvarchar](50) NOT NULL,
[KodPocztowy] [nvarchar](50) NOT NULL,
[Kraj] [nvarchar](50) NOT NULL,
[Telefon] [int] NOT NULL,
[Email] [nvarchar](50) NOT NULL,
CONSTRAINT [PK_KlienciIndywidualni] PRIMARY KEY CLUSTERED
(
[IDKlientaIndywidualnego] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[KlienciIndywidualni] WITH CHECK ADD CONSTRAINT [FK_KlienciIndywidualni_Klienci] FOREIGN KEY([IDKlienta])
REFERENCES [dbo].[Klienci] ([IDKlienta])
GO
ALTER TABLE [dbo].[KlienciIndywidualni] CHECK CONSTRAINT [FK_KlienciIndywidualni_Klienci]
GO
ALTER TABLE [dbo].[KlienciIndywidualni] WITH CHECK ADD CONSTRAINT [CK_KlienciIndywidualni] CHECK ((len([Telefon])>=(9)))
GO
ALTER TABLE [dbo].[KlienciIndywidualni] CHECK CONSTRAINT [CK_KlienciIndywidualni]
GO
ALTER TABLE [dbo].[KlienciIndywidualni] WITH CHECK ADD CONSTRAINT [CK_KlienciIndywidualni_mail] CHECK (([Email] like '%@%.[a-z][a-z][a-z]'))
GO
ALTER TABLE [dbo].[KlienciIndywidualni] CHECK CONSTRAINT [CK_KlienciIndywidualni_mail]
GO
第二个:
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Klienci](
[IDKlienta] [int] IDENTITY(1,1) NOT NULL,
CONSTRAINT [PK_Klienci] PRIMARY KEY CLUSTERED
(
[IDKlienta] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
程序:
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE dodaj_klienta_indywidualnego
-- parametry
@Imie nvarchar(50),
@Nazwisko nvarchar(50),
@Adres nvarchar(50),
@KodPocztowy nvarchar(50),
@Miasto nvarchar(50) ,
@Kraj nvarchar(50),
@Telefon int,
@Email nvarchar(50),
@NumerLegitymacji nvarchar(50) = null
AS
BEGIN
SET NOCOUNT ON;
declare @IDKlienta as int;
begin try
begin tran
--dodaj klienta
INSERT INTO Klienci
DEFAULT VALUES
set @IDKlienta = @@IDENTITY;
--dodaj klienta indywidualnego
INSERT INTO KlienciIndywidualni
VALUES(@IDKlienta, @Imie, @Nazwisko, @Adres, @Miasto,
@KodPocztowy, @Kraj, @Telefon, @Email);
COMMIT TRAN
end try
begin catch
declare @error as varchar(127)
set @error = (Select ERROR_MESSAGE())
RAISERROR('Nie mozna dodac osoby-klienta, blad danych. %s', 16, 1, @error);
ROLLBACK TRAN
end catch
END
GO