SQL 服务器:重复值

SQL Server : duplicates value

我正在更新我的项目现在问题是有时票号在更新后重复此代码有什么问题?

或者最优雅的解决方案是什么

我的密码是

DECLARE @tbl_ID TABLE (nID int)
UPDATE tbl_Tickets
SET Ticket_Type = @Ticket_Type,
VehicleNo= @VehicleNo,
Customer=@Customer,
Job_Code=@Job_Code,
Material=@Material,
First_Weight=@First_Weight,
Second_Weight=@Second_Weight,
Net_Weight=@Net_Weight,
Add_Charges=@Add_Charges,
Deduction=@Deduction,
Ticket_Amount=@Ticket_Amount,
SOURCE=@Source,
       Destination=@Destination,
OPERATOR=@Operator,
     Out_Time=@Out_Time,
     PC=@PC,
     Unit_Price=@Unit_Price OUTPUT INSERTED.QS_Code INTO @tbl_ID (nID)
 WHERE (VehicleNo=@VehicleNo)
 AND (Second_Weight IS NULL)
 AND (Ticket_Type <>'DELETED') IF @@ROWCOUNT=0 BEGIN

INSERT INTO tbl_Tickets (Ticket_Type, TicketNo, VehicleNo, nDate, Customer,Job_Code,Material,     First_Weight, Second_Weight, Net_Weight, Unit_Price, Add_Charges, Deduction, Ticket_Amount,SOURCE, Destination,
                       OPERATOR, In_Time, PC)
VALUES (@Ticket_Type,
      @TicketNo,
      @VehicleNo,
      @nDate,
      @Customer,
      @Job_Code,
      @Material,
      @First_Weight,
      @Second_Weight,
      @Net_Weight,
      @Unit_Price,
      @Add_Charges,
      @Deduction,
      @Ticket_Amount,
      @Source,
      @Destination,
      @Operator,
      @In_Time,
      @PC)
 SELECT IDENT_CURRENT('tbl_Tickets')
 FROM tbl_Tickets
 UPDATE tbl_TicketNumber
 SET TicketNo = @TTicket,
  PC= @TPC,
  nDate= @TnDate WHERE (PC=@TPC) IF @@ROWCOUNT=0 BEGIN
 INSERT INTO tbl_TicketNumber (TicketNo, PC, nDate)
 VALUES (@TTicket,
      @TPC,
      @TnDate) END END
SELECT *
FROM @tbl_ID

那我改成

cm.CommandText = " DECLARE @tbl_ID TABLE (nID int) UPDATE tbl_Tickets " & _
        " SET Ticket_Type = @Ticket_Type, TicketNo = @TicketNo, VehicleNo= @VehicleNo," & _
        " nDate=@nDate, Customer=@Customer, Job_Code=@Job_Code, Material=@Material, " & _
        " First_Weight=@First_Weight, Second_Weight=@Second_Weight, Net_Weight=@Net_Weight, " & _
        " Add_Charges=@Add_Charges, Deduction=@Deduction, Ticket_Amount=@Ticket_Amount, " & _
        " Source=@Source, Destination=@Destination, Operator=@Operator, Out_Time=@Out_Time, PC=@PC, Payment=@Payment,Unit_Price=@Unit_Price " & _
        EditString & _
        " OUTPUT INSERTED.QS_Code INTO @tbl_ID (nID)  WHERE (VehicleNo=@VehicleNo) AND (Second_Weight IS NULL) AND  (Ticket_Type <>'Weighbridge_VOID') AND  (Ticket_Type <>'Cash_VOID') " & _
       " IF @@ROWCOUNT=0  " & _
        " BEGIN " & _
            " INSERT INTO tbl_Tickets (Ticket_Type, TicketNo, VehicleNo, nDate, Customer,Job_Code,Material, " & _
            " First_Weight, Second_Weight, Net_Weight, Unit_Price, Add_Charges, Deduction, Ticket_Amount,Source, " & _
            " Destination, Operator, In_Time, PC, Payment " & _
            AddString & " ) VALUES (@Ticket_Type, '00-00', @VehicleNo, @nDate, @Customer,@Job_Code,@Material, " & _
            " @First_Weight, @Second_Weight, @Net_Weight, @Unit_Price, @Add_Charges, @Deduction, @Ticket_Amount,@Source, " & _
            " @Destination, @Operator, @In_Time, @PC, @Payment  " & _
            AddStringVal & " ) SELECT IDENT_CURRENT('tbl_Tickets') FROM tbl_Tickets " & _
           " END SELECT * From @tbl_ID" '

并像这样在更新后添加 TicketNumberUpdate 子

cm.CommandText = " UPDATE tbl_TicketNumber " & _
            " SET TicketNo = @TTicket, PC= @TPC, nDate= @TnDate" & _
            " WHERE (PC=@TPC) " & _
           " IF @@ROWCOUNT=0 " & _
           " INSERT INTO tbl_TicketNumber (TicketNo, PC, nDate) VALUES (@TTicket,@TPC,@TnDate) "

您似乎忽略了 IDENT_CURRENT 的值 尝试更改

SELECT IDENT_CURRENT('tbl_Tickets')
FROM tbl_Tickets

UPDATE tbl_TicketNumber SET
    TicketNo = @TTicket,
    PC= @TPC,
    nDate= @TnDate
WHERE (PC=@TPC) 

至此

SET @TTicket = IDENT_CURRENT('tbl_Tickets')

UPDATE tbl_TicketNumber SET
    TicketNo = @TTicket,
    PC= @TPC,
    nDate= @TnDate
WHERE (PC=@TPC)