对存储过程的困惑

Confusion about Stored Procedure

我已经编写了一个用于将数据插入我的 table 的存储过程。这些是我的 table 的列及其数据类型:

Ad nvarchar(150),
Yazar nvarchar(150),
SayfaSayisi smallint,
KategoriId int
Gmc datetime,
HostName  nvarchar(150)

问题是 GmcHostName 有自己的默认值。所以我不能在存储过程中使用这两个。

Gmc ---> GetDate() (to get insert date)
HostName --> Host_Name(  )

因此,当我执行查询时出现此错误。

There are more columns in the INSERT statement than values specified in the VALUES clause. The number of values in the VALUES clause must match the number of columns specified in the INSERT statement

这是查询

Create proc Kitap_Insert
    @Ad nvarchar(150),
    @Yazar nvarchar(150),
    @SayfaSayisi smallint,
    @KategoriId int
    Gmc datetime,
    HostName nvarchar(150)
as
    Insert into Kitap(Id, Ad, Yazar, SayfaSayisi, KategoriId)
    values(@Ad, @Yazar, @SayfaSayisi, @KategoriId)

这样做的正确方法是什么?

您需要从插入列表

中删除 ID
  Insert into Kitap(Ad,Yazar,SayfaSayisi,KategoriId)
     values(@Ad,@Yazar,@SayfaSayisi,@KategoriId)

或如下为其添加一个值

  Insert into Kitap(Id,Ad,Yazar,SayfaSayisi,KategoriId)
     values(@ID, @Ad,@Yazar,@SayfaSayisi,@KategoriId)

而不是:

Insert into Kitap(Id,Ad,Yazar,SayfaSayisi,KategoriId)
 values(@Ad,@Yazar,@SayfaSayisi,@KategoriId)

使用:

INSERT INTO Kitap(Ad,Yazar,SayfaSayisi,KategoriId)
VALUES (@Ad,@Yazar,@SayfaSayisi,@KategoriId)

您要求 SQL 引擎您也将提供 id(附加字段)(table 中不存在的字段或者是自动递增字段)并且您是没有提供相同的价值,因此你的错误 here are more columns in the INSERT statement than values specified in the VALUES clause. The number of values in the VALUES clause must match the number of columns specified in the INSERT statement

因此请从您的插入查询中删除额外的 ID。

您收到的错误是因为您试图将值插入到比 Values Clause 中指定的更多的列名中。

如果您有 ID 列作为 table 中的 Auto-increment 字段,那么您不必在其中包含 ID 列,因此您的插入查询将如下所示: -

Insert into Kitap
           (Ad,Yazar,SayfaSayisi,KategoriId)
       values
           (@Ad,@Yazar,@SayfaSayisi,@KategoriId)

如果您没有 ID 列作为 table 中的 Auto-increment 字段,那么您也在 Value Clause 中为该 id 列提供值,这样您的插入查询将像这样:-

注意:-Insert Query

中使用它之前,您必须计算并将值设置为 @Id 变量
  Declare @Id as INT
  SET @ID = ---- set here with some value which will become Primary key(I think)
  Insert into Kitap
           (Id,Ad,Yazar,SayfaSayisi,KategoriId)
       values
           (@Id, @Ad,@Yazar,@SayfaSayisi,@KategoriId)