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

  1. #1
    Candidat au Club
    Profil pro
    Inscrit en
    Janvier 2009
    Messages
    5
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Janvier 2009
    Messages : 5
    Points : 2
    Points
    2
    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

  2. #2
    Rédacteur

    Avatar de SQLpro
    Homme Profil pro
    Expert bases de données / SQL / MS SQL Server / Postgresql
    Inscrit en
    Mai 2002
    Messages
    21 768
    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 768
    Points : 52 565
    Points
    52 565
    Billets dans le blog
    5
    Par défaut
    Essayez avec un SET NOCOUNT ON.

    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/ * * * * *

  3. #3
    Candidat au Club
    Profil pro
    Inscrit en
    Janvier 2009
    Messages
    5
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Janvier 2009
    Messages : 5
    Points : 2
    Points
    2
    Par défaut
    C'est génial ca marche merci

  4. #4
    Candidat au Club
    Profil pro
    Inscrit en
    Janvier 2009
    Messages
    5
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Janvier 2009
    Messages : 5
    Points : 2
    Points
    2
    Par défaut
    J'ai fini de faire le script grace a l'aide de SQLPro alors je laisse le code complet dessous, ca peut toujours servir.

    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
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
     
    Create Procedure Print_Inserts_From
    @VNomTable varchar(50)
    as
    	Begin
     
    ---- Creation Table Temporaire + Inserts ----
     
    Set NoCount ON
    declare @ColumnCount int;
    declare @ListeColumn varchar(8000);
    declare @ListeColumn1 varchar(8000);
    declare @SchemaName varchar(8000);
    SET @ColumnCount = 0
    SET @ListeColumn = ''
    SET @ListeColumn1 = ''
    SET @SchemaName = (select TABLE_SCHEMA from INFORMATION_SCHEMA.TABLES where TABLE_NAME = @VNomTable)
     
    		while (SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = @VNomTable) > @ColumnCount
    			begin
    				SET @ColumnCount = @ColumnCount + 1
    				IF (COLUMNPROPERTY(OBJECT_ID(@SchemaName+'.'+@VNomTable),COL_NAME(OBJECT_ID(@SchemaName+'.'+@VNomTable), @ColumnCount),'IsIdentity') = 0)
    					begin
    						SET @ListeColumn = @ListeColumn + (SELECT (COL_NAME(OBJECT_ID(@SchemaName+'.'+@VNomTable), @ColumnCount)))
    						IF (select DATA_TYPE from INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = @VNomTable and COLUMN_NAME = COL_NAME(OBJECT_ID(@SchemaName+'.'+@VNomTable), @ColumnCount)) = 'datetime'
    							begin
    								SET @ListeColumn1 = @ListeColumn1 + 'CONVERT (varchar(2), datepart (dd,'+(COL_NAME(OBJECT_ID(@SchemaName+'.'+@VNomTable), @ColumnCount))+'))
    								+ ''/'' + CONVERT (varchar (2), datepart (mm, '+(COL_NAME(OBJECT_ID(@SchemaName+'.'+@VNomTable), @ColumnCount))+'))
    								+ ''/'' + CONVERT (varchar (4), datepart (yy, '+(COL_NAME(OBJECT_ID(@SchemaName+'.'+@VNomTable), @ColumnCount))+'))
    								+ '' '' + CONVERT (varchar (4), datepart (hh, '+(COL_NAME(OBJECT_ID(@SchemaName+'.'+@VNomTable), @ColumnCount))+'))
    								+ '':'' + CONVERT (varchar (4), datepart (mi, '+(COL_NAME(OBJECT_ID(@SchemaName+'.'+@VNomTable), @ColumnCount))+'))
    								+ '':'' + CONVERT (varchar (4), datepart (ss, '+(COL_NAME(OBJECT_ID(@SchemaName+'.'+@VNomTable), @ColumnCount))+'))
    								+ ''.'' + CONVERT (varchar (4), datepart (ms, '+(COL_NAME(OBJECT_ID(@SchemaName+'.'+@VNomTable), @ColumnCount))+'))'
    							end
    						IF (select DATA_TYPE from INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = @VNomTable and COLUMN_NAME = COL_NAME(OBJECT_ID(@SchemaName+'.'+@VNomTable), @ColumnCount)) = 'smalldatetime'
    							begin
    								SET @ListeColumn1 = @ListeColumn1 + 'CONVERT (varchar(2), datepart (dd,'+(COL_NAME(OBJECT_ID(@SchemaName+'.'+@VNomTable), @ColumnCount))+'))
    								+ ''/'' + CONVERT (varchar (2), datepart (mm, '+(COL_NAME(OBJECT_ID(@SchemaName+'.'+@VNomTable), @ColumnCount))+'))
    								+ ''/'' + CONVERT (varchar (4), datepart (yy, '+(COL_NAME(OBJECT_ID(@SchemaName+'.'+@VNomTable), @ColumnCount))+'))
    								+ '' '' + CONVERT (varchar (4), datepart (hh, '+(COL_NAME(OBJECT_ID(@SchemaName+'.'+@VNomTable), @ColumnCount))+'))
    								+ '':'' + CONVERT (varchar (4), datepart (mi, '+(COL_NAME(OBJECT_ID(@SchemaName+'.'+@VNomTable), @ColumnCount))+'))
    								+ '':'' + CONVERT (varchar (4), datepart (ss, '+(COL_NAME(OBJECT_ID(@SchemaName+'.'+@VNomTable), @ColumnCount))+'))'
    							end
    						Else
    							begin
    								SET @ListeColumn1 = @ListeColumn1 + (SELECT (COL_NAME(OBJECT_ID(@SchemaName+'.'+@VNomTable), @ColumnCount)))
    							end
    						IF (SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = @VNomTable) > @ColumnCount
    							begin
    								SET @ListeColumn = @ListeColumn + ' varchar(8000),'
    								SET @ListeColumn1 = @ListeColumn1 + ','
    							end
    						Else
    							begin
    								SET @ListeColumn = @ListeColumn + ' varchar(8000)'
    								SET @ListeColumn1 = @ListeColumn1
    							end
    					end
    			end
     
    Declare @ExecDeclareTable varchar(8000)
    set @ExecDeclareTable =
    'create table TmpTable (
    '+@ListeColumn+'
    )
    insert into TmpTable
    select '+@ListeColumn1+' from '+@SchemaName+'.'+@VNomTable
    exec (@ExecDeclareTable)
     
    ---- Update Table Temporaire ----
     
    declare @ColumnName varchar(50)
    declare @ExecUpdateTable varchar(8000)
     
    set @ColumnCount = (SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'TmpTable')
    while @ColumnCount > 0
    	begin
    		set @ColumnName = (SELECT (COL_NAME(OBJECT_ID('TmpTable'), @ColumnCount)))
     
    		set @ExecUpdateTable =
    		'update TmpTable
    		Set TmpTable.'+@ColumnName+' = ''null''
    		where TmpTable.'+@ColumnName+' is null
    		update TmpTable
    		set '+@ColumnName+' = REPLACE('+@ColumnName+','''''''','''''''''''')'
    		exec (@ExecUpdateTable)
     
    		Set @ColumnCount = @ColumnCount - 1
    	end
     
    ---- Travail sur Table Temporaire ----
     
    		declare @ListeSelect varchar(8000);
    		SET @ColumnCount = 0
    		SET @ListeColumn = ''
    		SET @ListeSelect = '''''''''+'
     
    		while (SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'TmpTable') > @ColumnCount
    			begin
    				SET @ColumnCount = @ColumnCount + 1
    						SET @ListeColumn = @ListeColumn + (SELECT (COL_NAME(OBJECT_ID('TmpTable'), @ColumnCount)))
    						SET @ListeSelect = @ListeSelect + 'convert (varchar(8000),'
    						SET @ListeSelect = @ListeSelect + (SELECT (COL_NAME(OBJECT_ID('TmpTable'), @ColumnCount)))
    							IF (SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'TmpTable') > @ColumnCount
    								begin
    									SET @ListeColumn = @ListeColumn + ','
    									SET @ListeSelect = @ListeSelect + ')'+'+'''''' , ''''''+'
    								end
    							IF (SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'TmpTable') = @ColumnCount
    								begin
    									SET @ListeSelect = @ListeSelect + ' )+'''''''''
    								end
    			end
     
    		declare @VSelect varchar (8000)
    		declare @VPrinting varchar (8000)
     
    		declare @TmpTable1 TABLE (
    		Column1 varchar(8000)
    		)
     
    		SET @VSelect = 'select '+@ListeSelect+' from TmpTable'
    		INSERT INTO @TmpTable1 (Column1)
    		exec (@VSelect)
     
    		update @TmpTable1
    		set Column1 = REPLACE(Column1,'''null''','null')
     
    		Set NoCount OFF
    		declare @NewFetch1 int
    		Declare curseur_print cursor FOR
    		SELECT Column1 FROM @TmpTable1
    		open curseur_print;
    		fetch next FROM curseur_print INTO @VPrinting
    		select @NewFetch1 = fetch_status from sys.dm_exec_cursors(0)
    		where name = 'curseur_print' and session_id = @@spid
    		Print '--------------------------------------------'
    		Print '--'
    		Print '--'
    		Print '-- Inserts de la Table '+@VNomTable
    		Print '--'
    		Print '--'
    		Print '--------------------------------------------'
    		Print ''
    		while (@NewFetch1 = 0)
    			begin
    				Print 'INSERT INTO ['+@VNomTable+'] ('+@ListeColumn+')'
    				Print 'VALUES ( '+@VPrinting+' )'
    				Print 'GO'
    				Print ' '
    				fetch next FROM curseur_print INTO @VPrinting
    				select @NewFetch1 = fetch_status from sys.dm_exec_cursors(0)
    				where name = 'curseur_print' and session_id = @@spid
    			end
    		close curseur_print;
    		deallocate curseur_print;
    		Set NoCount ON
    drop table TmpTable
     
    ---- Fin ----
     
    	End
    J'ai pas pu faire énorméments de test mais normalement vous pouvez récuperer le script d'insertion de n'importe quelle table avec ce script.

    J'ai aussi fais un script qui permet de récupérer l'ordre d'intégrité des tables d'une base de donné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
     
     
    ---- Créer une table temporaire de la liste des tables à insérer ----
     
    create table Tmp1Table (
    Niveau bigint,
    [name] varchar(8000),
    colonnes varchar(8000)
    )
     
    ---- Creer une table temporaire des tables insérées ----
     
    create table Tmp2Table (
    [name] varchar(8000),
    colonnes varchar(8000)
    )
     
    ---- insertion liste des tables N1 dans Tmp1Table ----
     
    insert into Tmp1Table
    select '1',t1.table_name,t1.column_name from (select * from information_schema.KEY_COLUMN_USAGE) t1,(select * from information_schema.table_constraints) t2
    where t1.constraint_name = t2.constraint_name
    and t1.table_name <> 'sysdiagrams'
    and t1.table_name not in (
    	select t1.table_name from (select * from information_schema.KEY_COLUMN_USAGE) t1,(select * from information_schema.table_constraints) t2
    	where t1.constraint_name = t2.constraint_name
    	and constraint_type = 'FOREIGN KEY'
    )
    order by table_name,constraint_type desc
     
    ---- insertion liste des tables NX dans Tmp2Table ----
     
    insert into Tmp2Table
    select t1.table_name,t1.column_name from (select * from information_schema.KEY_COLUMN_USAGE) t1,(select * from information_schema.table_constraints) t2
    where t1.constraint_name = t2.constraint_name
    and t1.table_name not in (
    	select t1.table_name from (select * from information_schema.KEY_COLUMN_USAGE) t1,(select * from information_schema.table_constraints) t2
    	where t1.constraint_name = t2.constraint_name
    	and t1.table_name not in (
    		select t1.table_name from (select * from information_schema.KEY_COLUMN_USAGE) t1,(select * from information_schema.table_constraints) t2
    		where t1.constraint_name = t2.constraint_name
    		and constraint_type = 'FOREIGN KEY'
    	)
    )
    order by table_name,constraint_type desc
     
    ---- Tables N2 ----
     
    declare @VNiveau smallint
    declare @ExecWhile varchar(8000)
    set @VNiveau = '2'
     
    while (select count(name) from Tmp2Table) > 0
    begin
    	set @ExecWhile =
    	'insert into Tmp1Table
    	select '+convert(varchar(3),@VNiveau)+',t1.name,t1.colonnes from
    	(
    		select name,colonnes from Tmp2Table
    		where colonnes in (select colonnes from Tmp1Table)
    		and name not in (
    			select name from Tmp2Table
    			where colonnes not in(
    				select colonnes from Tmp1Table
    			)
    		)
    		union
    		select name,colonnes from Tmp2Table
    		where name in (
    			select name from Tmp2Table
    			where colonnes not in(
    				select colonnes from Tmp1Table
    			)
    			group by name
    			having count(colonnes) = 1
    		)
    	) t1
     
    	delete from Tmp2Table
    	where name in (select name from Tmp1Table)'
    	exec (@ExecWhile)
    	set @VNiveau = @VNiveau + 1
    end
     
    ---- Select de la réponse ----
     
    select niveau,name from tmp1table
    group by niveau,name
     
    ---- Effacement des tables temporaires ----
     
    drop table Tmp1Table
    drop table Tmp2Table
    Et un petit mix de ces 2 scripts pour pouvoir récuperer les inserts d'une base de donnée dans l'ordre d'intégritée de la base. (vous devez creer la procédure qui permet de récuperer les insert d'une table avant de lancer ce script)

    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
     
    set nocount on
     
    ---- Créer une table temporaire de la liste des tables à insérer ----
     
    create table Tmp1Table (
    Niveau bigint,
    [name] varchar(8000),
    colonnes varchar(8000)
    )
     
    ---- Creer une table temporaire des tables insérées ----
     
    create table Tmp2Table (
    [name] varchar(8000),
    colonnes varchar(8000)
    )
     
    ---- insertion liste des tables N1 dans Tmp1Table ----
     
    insert into Tmp1Table
    select '1',t1.table_name,t1.column_name from (select * from information_schema.KEY_COLUMN_USAGE) t1,(select * from information_schema.table_constraints) t2
    where t1.constraint_name = t2.constraint_name
    and t1.table_name <> 'sysdiagrams'
    and t1.table_name not in (
    	select t1.table_name from (select * from information_schema.KEY_COLUMN_USAGE) t1,(select * from information_schema.table_constraints) t2
    	where t1.constraint_name = t2.constraint_name
    	and constraint_type = 'FOREIGN KEY'
    )
    order by table_name,constraint_type desc
     
    ---- insertion liste des tables NX dans Tmp2Table ----
     
    insert into Tmp2Table
    select t1.table_name,t1.column_name from (select * from information_schema.KEY_COLUMN_USAGE) t1,(select * from information_schema.table_constraints) t2
    where t1.constraint_name = t2.constraint_name
    and t1.table_name not in (
    	select t1.table_name from (select * from information_schema.KEY_COLUMN_USAGE) t1,(select * from information_schema.table_constraints) t2
    	where t1.constraint_name = t2.constraint_name
    	and t1.table_name not in (
    		select t1.table_name from (select * from information_schema.KEY_COLUMN_USAGE) t1,(select * from information_schema.table_constraints) t2
    		where t1.constraint_name = t2.constraint_name
    		and constraint_type = 'FOREIGN KEY'
    	)
    )
    order by table_name,constraint_type desc
     
    ---- Tables N2 ----
     
    declare @VNiveau smallint
    declare @ExecWhile varchar(8000)
    set @VNiveau = '2'
     
    while (select count(name) from Tmp2Table) > 0
    begin
    	set @ExecWhile =
    	'insert into Tmp1Table
    	select '+convert(varchar(3),@VNiveau)+',t1.name,t1.colonnes from
    	(
    		select name,colonnes from Tmp2Table
    		where colonnes in (select colonnes from Tmp1Table)
    		and name not in (
    			select name from Tmp2Table
    			where colonnes not in(
    				select colonnes from Tmp1Table
    			)
    		)
    		union
    		select name,colonnes from Tmp2Table
    		where name in (
    			select name from Tmp2Table
    			where colonnes not in(
    				select colonnes from Tmp1Table
    			)
    			group by name
    			having count(colonnes) = 1
    		)
    	) t1
     
    	delete from Tmp2Table
    	where name in (select name from Tmp1Table)'
    	exec (@ExecWhile)
    	set @VNiveau = @VNiveau + 1
    end
     
    ---- Récupération des Print ----
    set nocount off
     
    declare @VPrinting varchar (8000)
    declare @NewFetch int
    declare curseur_print1 cursor FOR
    		select name from Tmp1Table group by niveau,name order by niveau
    		open curseur_print1;
    		fetch next FROM curseur_print1 INTO @VPrinting
    		select @NewFetch = fetch_status from sys.dm_exec_cursors(0)
    		where name = 'curseur_print1' and session_id = @@spid
    		while (@NewFetch = 0)
    			begin
    				exec Print_Inserts_From @VPrinting
    				fetch next FROM curseur_print1 INTO @VPrinting
    				select @NewFetch = fetch_status from sys.dm_exec_cursors(0)
    				where name = 'curseur_print1' and session_id = @@spid
    			end
    		close curseur_print1;
    		deallocate curseur_print1;
    set nocount on
     
    ---- Effacement des tables temporaires ----
     
    drop table Tmp1Table
    drop table Tmp2Table

  5. #5
    Candidat au Club
    Profil pro
    Inscrit en
    Janvier 2009
    Messages
    5
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Janvier 2009
    Messages : 5
    Points : 2
    Points
    2
    Par défaut
    J'avais oublié de vous demander, si vous rencontrez des bugs lors de l'utilisation de ces scripts sur une base de données, je vous serez reconnaissant de me prévenir. Si vous pouvez aussi me faire parvenir un script de creation de base, ainsi qu'un script d'insertion de données afin que je puisse corriger mes scripts ...
    Mon adresse e-mail est : drzunway1@msn.com
    Merci

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