读书人

很奇怪的output有关问题请高手帮忙

发布时间: 2012-12-27 10:17:10 作者: rapoo

很奇怪的output问题,请高手帮忙
存储过程如下:
USE ShanghaiWorld
GO
/****** Object: StoredProcedure [dbo].[GetGroupDataStat] Script Date: 2012/10/10 8:52:18 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:<Author,,Name>
-- Create date: <Create Date,,>
-- Description:<Description,,>
-- =============================================
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
declare @tmpValue decimal(12, 2)
if @EndTime is not null
begin
select @tmpValue = SUM(MeasureValue) from ElecData where MetaID = @MetaID and DATEPART(year, ReadTime) = @YearStat and ReadTime > @StartTime and ReadTime < @EndTime--今年使用量
if @tmpValue is not null set @StatYearData = @StatYearData + @tmpValue
select @tmpValue = SUM(MeasureValue) from ElecData where MetaID = @MetaID and DATEPART(year, ReadTime) = @YearStat and DATEPART(month, ReadTime) = @MonthStat and ReadTime > @StartTime and ReadTime < @EndTime--当前使用量
if @tmpValue is not null set @StatMonth = @StatMonth + @tmpValue
select @tmpValue = SUM(MeasureValue) from ElecData where MetaID = @MetaID and DATEPART(year, ReadTime) = @LastYear and DATEPART(month, ReadTime) = @MonthStat and ReadTime > @StartTime and ReadTime < @EndTime--去年同期


if @tmpValue is not null set @StatLastDate = @StatLastDate + @tmpValue
end
else
begin
select @tmpValue = SUM(MeasureValue) from ElecData where MetaID = @MetaID and DATEPART(year, ReadTime) = @YearStat and ReadTime > @StartTime--今年使用量
if @tmpValue is not null set @StatYearData = @StatYearData + @tmpValue
select @tmpValue = SUM(MeasureValue) from ElecData where MetaID = @MetaID and DATEPART(year, ReadTime) = @YearStat and DATEPART(month, ReadTime) = @MonthStat and ReadTime > @StartTime--当前使用量
if @tmpValue is not null set @StatMonth = @StatMonth + @tmpValue
select @tmpValue = SUM(MeasureValue) from ElecData where MetaID = @MetaID and DATEPART(year, ReadTime) = @LastYear and DATEPART(month, ReadTime) = @MonthStat and ReadTime > @StartTime--去年同期
if @tmpValue is not null set @StatLastDate = @StatLastDate + @tmpValue
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

请高手看是哪出了问题!
[解决办法]
补充一下,如果在

select @StatMonth as CurrentStatData

前面加个:

set @StatLastDate = 44.55

这样程序里也能取到44.55的值,否则就为null
[解决办法]
select @StatMonth as CurrentStatData 你写这个干啥 去掉

存储过程调用没问题
那就是你的c#代码调用output的问题了
[解决办法]
select @StatMonth as CurrentStatData

改成
return @StatMonth
试试
[解决办法]
改成return了,相应的output还是取不到,愁死我了
[解决办法]
已解决,在C#板块中可见
[解决办法]

引用:
select @StatMonth as CurrentStatData

改成
return @StatMonth
试试

搞错了,不好意思
[解决办法]
引用:
已解决,在C#板块中可见

看到了,被人鄙视了,还需努力啊
[解决办法]
引用:
引用:

已解决,在C#板块中可见

看到了,被人鄙视了,还需努力啊


一时失误再所难免,感谢你的指导!

读书人网 >SQL Server

热点推荐