2011-10-03 66 views
0

當我在VS 2008中創建areport並嘗試使用此存儲過程時,它看起來似乎只有一條看起來像插入命令的記錄。我想回去是記錄從#WIPSSRS 2008存儲過程不返回正確的記錄集

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 
      ,[email protected] 
      ,[email protected] 
      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

+1

在SQL Server 2008上,您應該使用** MERGE **命令而不是逐行拼湊行遊標! –

+1

他不僅可以使用'MERGE',但是如果我正在閱讀這個權限,RBAR光標只是檢查是否存在並插入或更新到最近的值...只是哇... – Wil

回答

1

你有一個孤獨的NEXT沒有提取到你的while循環的頂部。

WHILE @@FETCH_STATUS = 0 
BEGIN 
    FETCH NEXT FROM @MyCursor 

它正在爲遊標中的每一行創建一個結果集。評論它,我想你會得到你想要的。

也因爲額外的提取,你只處理光標的每隔一行。如果這是你的意圖,那麼你將需要添加一個到獲取並推動thoes值到變量。

+0

謝謝,我盯着那整天都沒有見過。 – BillTetrault

相關問題