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
| declare @i int
declare @j int
declare @Client varchar(500)
declare @numrows int
declare @numexo int
declare @request as varchar(max)
declare @LogicielDB as varchar(max)
set @LogicielDB = 'Client%'
drop table ListeClientDB
create table ListeClientDB (idx int Primary Key IDENTITY(1,1),name varchar(100), nbExo int)
DECLARE @Logiciel_table TABLE (
idx int Primary Key IDENTITY(1,1)
, Client_id nvarchar(128) )
-- Remplit @Logiciel_table avec toutes les DBs concernées par mon logiciel.
INSERT @Logiciel_table
select name from sys.databases where name like @LogicielDB
-- Ici on parcours chaque DB pour en filtrer certain dossier sur base de règle métier...
SET @i = 1
SET @numrows = (SELECT COUNT(Client_id) FROM @Logiciel_table)
IF @numrows > 0
WHILE (@i <= @numrows)
BEGIN
SET @Client = (SELECT Client_id FROM @Logiciel_table WHERE idx = @i)
select @request = 'insert into ListeClientDB select '''+@Client+''', 0 from ' + @Client + '.dbo.d_acctgeneral where generalid = ''112'' and wordingfr = ''Primes de fusion'''
--select @request = @request + @Client
exec(@request)
--select(@request)
-- increment counter for next Client
SET @i = @i + 1
END
-- Ici, on compte le nombre d'exercice sur chaque dossier (nombre de table ''A_%AParam'' ou % est le numéro de l'année)
SET @i = 1
SET @numrows = (SELECT COUNT(name) FROM ListeClientDB)
IF @numrows > 0
WHILE (@i <= @numrows)
BEGIN
SET @Client = (SELECT name FROM ListeClientDB WHERE idx = @i)
select @request = 'update ListeClientDB set nbExo = (select COUNT(T.name) from ' + @Client + '.sys.tables T where T.name like ''A_%AParam'') from ListeClientDB L where L.name = ''' + @Client + ''''
exec(@request)
-- increment counter for next Year
SET @i = @i + 1
END
-- Affichage pour le suivi
select * from ListeClientDB
-- Vidage puis copie des tables à copier du template
SET @i = 1
SET @numrows = (SELECT COUNT(name) FROM ListeClientDB)
IF @numrows > 0
WHILE (@i <= @numrows)
BEGIN
SET @Client = (SELECT name FROM ListeClientDB WHERE idx = @i)
select @request = 'delete ' + @Client + '..D_BilanCustom'
exec(@request)
select @request = ''
-- Parcours de chaque année comptable du client pour copié l'année concerné du template
SET @j = 0
SET @numexo = (SELECT nbExo FROM ListeClientDB where name = @Client)
IF @numexo > 0
WHILE (@j < @numexo)
BEGIN
select @request = @request + ' insert into ' + @Client + '..D_BilanCustom ([BilanID],[AcctingYear],...) select [BilanID],' + convert(varchar,@j) + ',... ' +
'from Client1813..D_BilanCustom B inner join ' + @Client + '..A_' + right('0'+convert(varchar,@j),2) + '_AParam A on 1 = 1
where B.AcctingYear = case when A.AcctingYear <= ''2013'' then 0 else 1 end'
-- Permet de diviser l'ajout des bilans pour avant 2014 et après 2014 (l'année 2013 = 0 et 2014 = 1 dans le dossier 1813)
-- increment counter for next Year
SET @j = @j + 1
END
exec(@request)
--select(@request)
-- increment counter for next Year
SET @i = @i + 1
END |
Partager