Forum |  HardWare.fr | News | Articles | PC | S'identifier | S'inscrire | Shop Recherche
1468 connectés 

  FORUM HardWare.fr
  Programmation
  SQL/NoSQL

  Séquence d'incrémentation dans SQL Server 2000

 


 Mot :   Pseudo :  
 
Bas de page
Auteur Sujet :

Séquence d'incrémentation dans SQL Server 2000

n°1139093
Fry85
Posté le 04-07-2005 à 10:54:41  profilanswer
 

Voilà, ma base est quasiment finie
 
Mais en réfléchissant à l'utilisation à la longue, je crois que mes auto-incréments définis sur 4 octets risquent peut être de poser des problèmes à un moment.
J'aimerais bien repartir du début pour les identifiants mais l'auto-incrément de SQL Server 2000 ne le fait pas (il plante arrivé au bout).
Et vu que les identifiants auront déjà été utilisé une fois, peut-être que certains seront encore présents dans la base, donc faudrait qu'on puisse incrémenter mais aussi sauter les id déjà utilisés (si c'est pas possible, au moins que je puisse le faire redémarrer de 0)
 
Merci  :) !

mood
Publicité
Posté le 04-07-2005 à 10:54:41  profilanswer
 

n°1139381
Fry85
Posté le 04-07-2005 à 14:26:07  profilanswer
 

Bon personne pour ma rescousse !
Merci ca fait plaisir
 
Bref, je me suis creuse un peu la tete dessus et je pars maintenant sur un trigger du style

Code :
  1. CREATE TRIGGER T1
  2. ON Test
  3. FOR INSERT
  4. AS ...


 
Le problème c'est que je ne vois pas comment récupérer les données qui doivent être insérées avant qu'elles ne le soient, pour modifier si besoin est l'id, et finalement insérée une ligne correcte.
En tout cas si le trigger pouvait le faire, ca me permettrait de controle le max des id et de revenir a 0 si besoin, et aussi de vérifier si l'id qu'on souhaite insérer n'est pas déjà présents (dans ce cas on saute au prochain) !
 
Quelqu'un peut m'aider ? Merci !

n°1139491
Fry85
Posté le 04-07-2005 à 15:51:58  profilanswer
 

Bon, je rame toujours mais j'essaye d'explorer de nouvelles pistes :
par exemple, est-ce que je peux récupérer avec une requête les identifiants d'une colonne (non soumise à l'auto incrément) qui ne sont pas utilisés (récupérer les trous d'id en fait) ou au moins un de ceux-là pour me permettre d'enclencher l'insertion d'une ligne seulement.
 
C'est tout ce que je veux :(

n°1141291
Arjuna
Aircraft Ident.: F-MBSD
Posté le 05-07-2005 à 21:20:17  profilanswer
 

Hmmm...
 
Un truc comme ça peut-être ?
 

Code :
  1. create table matable
  2. (
  3. id   int,
  4. champ1 char(10),
  5. champ2 char(10)
  6. )
  7. GO
  8. create unique clustered index uix_matable on matable(id)
  9. GO
  10. create trigger trg_matable_autoincrement
  11. on matable
  12. instead of insert
  13. as
  14. declare @newid int
  15. begin
  16. select @newid = isnull(min(t1.id), 0) + 1
  17. from matable t1
  18. where not exists (select null from matable t2 where t2.id = t1.id + 1)
  19. insert into matable (id, champ1, champ2) select @newid, champ1, champ2 from inserted
  20. -- debug, passer à 1 = 0 pour la production
  21. if 1 = 1
  22. begin
  23.  select '1 reçu' action, id, champ1, champ2 from inserted
  24.  union
  25.  select '2 écrit' action, id, champ1, champ2 from matable where id = @newid
  26. end
  27. end
  28. go
  29. insert into matable (champ1, champ2) values ('ligne 1', 'position 1')
  30. insert into matable (champ1, champ2) values ('ligne 2', 'position 2')
  31. insert into matable (champ1, champ2) values ('ligne 3', 'position 3')
  32. delete matable where id = 2
  33. insert into matable (champ1, champ2) values ('ligne 4', 'position 2')
  34. insert into matable (id, champ1, champ2) values (200, 'ligne 200', 'position 4')
  35. select * from matable


 
Sortie :

Code :
  1. (1 ligne(s) affectée(s))
  2. action  id          champ1     champ2   
  3. ------- ----------- ---------- ----------
  4. 1 reçu  NULL        ligne 1    position 1
  5. 2 écrit 1           ligne 1    position 1
  6. (2 ligne(s) affectée(s))
  7. (1 ligne(s) affectée(s))
  8. (1 ligne(s) affectée(s))
  9. action  id          champ1     champ2   
  10. ------- ----------- ---------- ----------
  11. 1 reçu  NULL        ligne 2    position 2
  12. 2 écrit 2           ligne 2    position 2
  13. (2 ligne(s) affectée(s))
  14. (1 ligne(s) affectée(s))
  15. (1 ligne(s) affectée(s))
  16. action  id          champ1     champ2   
  17. ------- ----------- ---------- ----------
  18. 1 reçu  NULL        ligne 3    position 3
  19. 2 écrit 3           ligne 3    position 3
  20. (2 ligne(s) affectée(s))
  21. (1 ligne(s) affectée(s))
  22. (1 ligne(s) affectée(s))
  23. (1 ligne(s) affectée(s))
  24. action  id          champ1     champ2   
  25. ------- ----------- ---------- ----------
  26. 1 reçu  NULL        ligne 4    position 2
  27. 2 écrit 2           ligne 4    position 2
  28. (2 ligne(s) affectée(s))
  29. (1 ligne(s) affectée(s))
  30. (1 ligne(s) affectée(s))
  31. action  id          champ1     champ2   
  32. ------- ----------- ---------- ----------
  33. 1 reçu  200         ligne 200  position 4
  34. 2 écrit 4           ligne 200  position 4
  35. (2 ligne(s) affectée(s))
  36. (1 ligne(s) affectée(s))
  37. id          champ1     champ2   
  38. ----------- ---------- ----------
  39. 1           ligne 1    position 1
  40. 2           ligne 4    position 2
  41. 3           ligne 3    position 3
  42. 4           ligne 200  position 4
  43. (4 ligne(s) affectée(s))


Message édité par Arjuna le 05-07-2005 à 21:23:48
n°1141299
Arjuna
Aircraft Ident.: F-MBSD
Posté le 05-07-2005 à 21:25:08  profilanswer
 

PS: par contre, t'as pas intérêt à avoir beaucoup de lignes, parceque ça va vite rammer !


Message édité par Arjuna le 05-07-2005 à 21:25:30
n°1141314
Arjuna
Aircraft Ident.: F-MBSD
Posté le 05-07-2005 à 21:29:03  profilanswer
 

A noter que ça tient bien les transactions :
 

Code :
  1. begin transaction
  2. insert into matable (champ1, champ2) values ('ligne 1', 'position 1')
  3. insert into matable (champ1, champ2) values ('ligne 2', 'position 2')
  4. insert into matable (champ1, champ2) values ('ligne 3', 'position 3')
  5. delete matable where id = 2
  6. insert into matable (champ1, champ2) values ('ligne 4', 'position 2')
  7. insert into matable (id, champ1, champ2) values (200, 'ligne 200', 'position 4')
  8. select * from matable
  9. rollback
  10. select * from matable


 

Code :
  1. (1 ligne(s) affectée(s))
  2. action  id          champ1     champ2   
  3. ------- ----------- ---------- ----------
  4. 1 reçu  NULL        ligne 1    position 1
  5. 2 écrit 5           ligne 1    position 1
  6. (2 ligne(s) affectée(s))
  7. (1 ligne(s) affectée(s))
  8. (1 ligne(s) affectée(s))
  9. action  id          champ1     champ2   
  10. ------- ----------- ---------- ----------
  11. 1 reçu  NULL        ligne 2    position 2
  12. 2 écrit 6           ligne 2    position 2
  13. (2 ligne(s) affectée(s))
  14. (1 ligne(s) affectée(s))
  15. (1 ligne(s) affectée(s))
  16. action  id          champ1     champ2   
  17. ------- ----------- ---------- ----------
  18. 1 reçu  NULL        ligne 3    position 3
  19. 2 écrit 7           ligne 3    position 3
  20. (2 ligne(s) affectée(s))
  21. (1 ligne(s) affectée(s))
  22. (1 ligne(s) affectée(s))
  23. (1 ligne(s) affectée(s))
  24. action  id          champ1     champ2   
  25. ------- ----------- ---------- ----------
  26. 1 reçu  NULL        ligne 4    position 2
  27. 2 écrit 2           ligne 4    position 2
  28. (2 ligne(s) affectée(s))
  29. (1 ligne(s) affectée(s))
  30. (1 ligne(s) affectée(s))
  31. action  id          champ1     champ2   
  32. ------- ----------- ---------- ----------
  33. 1 reçu  200         ligne 200  position 4
  34. 2 écrit 8           ligne 200  position 4
  35. (2 ligne(s) affectée(s))
  36. (1 ligne(s) affectée(s))
  37. id          champ1     champ2   
  38. ----------- ---------- ----------
  39. 1           ligne 1    position 1
  40. 2           ligne 4    position 2
  41. 3           ligne 3    position 3
  42. 4           ligne 200  position 4
  43. 5           ligne 1    position 1
  44. 6           ligne 2    position 2
  45. 7           ligne 3    position 3
  46. 8           ligne 200  position 4
  47. (8 ligne(s) affectée(s))
  48. id          champ1     champ2   
  49. ----------- ---------- ----------
  50. 1           ligne 1    position 1
  51. 2           ligne 4    position 2
  52. 3           ligne 3    position 3
  53. 4           ligne 200  position 4
  54. (4 ligne(s) affectée(s))


Message édité par Arjuna le 05-07-2005 à 21:29:18
n°1141678
Fry85
Posté le 06-07-2005 à 09:54:37  profilanswer
 

Merci pour ta soluce, enfin j'en ai trouvee une autre dans la doc de SQL Server 2000 que j'ai adapté, ca donne ca :
 

Code :
  1. IF EXISTS (SELECT name FROM sysobjects
  2.    WHERE name = 'InsertAction' AND type = 'TR')
  3.    DROP TRIGGER InsertAction
  4. GO
  5. CREATE TRIGGER InsertAction
  6. ON Action
  7. INSTEAD OF INSERT
  8. AS
  9. DECLARE @minIdVal INT
  10. DECLARE @nextIdVal INT
  11. SELECT @minIdVal = MIN(idAction) FROM Action
  12. IF @minIdVal = 1
  13.    SELECT @nextIdVal = (MIN(idAction) + 1)
  14.    FROM Action a
  15.    WHERE idAction BETWEEN 1 AND 2147483647
  16.       AND NOT EXISTS (SELECT * FROM Action a1 WHERE a1.idAction = a.idAction + 1)
  17. ELSE
  18.    SET @nextIdVal = 1
  19. DECLARE @descriptif VARCHAR(255)
  20. SELECT @descriptif = descriptifAction FROM Inserted
  21. DECLARE @date DATETIME
  22. SELECT @date = dateAction FROM Inserted
  23. DECLARE @idSession INT
  24. SELECT @idSession = idSessionAction FROM Inserted
  25. DECLARE @idTypeAction INT
  26. SELECT @idTypeAction = idTypeActionAction FROM Inserted
  27. INSERT INTO Action (idAction, descriptifAction, dateAction, idSessionAction, idTypeActionAction)
  28. VALUES (@nextIdVal, @descriptif, @date, @idSession, @idtypeAction)


 
Ca marche bien mais je sais pas si ca risque pas d'être assez lent pour ma table qui contiendra 2 000 000 d'enregistrements !

n°1141706
Arjuna
Aircraft Ident.: F-MBSD
Posté le 06-07-2005 à 10:20:25  profilanswer
 

Ben ma soluce est plus courte et fait pareil :p


Aller à :
Ajouter une réponse
  FORUM HardWare.fr
  Programmation
  SQL/NoSQL

  Séquence d'incrémentation dans SQL Server 2000

 

Sujets relatifs
Flash Communication Server / Netconnection.call / onResult[c# - SQL] id du dernier enregistrement
mise à jour automatique d'un formulaire avec requete SQL[c#] Insertion dateTime dans SQL serveur
SQL Server 2005 Expressincrémentation auto dans mysql
requete SQL sous ACCESSeasy php j arrive pas a demarrer mon server sql
REQUETE SQL VERS 2 BASE DE MON SERV et ERREUR - PERDU JE SUIS 
Plus de sujets relatifs à : Séquence d'incrémentation dans SQL Server 2000


Copyright © 1997-2022 Hardware.fr SARL (Signaler un contenu illicite / Données personnelles) / Groupe LDLC / Shop HFR