日期:2014-05-17  浏览次数:20772 次

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

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 SqlParamet