Bonjour,
Deux instance SQL-SERVER sur la machine, l'une 2012 et l'autre 2008. Ma base est sur l'instance 2008.

J'ai une procédure stockée d'insertion, qui avant l'INSERT, doit générer une clé primaire de type VARCHAR. Tout fonctionne normalement pour une première insertion depuis mon interface ASP.NET ou ma base. Mais les insertions sont impossibles à partir de la 2nde tentative.

Ci joint le code de création de la table cible:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
USE [ENRE_PAX]
GO
 
/****** Object:  Table [dbo].[Agents]    Script Date: 10/04/2014 12:46:22 ******/
SET ANSI_NULLS ON
GO
 
SET QUOTED_IDENTIFIER ON
GO
 
CREATE TABLE [dbo].[Agents](
	[AgentMatricule] [nvarchar](10) NOT NULL,
	[Photo] [nvarchar](150) NOT NULL,
	[IdentifiantCivilite] [nvarchar](5) NOT NULL,
	[Nom] [nvarchar](50) NOT NULL,
	[Prenom] [nvarchar](50) NOT NULL,
	[DateNaissanceAgent] [datetime] NOT NULL,
	[LieuNaissanceAgent] [nvarchar](50) NULL,
	[EmpreinteIndexDroitAgent] [nvarchar](130) NOT NULL,
	[DateEmbauche] [datetime] NOT NULL,
	[SexeAgent] [nvarchar](2) NOT NULL,
	[AncienneteAgent] [int] NOT NULL,
	[Grade] [int] NULL,
	[Login] [nvarchar](30) NULL,
	[MotDePasse] [nvarchar](30) NULL,
	[DateCreationMotDePasse] [datetime] NULL,
	[DateDernièreConnexion] [datetime] NULL,
	[DateDernièreDeconnexion] [datetime] NULL,
	[AgentGroupe] [nvarchar](10) NULL,
	[AgentEquipe] [nvarchar](10) NULL,
	[TelephoneAgent] [nvarchar](15) NOT NULL,
	[EmailAgent] [nvarchar](50) NULL,
	[AdressePostaleAgent] [nvarchar](50) NULL,
	[DomicileAgent] [nvarchar](40) NULL,
	[NiveauAnglais] [int] NOT NULL,
	[NiveauEspagnol] [int] NOT NULL,
	[NiveauArabe] [int] NOT NULL,
	[NiveauPortuguais] [int] NOT NULL,
	[ObservationsAgent] [nvarchar](250) NULL,
 CONSTRAINT [PK_Agents] PRIMARY KEY CLUSTERED 
(
	[AgentMatricule] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY],
 CONSTRAINT [unique_login] UNIQUE NONCLUSTERED 
(
	[Login] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
 
GO
 
ALTER TABLE [dbo].[Agents]  WITH CHECK ADD  CONSTRAINT [FK_Agents_Civilite] FOREIGN KEY([IdentifiantCivilite])
REFERENCES [dbo].[Civilites] ([IdentifiantCivilite])
ON UPDATE CASCADE
ON DELETE CASCADE
GO
 
ALTER TABLE [dbo].[Agents] CHECK CONSTRAINT [FK_Agents_Civilite]
GO
Ci-joint le code création de la procédure stockée:

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
CREATE PROC [dbo].[PS_Insert_Agent]
			(	
				@AgentMatricule nvarchar(10),
				@Photo nvarchar(150),
				@IdentifiantCivilite nvarchar(5),
				@Nom nvarchar (50),
				@Prenom nvarchar(50),
				@DateNaissanceAgent datetime,
		 		@LieuNaissanceAgent varchar(50),
				@EmpreINTeIndexDroitAgent nvarchar(130),
				@DateEmbauche datetime,
				@SexeAgent nvarchar(2),
				@AncienneteAgent INT,
				@Grade int ,
				@Login nvarchar(30),
				@MotDepasse nvarchar(30),
				@DateCreationMotdePasse datetime ,
				@DateDernièreConnexion datetime,
				@DateDernièreDeconnexion datetime ,
				@AgentGroupe nvarchar(10),
				@AgentEquipe nvarchar(10) ,
				@TelephoneAgent nvarchar(15),
				@EmailAgent nvarchar(50) ,
				@AdressePostaleAgent nvarchar(50) ,
				@DomicileAgent nvarchar(40) ,
				@NiveauAnglais INT ,
				@NiveauEspagnol INT ,
				@NiveauArabe INT  ,
				@NiveauPortuguais INT  ,
				@ObservationsAgent nvarchar(250) 
			)
 
 
as
 
BEGIN
SET NOCOUNT ON;
			BEGIN TRY
 
 
				BEGIN TRANSACTION
 
								--	GENERATION DU MATRICULE
								DECLARE @TypeAGS varchar(6), @OrdreNumeric INT--, @Compteur INT
								SET @TypeAGS = 'NSRT-S'
								SET @OrdreNumeric = (SELECT count(AgentMatricule) FROM Agents)
								SET @OrdreNumeric = @OrdreNumeric  + 1
 
								 SET @AgentMatricule =(SELECT @TypeAGS +(SELECT CAST((SELECT(REPLICATE('0',4 -LEN(@OrdreNumeric)))) as VARCHAR)) as varchar) + 
								(SELECT cast(@OrdreNumeric as varchar))
								 --SELECT @Matricule as MatriculeAgent
 
										SET @DateCreationMotdePasse= (SELECT GETDATE())
										SET @DateDernièreConnexion = (SELECT GETDATE())
 
 
 
								 IF EXISTS(SELECT * FROM Agents WHERE AgentMatricule = @AgentMatricule)
 
									WHILE(@OrdreNumeric< @OrdreNumeric + 1)
										BEGIN	
														SET @OrdreNumeric= @OrdreNumeric + 1
 
														SET @AgentMatricule =(SELECT @TypeAGS +(SELECT CAST((SELECT(REPLICATE('0',4 -LEN(@OrdreNumeric)))) as VARCHAR)) as varchar) + 
														(SELECT cast(@OrdreNumeric as varchar))
 
 
												IF  EXISTS(SELECT * FROM Agents WHERE AgentMatricule = @AgentMatricule)
 
 
															CONTINUE
												ELSE
 
														INSERT INTO dbo.Agents (AgentMatricule,Photo,IdentifiantCivilite,nom,Prenom,DateNaissanceAgent,LieuNaissanceAgent,EmpreinteIndexDroitAgent,
		 															DateEmbauche,SexeAgent,AncienneteAgent,Grade,[Login] ,MotDepasse ,DateCreationMotdePasse ,DateDernièreConnexion,
																	DateDernièreDeconnexion,AgentGroupe,AgentEquipe,TelephoneAgent,EmailAgent,AdressePostaleAgent,DomicileAgent,
																	NiveauAnglais,NiveauEspagnol,NiveauArabe,NiveauPortuguais,ObservationsAgent)
 
 
														 VALUES(@AgentMatricule ,@Photo,@IdentifiantCivilite,@Nom ,@Prenom ,@DateNaissanceAgent,@LieuNaissanceAgent ,@EmpreINTeIndexDroitAgent ,
																@DateEmbauche ,	@SexeAgent ,@AncienneteAgent ,@Grade, @Login ,@MotDepasse ,@DateCreationMotdePasse,
																@DateDernièreConnexion,@DateDernièreDeconnexion,@AgentGroupe, @AgentEquipe, @TelephoneAgent ,
																@EmailAgent  ,@AdressePostaleAgent,	@DomicileAgent ,@NiveauAnglais , @NiveauEspagnol ,@NiveauArabe ,
																 @NiveauPortuguais,	@ObservationsAgent )
 
 
 
													BREAK
 
												--PRINT 'OPERATION REUSSIE';
										END
								ELSE
									BEGIN
 
												INSERT INTO dbo.Agents (AgentMatricule,Photo,IdentifiantCivilite,nom,Prenom,DateNaissanceAgent,LieuNaissanceAgent,EmpreinteIndexDroitAgent,
																		DateEmbauche,SexeAgent,AncienneteAgent,Grade,[Login] ,MotDepasse ,DateCreationMotdePasse ,DateDernièreConnexion,
																		DateDernièreDeconnexion,AgentGroupe,AgentEquipe,TelephoneAgent,EmailAgent,AdressePostaleAgent,DomicileAgent,
																		NiveauAnglais,NiveauEspagnol,NiveauArabe,NiveauPortuguais,ObservationsAgent)
 
 
															 VALUES(@AgentMatricule ,@Photo,@IdentifiantCivilite,@Nom ,@Prenom ,@DateNaissanceAgent,@LieuNaissanceAgent ,@EmpreINTeIndexDroitAgent ,
																	@DateEmbauche ,	@SexeAgent ,@AncienneteAgent ,@Grade, @Login ,@MotDepasse ,@DateCreationMotdePasse,
																	@DateDernièreConnexion,@DateDernièreDeconnexion,@AgentGroupe, @AgentEquipe, @TelephoneAgent , @EmailAgent,
																	@AdressePostaleAgent,	@DomicileAgent ,@NiveauAnglais , @NiveauEspagnol ,@NiveauArabe ,
																	 @NiveauPortuguais,	@ObservationsAgent )
 
									END
								COMMIT TRANSACTION
 
											--PRINT 'OPERATION REUSSIE'
 
 
 
 
 
		END TRY
		BEGIN CATCH
				Rollback Transaction
				PrINT 'OPERATION échouée'
 
		END CATCH
END
Ici le script d'exécution de la procédure en question.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
exec PS_Insert_Agent '','photo','Mr','Nom','Prenom','1998-05-01T00:00:00.000','Maville','Empreinte','2008-11-23T00:00:00.000','M',1425,5,'login','MotdePasse','2013-12-12T10:02:27.757','2013-12-12T10:02:27.757','2013-12-12T10:02:27.757','Gr','Eq','Telephone','email@com','adressepostale','domicile',2,0,0,0,''
Merci de m'aider à résoudre ce problème.
Cordialement.