取存储过程output的问题,请高手帮忙!

jjsbs 2012-10-10 10:03:16
存储过程如下:

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[GetGroupDataStat]
-- Add the parameters for the stored procedure here
@GroupId nvarchar(50), --集团ID
@YearStat int, --统计年份
@MonthStat int, --统计月份
@StatLastDate decimal(12, 2) = 1 output, --去年同期使用量
@StatYearData decimal(12, 2) = 1 output --今年使用量
AS
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON
declare @StatMonth decimal(12, 2) = 0 --当前使用量
declare @LastYear int --去年的年份
set @LastYear = @YearStat - 1

declare @tmpMetaID table --临时表,存放设备信息
(
[id] int IDENTITY(1,1),
[MetaID] nvarchar(50),
[StartTime] datetime,
[EndTime] datetime null
)

--获取所有设备及其使用时间(大于上一年)
insert into @tmpMetaID select a.MetaID, a.StartTime, a.EndTime from TenantMetaMapping a
inner join GroupTenantMapping b on a.TenantID = b.TenantID
where DATEPART(year, a.StartTime) >= @LastYear and DATEPART(month, a.StartTime) >= 1 and b.GroupID = @GroupId

declare @tmpMaxID int --临时表的记录条数
select @tmpMaxID = MAX(id) from @tmpMetaID

declare @MetaID nvarchar(50)
declare @StartTime datetime
declare @EndTime datetime

while @tmpMaxID > 0
begin
select @MetaID = [MetaID], @StartTime = [StartTime], @EndTime = [EndTime] from @tmpMetaID where [id] = @tmpMaxID
if @EndTime is not null
begin
select @StatYearData = @StatYearData + ISNULL(SUM(MeasureValue), 0) from ElecData where MetaID = @MetaID and DATEPART(year, ReadTime) = @YearStat and ReadTime > @StartTime and ReadTime < @EndTime --今年使用量
select @StatMonth = @StatMonth + ISNULL(SUM(MeasureValue), 0) from ElecData where MetaID = @MetaID and DATEPART(year, ReadTime) = @YearStat and DATEPART(month, ReadTime) = @MonthStat and ReadTime > @StartTime and ReadTime < @EndTime --当前使用量
select @StatLastDate = @StatLastDate + ISNULL(SUM(MeasureValue), 0) from ElecData where MetaID = @MetaID and DATEPART(year, ReadTime) = @LastYear and DATEPART(month, ReadTime) = @MonthStat and ReadTime > @StartTime and ReadTime < @EndTime --去年同期

end
else
begin
select @StatYearData = @StatYearData + ISNULL(SUM(MeasureValue), 0) from ElecData where MetaID = @MetaID and DATEPART(year, ReadTime) = @YearStat and ReadTime > @StartTime --今年使用量
select @StatMonth = @StatMonth + ISNULL(SUM(MeasureValue), 0) from ElecData where MetaID = @MetaID and DATEPART(year, ReadTime) = @YearStat and DATEPART(month, ReadTime) = @MonthStat and ReadTime > @StartTime --当前使用量
select @StatLastDate = @StatLastDate + ISNULL(SUM(MeasureValue), 0) from ElecData where MetaID = @MetaID and DATEPART(year, ReadTime) = @LastYear and DATEPART(month, ReadTime) = @MonthStat and ReadTime > @StartTime --去年同期

end
set @tmpMaxID = @tmpMaxID - 1
end

select @StatMonth as CurrentStatData

set nocount off

如果直接在查询分析器中运行,代码如下:
declare @aa decimal(12, 2) = 0
declare @bb decimal(12, 2) = 0
exec [GetGroupDataStat] '0003', 2012, 10, @aa output, @bb output

print @aa
print @bb

返回值都output都没有问题

但如果我在程序中运行:

public decimal GetCurrentStatValue(string groupID, int iYear, int iMonth, out decimal lastDateStat, out decimal yearStat)
{
SqlParameter[] parameters = {
new SqlParameter("@GroupId", SqlDbType.NVarChar, 50), //集团ID
new SqlParameter("@YearStat", SqlDbType.Int), //年份
new SqlParameter("@MonthStat", SqlDbType.Int), //月份
new SqlParameter("@StatLastDate", SqlDbType.Decimal, 9), //去年同期使用量
new SqlParameter("@StatYearData", SqlDbType.Decimal, 9), //今年使用量
};
parameters[0].Value = groupID;
parameters[1].Value = iYear;
parameters[2].Value = iMonth;
parameters[3].Precision = 12;
parameters[3].Scale = 2;
parameters[3].Direction = ParameterDirection.Output;
parameters[4].Precision = 12;
parameters[4].Scale = 2;
parameters[4].Direction = ParameterDirection.Output;

DataSet ds = DbHelperSQL.RunProcedure("GetGroupDataStat", parameters, "ds");
lastDateStat = parameters[3].Value.ToString() == "" ? 0 : (decimal)parameters[3].Value;
yearStat = parameters[4].Value.ToString() == "" ? 0 : (decimal)parameters[4].Value;
return ds.Tables[0].Rows[0][0].ToString() == "" ? 0 : (decimal)ds.Tables[0].Rows[0][0];
}

返回值没有问题,但output却总是null

请高手看是哪出了问题!
...全文
150 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
汤姆克鲁斯 2012-10-10
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 的回复:]
一:贴下DbHelperSQL.RunProcedure的代码
二:添加
parameters[3].Value = 1;
parameters[4].Value = 1;
三:2楼那家伙是来捣乱的,SQL基础太差,那个勋章简直就是假的。
[/Quote]
谢谢教诲,以后好好努力
jokeesloat 2012-10-10
  • 打赏
  • 举报
回复
我看了你的储存过程代码,在代码中,你为你的output变量赋值是使用
select @StatYearData = @StatYearData + ISNULL(SUM(MeasureValue), 0)

在数据库中,所有与 NULL 做 + - * / 的结果都是NULL.
在你的
declare @aa decimal(12, 2) = 0
declare @bb decimal(12, 2) = 0
exec [GetGroupDataStat] '0003', 2012, 10, @aa output, @bb output

代码中,为两个output变量赋值了,它们的初始值就不是NULL,所有输出正确的结果;
在你的托管代码中
 out decimal lastDateStat, out decimal yearStat

两个output变量没有赋值,也就是说初始值是 null ,那么它们的计算结果也就是NULL了。

select 1 + null 
的结果为 NULL。
jjsbs 2012-10-10
  • 打赏
  • 举报
回复
感谢 qldsrx
加了
parameters[3].Value = 0;
parameters[4].Value = 0;

然后改为

parameters[3].Direction = ParameterDirection.InputOutput;
parameters[4].Direction = ParameterDirection.InputOutput;

问题解决!
qldsrx 2012-10-10
  • 打赏
  • 举报
回复
一:贴下DbHelperSQL.RunProcedure的代码
二:添加
parameters[3].Value = 1;
parameters[4].Value = 1;
三:2楼那家伙是来捣乱的,SQL基础太差,那个勋章简直就是假的。
jjsbs 2012-10-10
  • 打赏
  • 举报
回复
改成return,返回值就没有了。

我之前有一个分页的存储过程,一切正常,但不知道这个为什么会这样,纠结中
汤姆克鲁斯 2012-10-10
  • 打赏
  • 举报
回复
select @StatMonth as CurrentStatData

改成
return @StatMonth
试试
jjsbs 2012-10-10
  • 打赏
  • 举报
回复
补充一下,如果在

select @StatMonth as CurrentStatData

前面加个:

set @StatLastDate = 44.55

这样程序里也能取到44.55的值,否则就为null

110,535

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧