SSRS 2008 存储过程未返回正确的记录集
当我在 VS 2008 中创建报表并尝试使用此存储过程时,似乎我只得到一条类似于插入命令的记录。我想要取回的是 #WIP
BEGIN
SET NOCOUNT OFF
DECLARE @BeginDate datetime
DECLARE @EndDate datetime --- Make sure month no 12
Declare @NextMonth INT
set @NextMonth = @Month + 1 --- Make sure month no 12
set @BeginDate = convert(datetime, convert(varchar(4),@Year) + right('00' + convert(varchar(2),@Month),2) + '01' ) ;
set @EndDate = convert(datetime, convert(varchar(4),@Year) + right('00' + convert(varchar(2),@NextMonth),2) + '01' ) ;
DECLARE @OwnerName nvarchar(50)
DECLARE @Value numeric(18,2)
DECLARE @Hours numeric(18,2)
DECLARE @Expenses numeric(18,2)
DECLARE @Discount numeric(18,2)
DECLARE @InvoceTotal numeric(18,2)
DECLARE @Progress numeric(18,2)
DECLARE @ActualBilled numeric(18,2)
DECLARE @MyCursor CURSOR
Create Table #WIP (
OwnerName varchar(50)
,BeginWIP numeric(18,2)
,EndingWIP numeric(18,2)
,PeriodAll numeric(18,2)
,PeriodCurent numeric(18,2)
,UnnatachedTime numeric(18,2)
,Progress numeric(18,2)
,Discount numeric(18,2)
,NewHours numeric(18,2)
,FeesStandard numeric(18,2)
,ActualBill numeric(18,2)
,Expenses numeric(18,2)
,TotalInvoice numeric(18,2)
,Real numeric(18,2) )
SET @MyCursor = CURSOR
FOR
select a.owneridname
,Sum(ISNULL(t1.tcpm_hoursentered,0)) as hrs ,SUM(t1.tcpm_billingatstandardrate) as Standardbilled
,SUM(t1.tcpm_ActualBilledAmount) as ActualBilled
,SUM(t1.tcpm_actualbilledamount) as Invoicetotal
from Filteredtcpm_timeItemValue t1
inner join Filteredtcpm_businessperiod b on t1.tcpm_businessperiodid = b.tcpm_businessperiodid
inner join FilteredSalesOrder s on t1.tcpm_projectid = s.salesorderid
inner join FilteredAccount a on s.accountid = a.accountid and a.statecode=0
where b.tcpm_startdate >= @BeginDate
and t1.tcpm_lastwipaction not in ('267120007','267120008','267120009') and t1.tcpm_hoursentered IS not null
group by a.owneridname
--OPEN @MyCursor
FETCH NEXT FROM @MyCursor INTO @OwnerName,@Hours,@Value,@ActualBilled,@InvoceTotal
WHILE @@FETCH_STATUS = 0
BEGIN
FETCH NEXT FROM @MyCursor
IF EXISTS(select 1 from #WIP where OwnerName = @OwnerName)
BEGIN
UPDATE #WIP
SET PeriodCurent=ISNULL(@Value,0)
,NewHours= @Hours
,ActualBill=@ActualBilled
,TotalInvoice=@InvoceTotal
WHERE OwnerName = @OwnerName
END
IF NOT EXISTS(select 1 from #WIP where OwnerName = @OwnerName)
BEGIN
INSERT INTO #WIP (OwnerName ,NewHours,ActualBill,TotalInvoice,PeriodCurent)
VALUES(@OwnerName,@Hours,@ActualBilled,@InvoceTotal,ISNULL(@Value,01))
END
FETCH NEXT FROM @MyCursor INTO @OwnerName,@Hours,@Value,@ActualBilled,@InvoceTotal
END
select OwnerName ,BeginWIP,NewHours,ActualBill,TotalInvoice,PeriodCurent,UnnatachedTime From #WIP
END的记录
When I create areport in VS 2008 and try to use this stored procedure it appears I get only one record that looks like from insert command. What I want to get back is record from #WIP
BEGIN
SET NOCOUNT OFF
DECLARE @BeginDate datetime
DECLARE @EndDate datetime --- Make sure month no 12
Declare @NextMonth INT
set @NextMonth = @Month + 1 --- Make sure month no 12
set @BeginDate = convert(datetime, convert(varchar(4),@Year) + right('00' + convert(varchar(2),@Month),2) + '01' ) ;
set @EndDate = convert(datetime, convert(varchar(4),@Year) + right('00' + convert(varchar(2),@NextMonth),2) + '01' ) ;
DECLARE @OwnerName nvarchar(50)
DECLARE @Value numeric(18,2)
DECLARE @Hours numeric(18,2)
DECLARE @Expenses numeric(18,2)
DECLARE @Discount numeric(18,2)
DECLARE @InvoceTotal numeric(18,2)
DECLARE @Progress numeric(18,2)
DECLARE @ActualBilled numeric(18,2)
DECLARE @MyCursor CURSOR
Create Table #WIP (
OwnerName varchar(50)
,BeginWIP numeric(18,2)
,EndingWIP numeric(18,2)
,PeriodAll numeric(18,2)
,PeriodCurent numeric(18,2)
,UnnatachedTime numeric(18,2)
,Progress numeric(18,2)
,Discount numeric(18,2)
,NewHours numeric(18,2)
,FeesStandard numeric(18,2)
,ActualBill numeric(18,2)
,Expenses numeric(18,2)
,TotalInvoice numeric(18,2)
,Real numeric(18,2) )
SET @MyCursor = CURSOR
FOR
select a.owneridname
,Sum(ISNULL(t1.tcpm_hoursentered,0)) as hrs ,SUM(t1.tcpm_billingatstandardrate) as Standardbilled
,SUM(t1.tcpm_ActualBilledAmount) as ActualBilled
,SUM(t1.tcpm_actualbilledamount) as Invoicetotal
from Filteredtcpm_timeItemValue t1
inner join Filteredtcpm_businessperiod b on t1.tcpm_businessperiodid = b.tcpm_businessperiodid
inner join FilteredSalesOrder s on t1.tcpm_projectid = s.salesorderid
inner join FilteredAccount a on s.accountid = a.accountid and a.statecode=0
where b.tcpm_startdate >= @BeginDate
and t1.tcpm_lastwipaction not in ('267120007','267120008','267120009') and t1.tcpm_hoursentered IS not null
group by a.owneridname
--OPEN @MyCursor
FETCH NEXT FROM @MyCursor INTO @OwnerName,@Hours,@Value,@ActualBilled,@InvoceTotal
WHILE @@FETCH_STATUS = 0
BEGIN
FETCH NEXT FROM @MyCursor
IF EXISTS(select 1 from #WIP where OwnerName = @OwnerName)
BEGIN
UPDATE #WIP
SET PeriodCurent=ISNULL(@Value,0)
,NewHours= @Hours
,ActualBill=@ActualBilled
,TotalInvoice=@InvoceTotal
WHERE OwnerName = @OwnerName
END
IF NOT EXISTS(select 1 from #WIP where OwnerName = @OwnerName)
BEGIN
INSERT INTO #WIP (OwnerName ,NewHours,ActualBill,TotalInvoice,PeriodCurent)
VALUES(@OwnerName,@Hours,@ActualBilled,@InvoceTotal,ISNULL(@Value,01))
END
FETCH NEXT FROM @MyCursor INTO @OwnerName,@Hours,@Value,@ActualBilled,@InvoceTotal
END
select OwnerName ,BeginWIP,NewHours,ActualBill,TotalInvoice,PeriodCurent,UnnatachedTime From #WIP
END
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你有一个单独的 FETCH NEXT ,在 while 循环的顶部没有 into 。
它正在为游标中的每隔一行创建一个结果集。评论出来,我想你会得到你想要的。
另外,由于额外的提取,您只处理游标的每隔一行。如果这是您的意图,那么您将需要在获取中添加一个并将这些值推入变量中。
You have a lone FETCH NEXT with no into at the top of your while loop.
It is creating a result set for every other row in your cursor. Comment it out and I think you will get what you want.
Also because of the extra fetch, you are only processing every other row of the cursor. If that was you intent, then you will need to add an into to the fetch and push thoes values into variables.