2011-02-25 82 views
1

我想在LINQ中執行存儲過程。在SQL Server中運行時,它會返回幾個字段,大多數是整數字段和浮點字段。但是,當我調用LINQ存儲過程中,所有的返回值爲0。這裏是我正在調用存儲過程:linq和存儲過程

var meterReadings = _database.MetricDataReport("20101127"); 

這裏是存儲過程:

ALTER PROCEDURE [dbo].[MetricDataReport] 
@FormDate VARCHAR(8) 
AS 
SET NOCOUNT ON 



/*Table To Hold Finished RecordSet*/ 
CREATE TABLE #OutputTable 
(
    PkMetricMeterID INT, 
    FkSiteID  INT, 
    FourWeekReading FLOAT, 
    LastWeekReading FLOAT, 
    ThisWeekReading FLOAT, 
    pkMeterID  INT, 
    DecimalPlaces INT 

) 

CREATE TABLE #WorkingTable 
(
    PkMetricMeterID INT, 
    FkSiteID  INT, 
    FormDate  DATETIME, 
    Reading   FLOAT, 
    pkMeterID  INT, 
    DecimalPlaces INT 
) 

----------------------------------- 
--- Process Physical Meters --- 
----------------------------------- 
    INSERT INTO #WorkingTable (PkMetricMeterID,FkSiteID,FormDate,Reading,pkMeterID,DecimalPlaces) 
    SELECT 
     luMetricMeters.PkMetricMeterID, 
     LuMetricMeterList.FkSiteID, 
     GetReadingsAndPriceLite.FormDate, 
     GetReadingsAndPriceLite.CalculatedConsumption * GetReadingsAndPriceLite.ConversionFactor, 
     luMetricMeterList.pkMeterID,    
     Meters.DecimalPlaces  
    FROM 
     luMetricMeters 
    INNER JOIN 
     luMetricMeterList ON luMetricMeterList.PkMetricMeterID = luMetricMeters.PkMetricMeterID 
    INNER JOIN 
     GetReadingsAndPriceLite ON GetReadingsAndPriceLite.pkMeterID = luMetricMeterList.pkMeterID 
    INNER JOIN 
     Meters ON Meters.PkMeterID = luMetricMeterList.pkMeterID 
    WHERE 
     GetReadingsAndPriceLite.FormDate <= @FormDate AND GetReadingsAndPriceLite.FormDate >= DATEADD(wk,-4,@FormDate) 
-------------------------------------------- 
--- Process Virtual Meters (NON KPI) --- 
-------------------------------------------- 

---Check To See If Meter Needs Updating 
DECLARE @CurrentMeterID INT 
DECLARE MeterCursor Cursor FAST_FORWARD FOR 
    SELECT 
     luMetricMeterList.pkMeterID 
    FROM 
     LuMetricMeters 
    INNER JOIN 
     luMetricMeterList ON luMetricMeterList.PkMetricMeterID = luMetricMeters.PkMetricMeterID 
    INNER JOIN 
     VirtualMeterReadings ON VirtualMeterReadings.MeterID = luMetricMeterList.pkMeterID 
    WHERE 
     VirtualMeterReadings.UpdateFlag = 1 
OPEN MeterCursor 
FETCH NEXT FROM MeterCursor INTO @CurrentMeterID 
WHILE @@Fetch_Status=0 
BEGIN 
    EXEC UpdateVirtualReadings @CurrentMeterID 
    FETCH NEXT FROM MeterCursor INTO @CurrentMeterID 
END 
CLOSE MeterCursor 
DEALLOCATE MeterCursor 


INSERT INTO #WorkingTable (PkMetricMeterID,FkSiteID,FormDate,Reading,pkMeterID,DecimalPlaces) 
    SELECT 
     luMetricMeters.PkMetricMeterID, 
     LuMetricMeterList.FkSiteID, 
     VirtualMeterReadings.FormDate, 
     VirtualMeterReadings.Reading, 
     LuMetricMeterList.pkMeterID, 
     Meters.DecimalPlaces    
    FROM 
     luMetricMeters 
    INNER JOIN 
     luMetricMeterList ON luMetricMeterList.PkMetricMeterID = luMetricMeters.PkMetricMeterID 
    INNER JOIN 
     VirtualMeterReadings ON VirtualMeterReadings.MeterID = luMetricMeterList.pkMeterID 
    INNER JOIN 
     Meters ON Meters.PkMeterID = luMetricMeterList.pkMeterID 
    WHERE 
     VirtualMeterReadings.FormDate <= @FormDate AND VirtualMeterReadings.FormDate >= DATEADD(wk,-4,@FormDate) 

SET NOCOUNT OFF 

SELECT DISTINCT 
    [#WorkingTable].PkMetricMeterID, 
    #WorkingTable.FkSiteID, 
    ThisWeekReading = (SELECT TOP 1 Reading FROM #WorkingTable WorkingTable1 WHERE WorkingTable1.PkMetricMeterID = #WorkingTable.PkMetricMeterID AND [#WorkingTable].FkSiteID = [WorkingTable1].FkSiteID ANd workingTable1.FORMDATE = @FormDate), 
    LastWeekReading = (SELECT TOP 1 Reading FROM #WorkingTable WorkingTable1 WHERE WorkingTable1.PkMetricMeterID = #WorkingTable.PkMetricMeterID AND [#WorkingTable].FkSiteID = [WorkingTable1].FkSiteID ANd workingTable1.FORMDATE = DATEADD(d,-7,@FormDate)), 
    FourWeekReading = (SELECT AVG(Reading) FROM #WorkingTable WorkingTable1 WHERE WorkingTable1.PkMetricMeterID = #WorkingTable.PkMetricMeterID AND [#WorkingTable].FkSiteID = [WorkingTable1].FkSiteID), 
    #WorkingTable.pkMeterID, 
    #WorkingTable.DecimalPlaces 
FROM 
    [#WorkingTable] 
ORDER BY  
    [#WorkingTable].PkMetricMeterID, 
    [#WorkingTable].FkSiteID 

回答

0

我不對此足夠了解,看看問題出在哪裏,但是你可能想要使用一個更簡單的存儲過程首先得到這個工作。例如只返回一個簡單列表的東西。

此外,看看ScottGu's blog post使用存儲過程與Linq到Sql。