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 :

Demande d'aide pour un script


Sujet :

Développement SQL Server

Mode arborescent

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre à l'essai
    Profil pro
    Inscrit en
    Janvier 2009
    Messages
    5
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Janvier 2009
    Messages : 5
    Par défaut Demande d'aide pour un script
    Bonjour, j'essaye de faire un script SQL qui permet de récupérer un script d'insert de données a partir d'une table qui est déja remplie.

    Le script que j'ai créé marche, cependant j'aimerais l'améliorer sur un point et je ne trouve pas la solution.

    Je vous laisse toutes les informations pour que vous puissiez essayer.

    Le fichier CreateTableEtInserts.sql contient la creation de la table et ses données.

    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
     
    /*==============================================================*/
    /* Table : MEMBRE                                               */
    /*==============================================================*/
    create table MEMBRE (
       NUMERO_MEMBRE        bigint               identity,
       NOM_MEMBRE           varchar(20)          not null,
       PRENOM_MEMBRE        varchar(20)          not null,
       DATE_DE_NAISSANCE    datetime             null,
       RESPONSABILITE       varchar(25)          not null,
       CIVILITE_MEMBRE      smallint             not null,
       constraint PK_MEMBRE primary key  (NUMERO_MEMBRE)
    )
    go
     
    --------------------------------------------
    --
    --
    -- Inserts de la Table Membre
    --
    --
    --------------------------------------------
     
    INSERT INTO [MEMBRE] (NOM_MEMBRE,PRENOM_MEMBRE,DATE_DE_NAISSANCE,RESPONSABILITE,CIVILITE_MEMBRE)
    VALUES ('Bruel' , 'Patrick' , 'sept  1 1960 12:00AM' , 'CHANTEUR' , '0')
    GO
     
    INSERT INTO [MEMBRE] (NOM_MEMBRE,PRENOM_MEMBRE,DATE_DE_NAISSANCE,RESPONSABILITE,CIVILITE_MEMBRE)
    VALUES ('Morgane' , 'Clara' , 'avr  5 1982 12:00AM' , 'MANAGER' , '2')
    GO
     
    INSERT INTO [MEMBRE] (NOM_MEMBRE,PRENOM_MEMBRE,DATE_DE_NAISSANCE,RESPONSABILITE,CIVILITE_MEMBRE)
    VALUES ('Boulet' , 'Isabelle' , 'oct  5 1970 12:00AM' , 'CHANTEUSE' , '1')
    GO
     
    INSERT INTO [MEMBRE] (NOM_MEMBRE,PRENOM_MEMBRE,DATE_DE_NAISSANCE,RESPONSABILITE,CIVILITE_MEMBRE)
    VALUES ('Segara' , 'Hélène' , 'déc  8 1968 12:00AM' , 'CHANTEUSE' , '1')
    GO
     
    INSERT INTO [MEMBRE] (NOM_MEMBRE,PRENOM_MEMBRE,DATE_DE_NAISSANCE,RESPONSABILITE,CIVILITE_MEMBRE)
    VALUES ('NONE' , 'Lorie' , 'août  2 1985 12:00AM' , 'CHANTEUSE' , '2')
    GO
     
    INSERT INTO [MEMBRE] (NOM_MEMBRE,PRENOM_MEMBRE,DATE_DE_NAISSANCE,RESPONSABILITE,CIVILITE_MEMBRE)
    VALUES ('Roques' , 'Rodolphe' , 'août  1 1984 12:00AM' , 'MANAGER' , '0')
    GO
     
    INSERT INTO [MEMBRE] (NOM_MEMBRE,PRENOM_MEMBRE,DATE_DE_NAISSANCE,RESPONSABILITE,CIVILITE_MEMBRE)
    VALUES ('Souchon' , 'Alain' , 'juil  1 1919 12:00AM' , 'CHANTEUR' , '0')
    GO
     
    INSERT INTO [MEMBRE] (NOM_MEMBRE,PRENOM_MEMBRE,DATE_DE_NAISSANCE,RESPONSABILITE,CIVILITE_MEMBRE)
    VALUES ('Halliday' , 'Johnny' , 'oct  4 1945 12:00AM' , 'CHANTEUR' , '0')
    GO
     
    INSERT INTO [MEMBRE] (NOM_MEMBRE,PRENOM_MEMBRE,DATE_DE_NAISSANCE,RESPONSABILITE,CIVILITE_MEMBRE)
    VALUES ('NONE' , 'Garou' , 'août  6 1954 12:00AM' , 'CHANTEUR' , '0')
    GO
     
    INSERT INTO [MEMBRE] (NOM_MEMBRE,PRENOM_MEMBRE,DATE_DE_NAISSANCE,RESPONSABILITE,CIVILITE_MEMBRE)
    VALUES ('Tapis' , 'Bernard' , 'mai  1 1946 12:00AM' , 'JIGOLO' , '0')
    GO
     
    INSERT INTO [MEMBRE] (NOM_MEMBRE,PRENOM_MEMBRE,DATE_DE_NAISSANCE,RESPONSABILITE,CIVILITE_MEMBRE)
    VALUES ('Jospin' , 'Lionnel' , 'nov 28 1909 12:00AM' , 'JIGOLO' , '0')
    GO
     
    INSERT INTO [MEMBRE] (NOM_MEMBRE,PRENOM_MEMBRE,DATE_DE_NAISSANCE,RESPONSABILITE,CIVILITE_MEMBRE)
    VALUES ('Bush' , 'George' , 'sept 10 1945 12:00AM' , 'GUIGNOL' , '0')
    GO
     
    INSERT INTO [MEMBRE] (NOM_MEMBRE,PRENOM_MEMBRE,DATE_DE_NAISSANCE,RESPONSABILITE,CIVILITE_MEMBRE)
    VALUES ('Lewinsky' , 'Monica' , 'sept 25 1969 12:00AM' , 'SECRETAIRE' , '2')
    GO
     
    INSERT INTO [MEMBRE] (NOM_MEMBRE,PRENOM_MEMBRE,DATE_DE_NAISSANCE,RESPONSABILITE,CIVILITE_MEMBRE)
    VALUES ('Sarko' , 'Nico' , 'août 30 1955 12:00AM' , 'Joueur de pipo' , '0')
    GO
    Le fichier RecupInserts.sql contient le script qui permet de récupérer les inserts grace a des print.

    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
     
    declare @ColumnCount int;
    declare @ListeColumn varchar(8000);
    declare @ListeSelect varchar(8000);
    set @ColumnCount = 0
    set @ListeColumn = ''
    set @ListeSelect = ''
    while (SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'MEMBRE') > @ColumnCount
    	begin
    		set @ColumnCount = @ColumnCount + 1
    		if (COLUMNPROPERTY(OBJECT_ID('MEMBRE'),COL_NAME(OBJECT_ID('MEMBRE'), @ColumnCount),'IsIdentity') = 0)
    			begin
    				set @ListeColumn = @ListeColumn + (select (COL_NAME(OBJECT_ID('MEMBRE'), @ColumnCount)))
    				set @ListeSelect = @ListeSelect + 'convert (varchar(8000),'
    				set @ListeSelect = @ListeSelect + (select (COL_NAME(OBJECT_ID('MEMBRE'), @ColumnCount)))
    					if (SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'MEMBRE') > @ColumnCount
    						begin
    							set @ListeColumn = @ListeColumn + ','
    							set @ListeSelect = @ListeSelect + ')'+'+'''''' , ''''''+'
    						end
    					if (SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'MEMBRE') = @ColumnCount
    						begin
    							set @ListeSelect = @ListeSelect + ')'
    						end
    			end
    	end
     
    declare @VSelect varchar (8000)
    declare @VPrinting varchar (8000)
    declare @TmpTable table (
    Column1 varchar(8000)
    )
    set @VSelect = 'select '+@ListeSelect+' from membre'
     
    insert into @TmpTable (Column1)
    exec (@VSelect)
     
    Declare curseur_print cursor for
    select Column1 from @TmpTable
    open curseur_print;
    fetch next from curseur_print into @VPrinting
    Print '--------------------------------------------'
    Print '--'
    Print '--'
    Print '-- Inserts de la Table Membre'
    Print '--'
    Print '--'
    Print '--------------------------------------------'
    Print ''
    while (@@fetch_status = 0)
    	begin
    		Print 'INSERT INTO [MEMBRE] ('+@ListeColumn+')'
    		Print 'VALUES ('''+@VPrinting+''')'
    		Print 'GO'
    		Print ' '
    		fetch next from curseur_print into @VPrinting
    	end
    close curseur_print;
    deallocate curseur_print;
    Le probleme, c'est que y a une partie du script ou j'ai pas réussis a faire autrement que de creer une table temporaire et d'inserer les données que je voulais dedans.
    Et quand j'execute le script pour récupérer les inserts, je me retrouve avec écrit : (14*ligne(s) affectée(s))
    juste au dessus de mes prints.
    C'est assez génant parce que j'aimerais pouvoir ensuite l'exécuter sur plusieurs tables en meme temp et c'est pas top de devoir effacer ca entre chaque inserts.

    Si quelqu'un trouve une solution ... pour pouvoir soit faire en sorte que cela s'affiche pas, soit en utilisant pas de table temporaire.

    Merci.
    Fichiers attachés Fichiers attachés

Discussions similaires

  1. [MySQL] Demande d'aide pour réaliser un script
    Par maxwell398 dans le forum PHP & Base de données
    Réponses: 1
    Dernier message: 07/01/2010, 20h05
  2. demande d'aide pour création script.ksh
    Par LuckySoft dans le forum Linux
    Réponses: 2
    Dernier message: 26/11/2008, 13h34
  3. Demande d'aide pour script de connexion
    Par loupsolitaire dans le forum Windows
    Réponses: 4
    Dernier message: 03/12/2007, 12h59
  4. demande de l'aide pour un script php
    Par samaa dans le forum Langage
    Réponses: 5
    Dernier message: 03/03/2007, 18h03

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