如果存在,则更新else插入在SQL Server企业版中无法正常工作

时间:2012-04-03 10:42:18

标签: sql-server vb.net sql-server-2000

我知道这可能是一个重复的问题,但现在真的让我感到沮丧。我必须插入/更新发货人信息,我写了一个这样的存储过程:

IF EXISTS (SELECT * FROM sysobjects WHERE name='usp_Nucleus_LSS_AddShipperInfo' AND type='P')
    DROP PROCEDURE usp_Nucleus_LSS_AddShipperInfo 
GO 

CREATE PROCEDURE dbo.usp_Nucleus_LSS_AddShipperInfo 
(
    @ShipperCode varchar(500),
    @ShipperName varchar(500),
    @CustomsClearance decimal,
    @MinCommissionAmount decimal,
    @CommissionPercent decimal,
    @TruckingAmount decimal,
    @ContractNo varchar(50),
    @ConsigneeName varchar(500),
    @SalesPerson varchar(500),
    @Email varchar(500),
    @ContactNo varchar(500),
    @ShipperPriority varchar(50),
    @EntryBy varchar(50),
    @EntryDate datetime,
    @UpdatedBy varchar(50),
    @UpdateDate datetime
)
AS 
DECLARE
    @NewShipperID bigint,
    @OldShipperID bigint 
BEGIN 
    SELECT @OldShipperID = (SELECT ISNULL(ShipperID, 0) AS ShipperID FROM LSS_t_ShipperInfo WHERE ShipperCode = @ShipperCode)
    SELECT @NewShipperID = (SELECT ISNULL(MAX(ShipperID),0) AS ShipperID FROM LSS_t_ShipperInfo)
    IF (@OldShipperID > 0)
        BEGIN 
            UPDATE LSS_t_ShipperInfo SET ShipperName=@ShipperName, CustomsClearance=@CustomsClearance, 
            MinCommissionAmount=@MinCommissionAmount, CommissionPercent=@CommissionPercent, TruckingAmount=@TruckingAmount, 
            ContractNo=@ContractNo, ConsigneeName=@ConsigneeName, SalesPerson=@SalesPerson, Email=@Email, ContactNo=@ContactNo, 
            ShipperPriority=@ShipperPriority, UpdatedBy=@UpdatedBy, UpdateDate=@UpdateDate 
            WHERE ShipperID = @OldShipperID 
        END 
    ELSE
        BEGIN
            INSERT INTO LSS_t_ShipperInfo (ShipperID, ShipperCode, ShipperName, CustomsClearance, MinCommissionAmount, 
            CommissionPercent, TruckingAmount, ContractNo, ConsigneeName, SalesPerson, Email, ContactNo, ShipperPriority, EntryBy, 
            EntryDate, UpdatedBy, UpdateDate) VALUES (@NewShipperID+1, @ShipperCode, @ShipperName, @CustomsClearance, @MinCommissionAmount, 
            @CommissionPercent, @TruckingAmount, @ContractNo, @ConsigneeName, @SalesPerson, @Email, @ContactNo, @ShipperPriority, @EntryBy, 
            @EntryDate, @UpdatedBy, @UpdateDate)
        END
END 

GO 

我使用单行从我的应用程序执行此操作并且它有效。但下次当我再次使用两行运行它时,它既不插入也不更新记录。我不明白这里有什么问题。我分别运行每个查询,它们工作正常。这是我的VB.Net代码:

oDBCmd.Connection = oDBCon
                oDBCmd.CommandText = "usp_Nucleus_LSS_AddShipperInfo"
                oDBCmd.CommandType = CommandType.StoredProcedure
                oDBCmd.CommandTimeout = 0
                nResult = -1

                oDBCmd.Parameters.Add("@ShipperCode", OleDbType.VarChar).Value = DGMAIN.Rows(i).Cells("DGMAIN_SHIPPER_CODE").Value.ToString()
                oDBCmd.Parameters.Add("@ShipperName", OleDbType.VarChar).Value = DGMAIN.Rows(i).Cells("DGMAIN_SHIPPER_NAME").Value.ToString()
                oDBCmd.Parameters.Add("@CustomsClearance", OleDbType.Decimal).Value = DGMAIN.Rows(i).Cells("DGMAIN_CUSTOMS_CLEARANCE").Value.ToString()
                oDBCmd.Parameters.Add("@MinCommissionAmount", OleDbType.Decimal).Value = DGMAIN.Rows(i).Cells("DGMAIN_MIN_COMMISSION_AMOUNT").Value.ToString()
                oDBCmd.Parameters.Add("@CommissionPercent", OleDbType.Decimal).Value = DGMAIN.Rows(i).Cells("DGMAIN_COMMISSION_PERCENT").Value.ToString()
                oDBCmd.Parameters.Add("@TruckingAmount", OleDbType.Decimal).Value = DGMAIN.Rows(i).Cells("DGMAIN_TRUCKING_AMOUNT").Value.ToString()
                oDBCmd.Parameters.Add("@ContractNo", OleDbType.VarChar).Value = DGMAIN.Rows(i).Cells("DGMAIN_CONTRACT_NO").Value.ToString()
                oDBCmd.Parameters.Add("@ConsigneeName", OleDbType.VarChar).Value = DGMAIN.Rows(i).Cells("DGMAIN_CONSIGNEE_NAME").Value.ToString()
                oDBCmd.Parameters.Add("@SalesPerson", OleDbType.VarChar).Value = DGMAIN.Rows(i).Cells("DGMAIN_SALES_PERSON").Value.ToString()
                oDBCmd.Parameters.Add("@Email", OleDbType.VarChar).Value = DGMAIN.Rows(i).Cells("DGMAIN_EMAIL").Value.ToString()
                oDBCmd.Parameters.Add("@ContactNo", OleDbType.VarChar).Value = DGMAIN.Rows(i).Cells("DGMAIN_CONTACT_NO").Value.ToString()
                oDBCmd.Parameters.Add("@ShipperPriority", OleDbType.VarChar).Value = DGMAIN.Rows(i).Cells("DGMAIN_SHIPPER_PRIORITY").Value.ToString()
                oDBCmd.Parameters.Add("@EntryBy", OleDbType.VarChar).Value = System.Environment.UserName.ToString().ToUpper()
                oDBCmd.Parameters.Add("@EntryDate", OleDbType.Date).Value = System.DateTime.Now().ToString()
                oDBCmd.Parameters.Add("@UpdatedBy", OleDbType.VarChar).Value = System.Environment.UserName.ToString().ToUpper()
                oDBCmd.Parameters.Add("@UpdateDate", OleDbType.Date).Value = System.DateTime.Now().ToString()

                nResult = oDBCmd.ExecuteNonQuery()

我使用的是VB.Net 2.0和SQL Server企业版。

提前致谢。

1 个答案:

答案 0 :(得分:2)

我查看了你的存储过程,看起来很好。

我认为您的vb.net代码存在问题。我怀疑你是否缺少执行多行数据表的for循环。

请在此处试用此代码:

    For i As Integer = 0 To DGMAIN.Rows.Count - 1
        oDBCmd = New SqlCommand
        oDBCmd.Connection = oDBCon
        oDBCmd.CommandText = "usp_Nucleus_LSS_AddShipperInfo"
        oDBCmd.CommandType = CommandType.StoredProcedure
        oDBCmd.CommandTimeout = 0
        nResult = -1

        oDBCmd.Parameters.Add("@ShipperCode", OleDbType.VarChar).Value = DGMAIN.Rows(i)("DGMAIN_SHIPPER_CODE").Value.ToString()
        oDBCmd.Parameters.Add("@ShipperName", OleDbType.VarChar).Value = DGMAIN.Rows(i)("DGMAIN_SHIPPER_NAME").Value.ToString()
        oDBCmd.Parameters.Add("@CustomsClearance", OleDbType.Decimal).Value = DGMAIN.Rows(i)("DGMAIN_CUSTOMS_CLEARANCE").Value.ToString()
        oDBCmd.Parameters.Add("@MinCommissionAmount", OleDbType.Decimal).Value = DGMAIN.Rows(i)("DGMAIN_MIN_COMMISSION_AMOUNT").Value.ToString()
        oDBCmd.Parameters.Add("@CommissionPercent", OleDbType.Decimal).Value = DGMAIN.Rows(i)("DGMAIN_COMMISSION_PERCENT").Value.ToString()
        oDBCmd.Parameters.Add("@TruckingAmount", OleDbType.Decimal).Value = DGMAIN.Rows(i)("DGMAIN_TRUCKING_AMOUNT").Value.ToString()
        oDBCmd.Parameters.Add("@ContractNo", OleDbType.VarChar).Value = DGMAIN.Rows(i)("DGMAIN_CONTRACT_NO").Value.ToString()
        oDBCmd.Parameters.Add("@ConsigneeName", OleDbType.VarChar).Value = DGMAIN.Rows(i)("DGMAIN_CONSIGNEE_NAME").Value.ToString()
        oDBCmd.Parameters.Add("@SalesPerson", OleDbType.VarChar).Value = DGMAIN.Rows(i)("DGMAIN_SALES_PERSON").Value.ToString()
        oDBCmd.Parameters.Add("@Email", OleDbType.VarChar).Value = DGMAIN.Rows(i)("DGMAIN_EMAIL").Value.ToString()
        oDBCmd.Parameters.Add("@ContactNo", OleDbType.VarChar).Value = DGMAIN.Rows(i)("DGMAIN_CONTACT_NO").Value.ToString()
        oDBCmd.Parameters.Add("@ShipperPriority", OleDbType.VarChar).Value = DGMAIN.Rows(i)("DGMAIN_SHIPPER_PRIORITY").Value.ToString()
        oDBCmd.Parameters.Add("@EntryBy", OleDbType.VarChar).Value = System.Environment.UserName.ToString().ToUpper()
        oDBCmd.Parameters.Add("@EntryDate", OleDbType.Date).Value = System.DateTime.Now().ToString()
        oDBCmd.Parameters.Add("@UpdatedBy", OleDbType.VarChar).Value = System.Environment.UserName.ToString().ToUpper()
        oDBCmd.Parameters.Add("@UpdateDate", OleDbType.Date).Value = System.DateTime.Now().ToString()

        nResult = oDBCmd.ExecuteNonQuery()

    Next

希望这会对你有所帮助