将结果输入 table 的存储过程

Stored procedure to enter Results into a table

我正在尝试创建一个存储过程,从 C# 程序中获取数据,然后将数据输入到一个名为 dbo.Results 的 table 中,从 dbo.People table.

create Procedure InsertResults
    @ForName nvarchar(50),
    @Surname nvarchar(50)
    @RaceID int,
    @Place int,
    @Fleet nchar(10)
AS
Begin

Insert Into [dbo].[Results] values
    (IDENT_CURRENT(dbo.Results),@RaceID,(Select UserID From dbo.People Where (ForName = @ForName and Surname = @Surname)),@Place,@Fleet)
End

当我执行 SQL 查询以创建过程时 returns

Msg 4104, Level 16, State 1, Procedure InsertResults, Line 11 [Batch Start Line 0]
The multi-part identifier "dbo.Results" could not be bound.

  • IDENT_CURRENT() 接受一个字符串,但是 you don't want to do that anyway

  • 您遗漏了 IDENTITY 列而不是尝试查找它

  • 如果你想 INSERT ... SELECT 你不要使用 VALUES:

    INSERT dbo.Results(all,cols,except,identity)
      SELECT @RaceID,UserID,@Place,@Fleet
        FROM dbo.People 
        WHERE ForName = @ForName 
          AND Surname = @Surname;
    

(另外,让我想知道当你有两个不同的人名为 John Smith 时你期望发生什么。)