IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Développement SQL Server Discussion :

[2008] Problème avec CTE


Sujet :

Développement SQL Server

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    CUCARACHA
    Invité(e)
    Par défaut [2008] Problème avec CTE
    Salut,

    J'ai créé un petit éditeur de formulaires dynamiques.
    Formulaire
    Groupe fonctionnel de champs
    Champs

    J'aimerais donner la possibilité à mes utilisateurs de dupliquer dans tous les freres du groupe en cours les fils de ce groupe.

    J'ai essayé de passer par une CTE pour éviter une boucle mais j'ai du faire une erreur de conception car j'ai le message : TargetGroup.Id ne peut pas être lié.

    J'imagine que c'est une limitation des CTE car la logique me semble correcte.

    Voici la source de ma SP :

    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
     
    ALTER PROCEDURE IS_APP.spDynamicFormGroup
    (
    	@DynamicFormGroupToDuplicate int 
    )
    AS
    BEGIN
    	DECLARE 
    		@tblGroupsId TABLE(Id int)
     
    	DECLARE
    		@FK_DynamicForm_FieldType int,
    		@FK_DynamicForm_FieldConttrolType int,
    		@Title nvarchar(50),
    		@Length int,
    		@minValue nvarchar(50),
    		@maxValue nvarchar(50),
    		@Required bit,
    		@IconURL nvarchar(127),
    		@MessageMissing nvarchar(127),
    		@MessageWrongType nvarchar(127),
    		@MessageUnderLowerBound nvarchar(127),
    		@MessageOverUpperBound nvarchar(127),
    		@SortIndex int	
     
    	INSERT INTO @tblGroupsId (Id)
    		SELECT 
    		G.Id
    	FROM DynamicForm_Group G
    	WHERE
    		G.Id <> @DynamicFormGroupToDuplicate
    		AND
    		G.FK_DynamicForm = (SELECT FK_DynamicForm FROM DynamicForm_Group WHERE Id = @DynamicFormGroupToDuplicate);	
     
    	WITH TargetGroup AS (
    		SELECT Id
    		FROM @tblGroupsId
    	) INSERT INTO IS_APP.DynamicForm_Fields 
    		 (
    			FK_DynamicForm_Group,
    			FK_DynamicForm_FieldConttrolType,
    			FK_DynamicForm_FieldType,
    			IconURL,
    			Length,
    			maxValue,
    			MessageMissing,
    			MessageOverUpperBound,
    			MessageWrongType,
    			minValue,
    			Required,
    			SortIndex,
    			Title
    		)
    		SELECT 
    			TargetGroup.Id, <---------- PROBLEME ICI
    			FK_DynamicForm_FieldConttrolType,
    			FK_DynamicForm_FieldType,
    			IconURL,
    			Length,
    			maxValue,
    			MessageMissing,
    			MessageOverUpperBound,
    			MessageWrongType,
    			minValue,
    			Required,
    			SortIndex,
    			Title
    		FROM
    			IS_APP.DynamicForm_Field
    		WHERE
    			FK_DynamicForm_Group = @DynamicFormGroupToDuplicate
    En fait, ma première requête récupère les ID des groupes auxquels je dois associer les champs à rattacher.

    La seconde insère tous les champs issus du groupe dont il faut dupliquer les champs dans la table des champs en utilisant le Id du groupe issu de la CTE.

    Je ne comprends pas pourquoi une erreur de liaison se produit.

    Quelqu'un pourrait-il m'aider ?

    D'avance merci

    Laurent Jordi

  2. #2
    CUCARACHA
    Invité(e)
    Par défaut
    La table temporaire n'est pas indispensable mais là n'est pas le sujet du problème...

  3. #3
    CUCARACHA
    Invité(e)
    Par défaut Votre avis sur la solution ?
    Salut,

    J'ai trouvé une solution mais je ne suis pas certain que ce soit le top en termes d'optimisation.

    Je n'ai pas besoin d'une super perf mais j'aimerais plutôt savoir s'il y aurait un vecteur d'amélioration...

    Fonction:
    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
     
    ALTER FUNCTION IS_APP.fnDynamicForm_GetBrotherGroups
    	(
    		@GroupId int
    	)
    RETURNS @toReturn TABLE (
    	FK_DynamicForm int,
    	FK_DynamicForm_Group int,
    	SortIndex int
    )
    AS
    	BEGIN
    		DECLARE @DynamicFormId int
     
    		SELECT 
    			@DynamicFormId = g1.FK_DynamicForm
    		FROM
    			IS_APP.DynamicForm_DynamicForm_Group as g1
    		WHERE
    			g1.FK_DynamicForm_Group = @GroupId
     
     
    		INSERT INTO @toReturn (
    			FK_DynamicForm,
    			FK_DynamicForm_Group,
    			SortIndex
    		)SELECT 
    			@DynamicFormId as FK_DynamicForm, 
    			J.FK_DynamicForm_Group,
    			J.SortIndex
    		FROM 
    			IS_APP.DynamicForm_Group as G inner join IS_APP.DynamicForm_DynamicForm_Group as J
    				on (G.Id = J.FK_DynamicForm_Group AND G.Id <> @GroupId)
    		WHERE 
    			J.FK_DynamicForm = @DynamicFormId
     
    	RETURN
    	END
    Proc:
    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
     
    ALTER PROCEDURE IS_APP.spDynamicFormAttachFieldsToBrothersGroup
    	(
    	@GroupId int
    	)
    AS
    	DECLARE 
    		@FK_DynamicForm_FieldType int,
    		@FK_DynamicForm_FieldControlType int,
    		@Title nvarchar(50),
    		@Length int,
    		@minValue nvarchar(50),
    		@maxValue nvarchar(50),
    		@Required bit,
    		@IconURL nvarchar(127),
    		@MessageMissing nvarchar(127),
    		@MessageWrongType nvarchar(127),
    		@MessageUnderLowerBound nvarchar(127),
    		@MessageOverUpperBound nvarchar(127),
    		@SortIndex int
     
    	-- Récupération des champs du groupe courant
    	SELECT
    		@FK_DynamicForm_FieldType = FLD.FK_DynamicForm_FieldType,
    		@FK_DynamicForm_FieldControlType = FLD.FK_DynamicForm_FieldControlType,
    		@Title = FLD.Title,
    		@Length = FLD.Length,
    		@minValue = FLD.minValue,
    		@maxValue = FLD.maxValue,
    		@Required = FLD.Required,
    		@IconURL = FLD.IconURL,
    		@MessageMissing = FLD.MessageMissing,
    		@MessageWrongType = FLD.MessageWrongType,
    		@MessageUnderLowerBound = FLD.MessageUnderLowerBound,
    		@MessageOverUpperBound = FLD.MessageOverUpperBound,
    		@SortIndex = FLD.SortIndex
    	FROM
    		IS_APP.DynamicForm_Field as FLD
    	WHERE
    		FLD.FK_DynamicForm_Group = @GroupId
    	ORDER BY
    		FLD.SortIndex;
     
     
    	with FieldsToDuplicate as (
    		SELECT
    			GRPS.FK_DynamicForm_Group as FK_DynamicForm_Group,
    			FLD.FK_DynamicForm_FieldType as FK_DynamicForm_FieldType,
    			FLD.FK_DynamicForm_FieldControlType as FK_DynamicForm_FieldControlType,
    			FLD.Title as Title,
    			FLD.Length as Length,
    			FLD.minValue as minValue,
    			FLD.maxValue as maxValue,
    			FLD.Required as Required,
    			FLD.IconURL as IconURL,
    			FLD.MessageMissing as MessageMissing,
    			FLD.MessageWrongType as MessageWrongType,
    			FLD.MessageUnderLowerBound as MessageUnderLowerBound,
    			FLD.MessageOverUpperBound as MessageOverUpperBound,
    			FLD.SortIndex as SortIndex
    		FROM
    			IS_APP.DynamicForm_Field as FLD, IS_APP.fnDynamicForm_GetBrotherGroups(@GroupId) as GRPS
    		WHERE
    			FLD.FK_DynamicForm_Group = @GroupId	
    		)	INSERT INTO IS_APP.DynamicForm_Field (
    				FK_DynamicForm_Group,
    				FK_DynamicForm_FieldType,
    				FK_DynamicForm_FieldControlType,
    				Title,
    				Length,
    				minValue,
    				maxValue,
    				Required,
    				IconURL,
    				MessageMissing,
    				MessageWrongType,
    				MessageUnderLowerBound,
    				MessageOverUpperBound,
    				SortIndex
    	)
    		SELECT 
    			F.FK_DynamicForm_Group,
    			F.FK_DynamicForm_FieldType,
    			F.FK_DynamicForm_FieldControlType,
    			F.Title,
    			F.Length,
    			F.minValue,
    			F.maxValue,
    			F.Required,
    			F.IconURL,
    			F.MessageMissing,
    			F.MessageWrongType,
    			F.MessageUnderLowerBound,
    			F.MessageOverUpperBound,
    			F.SortIndex
    		FROM
    			FieldsToDuplicate as F
    		ORDER BY
    			F.SortIndex
    	RETURN
    Merci pour votre aide,

    Laurent Jordi

  4. #4
    Rédacteur

    Avatar de SQLpro
    Homme Profil pro
    Expert bases de données / SQL / MS SQL Server / Postgresql
    Inscrit en
    Mai 2002
    Messages
    21 999
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Var (Provence Alpes Côte d'Azur)

    Informations professionnelles :
    Activité : Expert bases de données / SQL / MS SQL Server / Postgresql
    Secteur : Conseil

    Informations forums :
    Inscription : Mai 2002
    Messages : 21 999
    Billets dans le blog
    6
    Par défaut
    Remplace :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    TargetGroup.Id, <---------- PROBLEME ICI
    Par :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    (SELECT ID, FROM TargetGroup), <---------- plus de PROBLEME ICI !!!
    A +
    Frédéric Brouard - SQLpro - ARCHITECTE DE DONNÉES - expert SGBDR et langage SQL
    Le site sur les SGBD relationnels et le langage SQL: http://sqlpro.developpez.com/
    Blog SQL, SQL Server, SGBDR : http://blog.developpez.com/sqlpro
    Expert Microsoft SQL Server - M.V.P. (Most valuable Professional) MS Corp.
    Entreprise SQL SPOT : modélisation, conseils, audit, optimisation, formation...
    * * * * * Expertise SQL Server : http://mssqlserver.fr/ * * * * *

  5. #5
    CUCARACHA
    Invité(e)
    Par défaut
    Ah ok, je n'y avais pas pensé... Merci pour ton aide

    Laurent Jordi

Discussions similaires

  1. Visual Basic 2008 probléme avec tablebindingsource
    Par ashxdh dans le forum VB.NET
    Réponses: 2
    Dernier message: 26/11/2009, 11h48
  2. [VB express 2008] Problème avec datagrid
    Par libremax dans le forum Windows Forms
    Réponses: 4
    Dernier message: 06/10/2009, 11h36
  3. [VB 2008] Problème avec excel
    Par libremax dans le forum Windows Forms
    Réponses: 3
    Dernier message: 29/06/2009, 21h44
  4. [VS 2008] Problème avec Libc.lib
    Par Ange44 dans le forum Visual C++
    Réponses: 4
    Dernier message: 05/09/2008, 11h12
  5. EURO 2008, problèmes avec les billets?
    Par gantec dans le forum Films & TV
    Réponses: 6
    Dernier message: 31/03/2008, 15h09

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo