0

I create this function to calculate shortage material quantity

ALTER FUNCTION [dbo].[ReturnShortageByItemCodeLinePackage]
    (@lineId int, @testpackId int, @MaterialDescriptionId int)
RETURNS float
AS BEGIN
    DECLARE @shortageQuantity float
    DECLARE @MIVQuantity float
    DECLARE @totalQuantity float
    DECLARE @spoolQuantity float
    DECLARE @ExistInSiteQuantity float
    DECLARE @BeforeDoneQuantity float

    SELECT  
        @totalQuantity = Quantity,
        @spoolQuantity = QuantitySpool,
        @ExistInSiteQuantity = QuantityExistInSite, 
        @BeforeDoneQuantity = QuantityBeforeDone
    FROM 
        [SPMS2].[dbo].Materials 
    WHERE
        LineId = @lineId 
        AND TestPackageId = @testpackId  
        AND MaterialDescriptionId = @MaterialDescriptionId

    SELECT 
        @MIVQuantity = SUM(QuantityDeliver) 
    FROM
        MaterialIssueVoucherDetails miv
    JOIN 
        MaterialRequestContractorDetails mrc ON miv.MaterialRequestContractorDetailId = mrc.Id
    WHERE
        TestPackageId = @testpackId 
        AND LineId = @lineId 
        AND miv.MaterialDescriptionId = @MaterialDescriptionId

    IF @MIVQuantity IS NULL
    BEGIN
        SET @MIVQuantity = 0    
    END

    SET @shortageQuantity = @totalQuantity - (@BeforeDoneQuantity + @ExistInSiteQuantity + @spoolQuantity + @MIVQuantity)

    RETURN round(@shortageQuantity,3)
END

I use this function inside my stored procedure as you can see here:

ALTER  PROCEDURE [dbo].[SPViewMTO] 
AS
BEGIN
    SELECT        
        dbo.Lines.Unit, dbo.Lines.LineNumber, dbo.Lines.DocumentNumber, 
        dbo.BaseMaterials.Name AS MaterialName, 
        dbo.MaterialDescriptions.Name AS MaterialDescription, 
        dbo.MaterialDescriptions.Description, dbo.MaterialScopes.ScopeName, 
        dbo.MaterialScopeObjectNames.ObjectName, 
        dbo.MaterialDescriptions.Size1, dbo.MaterialDescriptions.Size2, 
        dbo.MaterialDescriptions.ItemCode, 
        dbo.Materials.Quantity, dbo.Materials.Discipline, dbo.Materials.Id, 
        dbo.Lines.Id AS LineId, dbo.Materials.QuantitySpool,  
        dbo.Materials.QuantityExistInSite, dbo.Materials.QuantityBeforeDone, 
        dbo.TestPackages.PackageNumber, dbo.Materials.TestPackageId,
        ISNULL(dbo.ReturnShortageByItemCodeLinePackage(Lines.Id, TestPackageId, MaterialDescriptionId), 0) AS Shortage
    FROM            
        dbo.Materials 
    INNER JOIN
        dbo.Lines ON dbo.Materials.LineId = dbo.Lines.Id 
    INNER JOIN
        dbo.BaseMaterials ON dbo.Lines.BaseMaterialId = dbo.BaseMaterials.Id 
    INNER JOIN
        dbo.MaterialDescriptions ON dbo.Materials.MaterialDescriptionId = dbo.MaterialDescriptions.Id 
    INNER JOIN
        dbo.MaterialScopes ON dbo.MaterialDescriptions.MaterialScopeId = dbo.MaterialScopes.Id 
    INNER JOIN
        dbo.MaterialScopeObjectNames ON dbo.MaterialDescriptions.MaterialScopeObjectId = dbo.MaterialScopeObjectNames.Id 
    INNER JOIN
        dbo.TestPackages ON dbo.Materials.TestPackageId = dbo.TestPackages.Id
END

The time execution of the stored procedure is about 47 seconds inside my SQL Server Management Studio directly, but when I call the stored procedure` from my C# application I get this error:

timeout expired the timeout period elapsed SQL Server 2012

Here is my code to call the stored procedure:

lst = _ctx.Database.SqlQuery<ViewDomainClass.MaterialOffice.DAViewMTO>
                      ("EXEC [dbo].SPViewMTO").ToList();
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Ehsan Akbar
  • 6,977
  • 19
  • 96
  • 180
  • 1
    Try to change CommandTimeout property. See http://stackoverflow.com/questions/6232633/entity-framework-timeouts If it helps, I'll post it as answer. – Anton Sep 08 '16 at 05:43
  • @Anton thanks i just remove timeout property in connection string and add it to context like this :this.context.Database.CommandTimeout = 180; – Ehsan Akbar Sep 08 '16 at 06:23

2 Answers2

0

Timeout is a property of connection string in ADO.Net and other Frameworks like LINQ & EF. Try to change timeout in your connection string as you want. Also you can set time out on SQLCommand to override timeout property.

Imran Sh
  • 1,623
  • 4
  • 27
  • 50
0

You use (NOLOCK) you can increase performance your query.

ALTER  PROCEDURE [dbo].[SPViewMTO] 
AS
BEGIN


 SELECT        dbo.Lines.Unit, dbo.Lines.LineNumber, dbo.Lines.DocumentNumber, dbo.BaseMaterials.Name AS MaterialName, 
                             dbo.MaterialDescriptions.Name AS MaterialDescription, dbo.MaterialDescriptions.Description, dbo.MaterialScopes.ScopeName, 
                             dbo.MaterialScopeObjectNames.ObjectName, dbo.MaterialDescriptions.Size1, dbo.MaterialDescriptions.Size2, dbo.MaterialDescriptions.ItemCode, 
                             dbo.Materials.Quantity, dbo.Materials.Discipline, dbo.Materials.Id, dbo.Lines.Id AS LineId, dbo.Materials.QuantitySpool, dbo.Materials.QuantityExistInSite, 
                             dbo.Materials.QuantityBeforeDone, dbo.TestPackages.PackageNumber, dbo.Materials.TestPackageId,isnull(dbo.ReturnShortageByItemCodeLinePackage(Lines.Id,TestPackageId,MaterialDescriptionId),0) As Shortage
    FROM            dbo.Materials(NOLOCK) INNER JOIN
                             dbo.Lines(NOLOCK) ON dbo.Materials.LineId = dbo.Lines.Id INNER JOIN
                             dbo.BaseMaterials(NOLOCK) ON dbo.Lines.BaseMaterialId = dbo.BaseMaterials.Id INNER JOIN
                             dbo.MaterialDescriptions(NOLOCK) ON dbo.Materials.MaterialDescriptionId = dbo.MaterialDescriptions.Id INNER JOIN
                             dbo.MaterialScopes(NOLOCK) ON dbo.MaterialDescriptions.MaterialScopeId = dbo.MaterialScopes.Id INNER JOIN
                             dbo.MaterialScopeObjectNames(NOLOCK) ON dbo.MaterialDescriptions.MaterialScopeObjectId = dbo.MaterialScopeObjectNames.Id INNER JOIN
                             dbo.TestPackages(NOLOCK) ON dbo.Materials.TestPackageId = dbo.TestPackages.Id

    EnD
Huseyin Durmus
  • 380
  • 6
  • 14