Filter numbers not yet registered via SQL

Asked

Viewed 504 times

2

I’m trying to find the numbers that haven’t been entered in the bank yet.

I have a client table, in it I have the fields codigo and nome.

When the user registers the client, he puts the code manually, so he risks skipping some numbers.

Is there any way SQL for me to try to select these numbers that have not yet been registered?

  • Some reason not to use a primary incremental key as code ?

  • The "code" column is of the whole type?

  • It is of type integer yes and primary key. It is that in fact the user determines the number to register

  • @Emerson there is the danger. He really needs to determine the number? Why ? I don’t see a good reason to allow this.

  • So @gmsantos, I found a different way, a system that draws more attention. Because I want to put aside the number that hasn’t been registered yet!

  • What you want is possible with SEQUENCE. You can predict what the next code will be =)

Show 1 more comment

4 answers

1

It would even be simple to do that:

Do a search in the same table before adding a new record and take the return and add the code in an array and then add compare if the code entered by the user is within this array.

It’s a very simple way to do it.

  • Thanks Maicon. But you have an example of what this array would look like?

  • It depends on the programming language you use, there are several ways to do this but consider actually using an incremental field for your and your customer’s security.

  • OK Maicon, I’ll try here. But it was as I said, I would like this system to be a little different, out of the pattern...

  • 1

    But trust me, man, this pattern is gonna end up giving you a headache and your client with this method is much easier to give index problem later and get lost when using a FK for example, it’s an unnecessary control you have to do, leave everything to the bank because it was made for this but if you want to do as this doing continue quietly but taking great care of how you will implement.

  • 1

    Thank you very much for the Maicon tip, but I will try to do yes.. I will take the utmost care =)

1


Emerson,

To verify which codes were not registered in the range, you can execute the query below by changing the table name.

DECLARE @CONTADOR INTEGER
DECLARE @TT INTEGER

CREATE TABLE #CODIGO (
CODIGO INTEGER )

SET @CONTADOR = 1


WHILE ((SELECT MAX(CODIGO) FROM TABELA) >= @CONTADOR)
BEGIN
    SET @TT = (SELECT COUNT(CODIGO) FROM TABELA WHERE CODIGO = @CONTADOR)

IF @TT = 0
BEGIN
INSERT INTO #CODIGO VALUES (@CONTADOR)
END

SET @CONTADOR = @CONTADOR + 1

END

SELECT * FROM #CODIGO
DROP TABLE #CODIGO
  • We’re talking about users. What if some user sets the maximum integer value as code?

  • 1

    Jean, actually I don’t need to always take the last number and add 1 more. I need to check if there is any number that was skipped at the time of registering!

  • Ah yes, I will prepare the query for you! I already edit.

  • Emerson, I edited it.

  • @Jeangustavoprates, this giving in this query. I don’t know much in bd... I’m using Postgresql and it seems that the syntax changes a lot neh?

1

If you are using one SEQUENCE it is possible to predict what the next codigo to be inserted.

CREATE SEQUENCE codigoUsuario;

To select the code:

SELECT nextval('codigoUsuario');

When creating your table you can set the SEQUENCE directly in the field:

CREATE TABLE tabela ( codigo INTEGER DEFAULT nextval('codigoUsuario') );

I don’t think it’s a good idea to leave code control in the hands of the user.

Need to check if there is already a certain new code at the time of creation will only create more complexity in the application, which without a really feasible use case is simply unnecessary.

  • So, man, I don’t really get it. This nextval is what it’s for?

  • It checks which number will be next to be entered. Example: You make a SELECT nextval('codigoUsuario'); and returns 5. Next you make an Insert that involves this Quence. The next time you make a SELECT nextval('codigoUsuario') will return 6.

  • It gives this error: relation "per_code" does not exist. Has ave with the sequence ne?

  • Yes, you need to create the SEQUENCE in the database before. Read the documentation in the link. It is like an incremental key outside the table.

  • No link appeared. And on the sequence, I created the code as a primary key only without sequence and auto increment.

  • http://www.postgresql.org/docs/8.1/static/sql-createsequence.html

  • gmsantos, his problem is finding out which numbers were not registered between the periods of codes already registered.

  • @Jeangustavoprates in the background he does not have this problem, as I noticed in the comments he wants "a different system that draws more attention" and I do not see how a good practice allow the user to control the primary keys.

  • I even understand @gmsantos, but I really need the system that way. I’m getting orders here...

  • Emerson, SEQUENCE is meeting with part of what you said. But it’s up to you to also point out to your responsible boss or architect that this is bad practice and will likely cause future problems. Think, if this were a good idea, other programmers would do it this way, right?

Show 5 more comments

1

SELECT linha
  FROM (SELECT DECODE(codigo,linha,'N','S') AS  proximo
              ,linha
                 FROM (SELECT codigo
                             ,ROWNUM linha
                             ,nome 
                         FROM cliente
                        ORDER BY codigo))                          
 WHERE proximo = 'S'
   AND ROWNUM = 1

The above example I did with oracle, it makes the next number valid. If helped mark as right, thanks.

  • Kelvin, I need this way to be a different system even, where the client can put the number you want...! =/

  • Let me understand, I am the client and I want to register, but in this table there are two records, code 1 and code 3. If the user wants to register with code 7 for example, what should happen?

  • It would let pass normally, Dai in this example the next free code in the database would be code 2. I would show on the screen that!

  • I edited with the correct answer.

  • If I’m not mistaken I’d have to exchange the ROWNUM for row_number() for postgre,

  • Can you explain to me what this sql does? I believe it makes no sense to what I need

  • For example in a table with codes 1, 2, 5, 7 , 10 the query will return which will be the next available code, which for the example would be 3.

  • Worked for you?

  • I am used postgresql, and although not much knowledge in sql, I will try still ok? I am leaving now but tomorrow I put in practice

  • To tell you the truth, I couldn’t get through it, man. Code and line? I’m sorry for my ignorance but I don’t understand anything yet =/

Show 5 more comments

Browser other questions tagged

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