我想创建一个存储过程,然后获取我刚刚创建的ID
(身份)
这就是我所做的:
ALTER PROCEDURE [dbo].[SP_Insert_Hai_Pi]
@id INT OUT,
@email VARCHAR(350) = NULL,
@datetimeNow datetime,
@score INT = 0
AS
INSERT INTO TBL_HAI_PI (USER_EMAIL,DATETIME_GAME,CORRECT_ANSWERS) VALUES (@email, CURRENT_TIMESTAMP,@score)
SET @id = SCOPE_IDENTITY();
return @id
然后:
SqlCommand command = new SqlCommand("SP_Insert_Hai_Pi", conn);
command.CommandType = CommandType.StoredProcedure;
command.Parameters.Add("@email", SqlDbType.VarChar).Value = email;
conn.Open();
int tempId = command.ExecuteNonQuery();
conn.Close();
return tempId;
Tt告诉我:
过程或函数'SP_Insert_Hai_Pi'需要参数'@id',这是未提供的。
怎么了?
答案 0 :(得分:0)
即使是输出,您也必须定义它:
.....
command.CommandType = CommandType.StoredProcedure;
SqlParameter outputIdParam = new SqlParameter("@ID", SqlDbType.Int)
{
Direction = ParameterDirection.Output
};
command.Parameters.Add(outputIdParam);
command.Parameters.Add("@email", SqlDbType.VarChar).Value = email;
.....