Unique or something similar to 3 or more different tables

Asked

Viewed 41 times

0

Sirs,

In the company where I work uses an ERP that I do not know for what reason, is carried out the typing of sales notes, return of sales and returns of purchases in three different forms, and the same activity could be performed in the same form, finally, the nf number field, is equal to the three forms and cannot be repeated, if in the sales form you used nf number 525, the next entry will be 526 in any of the three forms, for this the ERP company used a function that brings the last number used, adds + 1 and finds the next one to be used. Who has a little knowledge, will understand that this is a hell of a gambiarra and that at some point one of the three forms can get the same number, if the typing occurs at the same time. Now, how do we get around that? I already contacted the company about 3 months ago regarding this problem, several charges and I still have the problem here at the company.

Function used:

CREATE FUNCTION [dbo].[NfNumero]

(
    @NfEspecie VARCHAR(3),
    @NfSerie VARCHAR(3)
)

RETURNS NUMERIC(15)

AS

BEGIN

DECLARE @NfNumero AS NUMERIC(15)

SELECT @NfNumero = MAX(A.NfNumero)
  FROM ( 

  SELECT ISNULL(MAX(NfNumero),0) AS NfNumero 
    FROM Venda WITH(NOLOCK)
   WHERE NfEspecie  = @NfEspecie
     AND NfSerie   = @NfSerie

   UNION ALL

  SELECT ISNULL(MAX(NfNumero),0) NfNumero 
    FROM DevolucoesVendas  WITH(NOLOCK)
   WHERE NfEspecie = @NfEspecie
     AND NfSerie   = @NfSerie

   UNION ALL

  SELECT ISNULL(MAX(NfNumero),0) NfNumero  
    FROM DevolucoesCompras WITH(NOLOCK)
   WHERE NfEspecie = @NfEspecie 
     AND Empresa   = @Empresa 
     AND NfSerie   = @NfSerie   ) AS A

RETURN @NfNumero + 1

END
  • The problem has already occurred ? You need to see the three (at least) calls to see if they are correct (without going into details) when buying an ERP and why you don’t want to worry about these things...

  • The call is simple, when the note "is born", the number field is 0 (zero), at the time the record is recorded, an update is made taking the return of this function and saving in the nf number field. The three forms are the same.

  • Okay, if you don’t have a lock and transactions you might even have a problem, but you’re an Erp client? Can you change the code? Have you ever had a duplicate key? Your quarrel is with the supplier.

  • Yes, I can change some snippets that use SQL Server, like functions and procedures, as well as some queries. The question of the supplier, however, is another story that better not go into detail, because it is disappointing.

  • I know what this is , ever thought of a validation Rigger , would ensure uniqueness , see if it violates the contract.

  • I’ve thought of Rigger, yes, but as the last alternative.

  • ... but , the problem has already occurred ?!

  • Yes, it occurs every week, at least 15 cases, depends on the flow of notes generated simultaneously.

  • 1

    Maybe (I don’t know sql server in depth) not using WITH(NOLOCK), the danger would be to have http://sqlserverplanet.com/tsql/using-with-nolock http://sqlserverplanet.com/optimization/set-transaction-isolation-isolation-level but kind of thing who buys an Erp shouldn’t be worried ...

Show 4 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.