只有从LINQ执行时,存储过程才会失败

时间:2011-08-12 16:03:04

标签: linq stored-procedures

我有一个存储过程,可以向表中添加一些行,非常基本的东西。当我从SQL Server Management Studio中执行我的存储过程时,它返回正常。然而,当我从我的应用程序中运行它并通过LINQ到SP调用它似乎进入SP罚款,但SP内的进程失败。相同的参数看起来完全一样。

我知道这不是很多,但有没有人有任何想法可能导致这个?

这是我的存储过程:

set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
GO
ALTER procedure [dbo].[sp_PTS_Action_Hardware_Batch_Insert] (
@HdwString  Varchar(Max),
@UID        Varchar(15)
)
as
Declare     @Success_Nbr    Bit
Select      @Success_Nbr    = 0
Declare     @Success_Desc   Varchar(100)
Declare     @Stuff          Varchar(200)
Declare     @TLoc           BigInt
declare     @retV            int
--Declare   @RLoc           BigInt
Declare     @StartLoc       BigInt
Declare     @SLen           BigInt

Declare     @V  Varchar(25)
Declare     @P  Varchar(50)
Declare     @S  Varchar(50)
Declare     @L  Varchar(50)
Declare     @D  Varchar(200)
Declare     @F  Varchar(4)
Declare     @C  Varchar(50)
Declare     @M  Varchar(50)
Declare     @I  Varchar(20)
Declare     @Q  Varchar(10)
/*
Declare     @Req_Voucher_Number varchar(25),
Declare     @Part_Number        varchar(50),
Declare     @Serial_Number      varchar(50),
Declare     @Lot_Number         varchar(50),
Declare     @Description        varchar(200),       
Declare     @Federal_Class_Code varchar(4),
Declare     @Unit_Of_Issue      varchar(5),
Declare     @Item_Code_ID       int,
Declare     @Unit_Price         money,
Declare     @Qty                decimal(7, 2),
Declare     @UID                Varchar(15)
*/
Declare     @Item_Code_ID       int
Declare     @Unit_Price         money
Declare     @Qty                decimal(7, 2)

If IsNull(@HdwString,'') = ''
   BEGIN
    Select  @Success_Desc = 'No Data Provided.'
    Goto ExitProcedure
   END

If IsNull(CharIndex('\t',@HdwString,1),0)= 0
   BEGIN
    Select  @Success_Desc = 'No Delimeter Defined.'
    Goto ExitProcedure
   END
If IsNull(CharIndex('\r\n',@HdwString,1),0)= 0
   BEGIN
    Select  @Success_Desc = 'No Row End Defined.'
    Goto ExitProcedure
   END

EXEC sp_FIX_DoubleQuotesMax @HdwString OUTPUT
/*
DECLARE @DD1149_Hdw TABLE (
    [Req_Voucher_Number] [varchar](25),
    [Part_Number] [varchar](50),
    [Serial_Number] [varchar](50),
    [Lot_Number] [varchar](50),
    [Description] [varchar](200),
    [Federal_Class_Code] [varchar](4),
    [Item_Code_Desc] [varchar](50),
    [Unit_Price] [money] NOT NULL,
    [Unit_Of_Issue] [varchar](20)  NOT NULL,
    [Qty] [decimal](7, 2) NOT NULL
)
*/
Select  @StartLoc   = 1,
        @SLen       = Len(@HdwString),
        --@RLoc     = CharIndex('\r\n',@HdwString,1),
        @TLoc       = CharIndex('\t',@HdwString,1)

BEGIN TRANSACTION

Looper:
--End Of File?
IF @TLoc = 0
   BEGIN
    --Select 'This is the stub where the parsed date would then be pushed to the PTS_DD1149_Hardware table.'
    goto ProcessTransaction
    --Add Records To Hardware Table
/*
    IF @@ERROR != 0
       BEGIN
        ROLLBACK TRANSACTION
        Select  @Success_Desc = 'There was a problem adding the hardware.  No records were added.'
       END
    ELSE
       BEGIN
        COMMIT TRANSACTION
        Select  @Success_Desc = 'There was a problem adding the hardware.  No records were added.'
       END
    Goto ExitProcedure
*/
   END

ELSE
   BEGIN
    --ReqVoucher#
    Select  @V = Substring(@HdwString,@StartLoc,@TLoc-@StartLoc)
    Select  @StartLoc = @TLoc + 2
    --Part#
    Select  @TLoc = CharIndex('\t',@HdwString,@TLoc+1)
    Select  @P = Substring(@HdwString,@StartLoc,@TLoc-@StartLoc)
    Select  @StartLoc = @TLoc + 2
    --Ser#
    Select  @TLoc = CharIndex('\t',@HdwString,@TLoc+1)
    Select  @S = Substring(@HdwString,@StartLoc,@TLoc-@StartLoc)
    Select  @StartLoc = @TLoc + 2
    --Lot#
    Select  @TLoc = CharIndex('\t',@HdwString,@TLoc+1)
    Select  @L = Substring(@HdwString,@StartLoc,@TLoc-@StartLoc)
    Select  @StartLoc = @TLoc + 2
    --Desc
    Select  @TLoc = CharIndex('\t',@HdwString,@TLoc+1)
    Select  @D = Substring(@HdwString,@StartLoc,@TLoc-@StartLoc)
    Select  @StartLoc = @TLoc + 2
    --Fed Code
    Select  @TLoc = CharIndex('\t',@HdwString,@TLoc+1)
    Select  @F = Substring(@HdwString,@StartLoc,@TLoc-@StartLoc)
    Select  @StartLoc = @TLoc + 2
    --Item Code
    Select  @TLoc = CharIndex('\t',@HdwString,@TLoc+1)
    Select  @C = Substring(@HdwString,@StartLoc,@TLoc-@StartLoc)
    Select  @StartLoc = @TLoc + 2
    --Price
    Select  @TLoc = CharIndex('\t',@HdwString,@TLoc+1)
    Select  @M = Substring(@HdwString,@StartLoc,@TLoc-@StartLoc)
    Select  @StartLoc = @TLoc + 2
    --UOM
    Select  @TLoc = CharIndex('\t',@HdwString,@TLoc+1)
    Select  @I = Substring(@HdwString,@StartLoc,@TLoc-@StartLoc)
    Select  @StartLoc = @TLoc + 2
    --Qty
    Select  @TLoc = CharIndex('\r\n',@HdwString,@TLoc+1)
    Select  @Q = Substring(@HdwString,@StartLoc,@TLoc-@StartLoc)

    --SELECT    @V, @P, @S, @L, @D, @F, @C, @M, @I, @Q

    IF Not Exists(Select * From Unit_Of_Issue Where Unit_Of_Issue = @I)
        IF Not Exists(Select * From Unit_Of_Issue Where UI_Description = @I)
           BEGIN
            ROLLBACK TRANSACTION
            Goto ExitProcedure
           END
        ELSE
            Select  @I = Unit_Of_Issue From Unit_Of_Issue Where UI_Description = @I

    IF Not Exists(Select * From PTS_Item_Codes Where Item_Code = @C)
       BEGIN
        ROLLBACK TRANSACTION
        Goto ExitProcedure
       END
    ELSE
        Select  @Item_Code_ID = Item_Code_ID From PTS_Item_Codes Where Item_Code = @C

    IF @@ERROR != 0
       BEGIN
        ROLLBACK TRANSACTION
        Goto ExitProcedure
       END

    Select  @Unit_Price = Convert(Money,@M), @Qty = Convert(decimal(7,2),@Q)
    IF @@ERROR != 0
       BEGIN
        ROLLBACK TRANSACTION
        Goto ExitProcedure
       END

    --Select    5, @V, @P, @S, @L, @D, @F, @M, @Item_Code_ID, @Unit_Price, @Qty, @UID
    exec @retV = sp_PTS_Action_DD1149_Insert_Hardware_No_RecSet @V, @P, @S, @L, @D, @F, @I, @Item_Code_ID, @Unit_Price, @Qty, @UID
    IF @retV != 1
       BEGIN
        ROLLBACK TRANSACTION
        Goto ExitProcedure
       END
    --Add To Table
    --INSERT    @DD1149_Hdw
    --SELECT    @V, @P, @S, @L, @D, @F, @C, @M, @I, @Q

    --Testing
    --Select * from @DD1149_Hdw

    --Select    @V, @P, @S, @L, @D, @F, @C, @M, @I, @Q

    --Reset Variables
    Select  @StartLoc = @TLoc + 4,
            --@RLoc = CharIndex('\r\n',@HdwString,@TLoc+3),
            @TLoc       = CharIndex('\t',@HdwString,@TLoc+3),
            @V          = NULL,
            @P          = NULL,
            @S          = NULL,
            @L          = NULL,
            @D          = NULL,
            @F          = NULL,
            @C          = NULL,
            @M          = NULL,
            @I          = NULL,
            @Q          = NULL,
            @Unit_Price = NULL,
            @Qty        = NULL,
            @retV       = NULL

   END

Goto Looper

ProcessTransaction:
    COMMIT TRANSACTION

    Select  @Success_Nbr = 1

ExitProcedure:

If @Success_Nbr = 0
    Select Convert(Varchar(7),'Failed') as Success_Desc
Else
    Select Convert(Varchar(7),'Success') as Success_Desc

--Select    @Success_Nbr as Success_Nbr
/*
Select  @Success_Nbr as Success_Nbr,
        @Success_Desc as Success_Desc,
        *
From    @DD1149_Hdw
*/

1 个答案:

答案 0 :(得分:0)

Linq在评估查询时可能很懒惰。你绝对确定你的插入是按照你正在尝试的顺序发生的。给出外键错误,我的猜测是否定的。