错误:列名或提供值的数目不匹配表定义[关闭](Error : Column name or numb

2019-10-17 15:02发布

我目前正在对我的小数据库的项目,但我已经遇到的问题。 我AVE脚本下方贴,现在我想写使用它的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

Answer 1:

你的第二个插入需要十个参数,你只提供9,如果你跳过NumerLegitymacji领域,那么你应该通过它的名称来指定每一列,使插入目前正等待最后的NOT NULL列。

    --dodaj klienta indywidualnego
    INSERT INTO KlienciIndywidualni (
    IDKlienta,
    Imie,
    Nazwisko,
    Adres,
    Miasto,
    KodPocztowy,
    Kraj,
    Telefon,
    Email
)
    VALUES(@IDKlienta, @Imie, @Nazwisko, @Adres, @Miasto, 
    @KodPocztowy, @Kraj, @Telefon, @Email);
    COMMIT TRAN


文章来源: Error : Column name or number of supplied values does not match table definition [closed]