我在 SQL Server 中有一个现有的存储过程,我需要从我的 C# 代码中调用它并获取结果。这是这个 SP 的样子
CREATE PROCEDURE [dbo].[sp_MSnextID_DDB_NextID]
@tablesequence varchar(40)
AS
declare @next_id integer
begin transaction
update DDB_NextID
set DDB_SEQUENCE = DDB_SEQUENCE + 1
where DDB_TABLE = @tablesequence
select @next_id = DDB_SEQUENCE from DDB_NextID
where DDB_TABLE = @tablesequence
commit transaction
RETURN @next_id
这是我的 C# 代码
using (OdbcConnection connection = new OdbcConnection(GetConnectionString()))
{
using (IDbCommand command = new OdbcCommand())
{
command.CommandText = "sp_MSnextID_DDB_NEXTID";
command.CommandType = CommandType.StoredProcedure;
IDbDataParameter parameter1 = command.CreateParameter();
parameter1.DbType = DbType.String;
parameter1.ParameterName = "@tablesequence";
parameter1.Value = "ddb_dc_document";
parameter1.Direction = ParameterDirection.Input;
command.Parameters.Add(parameter1);
IDbDataParameter parameter2 = command.CreateParameter();
parameter2.DbType = DbType.Int32;
parameter2.ParameterName = "@Return_Value";
parameter2.Direction = ParameterDirection.ReturnValue;
command.Parameters.Add(parameter2);
command.Connection = connection;
connection.Open();
command.ExecuteNonQuery();
IDbDataParameter o = (command.Parameters)["@Return_Value"] as IDbDataParameter;
//Got return value from SP in o.Value
}
}
问题是我遇到了异常
[42000] [Microsoft][SQL Native Client][SQL Server]过程或函数“sp_MSnextID_DDB_NextID”需要参数“@tablesequence”,但未提供该参数。
我无法解释或修复。我错过了什么?
为了找到解决方法,我尝试了不同的方法:执行以下查询,在临时表中设置数据
create table #temp (i integer); insert into #temp exec sp_MSNextID_DDB_NEXTID @tablesequence='ddb_dc_document';select * from #temp;
在这种情况下 SP 正确执行,但 select 返回零行!