好吧,
所以我做了很多試驗和錯誤的,在這裏是我得到的。它似乎工作正常,雖然它聽起來相當大的東西,似乎容易在第一次。
整體這是法語和英語,因爲我們的客戶是法國人,所以我們無論如何。它確實在我需要的所有表格中插入每個日期。剩下的唯一東西就是我需要根據某些參數選擇插入日期的第一行,但這很容易。
DECLARE @IdProcessusRenouvellement bigint
DECLARE @NomProcessus nvarchar(255)
SELECT @IdProcessusRenouvellement = ID FROM T_Ref_Processus WHERE Nom LIKE 'EXP%'
SELECT @NomProcessus = Nom FROM T_Ref_Processus WHERE Nom LIKE 'EXP%'
DECLARE @InsertedSequence table(ID bigint)
DECLARE @Contrats table(ID bigint,IdClient bigint,NumeroContrat nvarchar(255))
INSERT INTO @Contrats SELECT ID,IdClient,NumeroContrat FROM T_ClientContrat
DECLARE @InsertedIdsSeq as Table(ID bigint)
-- Séquences de travail
INSERT INTO T_ClientContratSequenceTravail(IdClientContrat,Nom,DateDebut)
OUTPUT Inserted.ID INTO @InsertedIdsSeq
SELECT ID, @NomProcessus + ' - ' + CONVERT(VARCHAR(10), GETDATE(), 120) + ' : ' + NumeroContrat ,GETDATE()
FROM @Contrats
-- Processus
DECLARE @InsertedIdsPro as Table(ID bigint,IdProcessus bigint)
INSERT INTO T_ClientContratProcessus
(IdClientContratSequenceTravail,IdProcessus,Nom,DateDebut,DelaiRappel,DateRappel,LienAvecPro cessusRenouvellement,IdStatutProcessus,IdResponsable,Sequence)
OUTPUT Inserted.ID,Inserted.IdProcessus INTO @InsertedIdsPro
SELECT I.ID,P.ID,P.Nom,GETDATE(),P.DelaiRappel,GETDATE(),P.LienAvecProcessusRenouvellement,0,0,0
FROM @InsertedIdsSeq I, T_Ref_Processus P
WHERE P.ID = @IdProcessusRenouvellement
-- Étapes
DECLARE @InsertedIdsEt as table(ID bigint,IdProcessusEtape bigint)
INSERT INTO T_ClientContratProcessusEtape
(IdClientContratProcessus,IdProcessusEtape,Nom,DateDebut,DelaiRappel,DateRappel,NomListeVeri fication,Sequence,IdStatutEtape,IdResponsable,IdTypeResponsable,ListeVerificationTermine)
OUTPUT Inserted.ID,Inserted.IdProcessusEtape INTO @InsertedIdsEt
SELECT I.ID,E.ID,
E.Nom,GETDATE(),E.DelaiRappel,GETDATE(),COALESCE(L.Nom,''),E.Sequence,0,0,E.IdTypeResponsabl e,0
FROM @InsertedIdsPro I INNER JOIN T_Ref_ProcessusEtape E ON I.IdProcessus = E.IdProcessus
LEFT JOIN T_Ref_ListeVerification L ON E.IdListeVerification = L.ID
-- Étapes : Items de la liste de vérification
INSERT INTO T_ClientContratProcessusEtapeListeVerificationItem
(IdClientContratProcessusEtape,Nom,Requis,Verifie)
SELECT I.ID,IT.Nom,IT.Requis,0
FROM @InsertedIdsEt I
INNER JOIN T_Ref_ProcessusEtape E ON I.IdProcessusEtape = E.ID
INNER JOIN T_Ref_ListeVerificationItem IT ON E.IdListeVerification = IT.IdListeVerification
-- Tâches
DECLARE @InsertedIdsTa as table(ID bigint, IdProcessusEtapeTache bigint)
INSERT INTO T_ClientContratProcessusEtapeTache
(IdClientContratProcessusEtape,IdProcessusEtapeTache,Nom,DateDebut,DelaiRappel,DateRappel,No mListeVerification,Sequence,IdStatutTache,IdResponsable,IdTypeResponsable,ListeVerificationT ermine)
OUTPUT Inserted.ID,Inserted.IdProcessusEtapeTache INTO @InsertedIdsTa
SELECT I.ID,T.ID,
T.Nom,GETDATE(),T.DelaiRappel,GETDATE(),COALESCE(L.Nom,''),T.Sequence,0,0,T.IdTypeResponsabl e,0
FROM @InsertedIdsEt I
INNER JOIN T_Ref_ProcessusEtapeTache T ON I.IdProcessusEtape = T.IdProcessusEtape
LEFT JOIN T_Ref_ListeVerification L ON T.IdListeVerification = L.ID
-- Tâches : Items de la liste de vérification
INSERT INTO T_ClientContratProcessusEtapeTacheListeVerificationItem
(IdClientContratProcessusEtapeTache,Nom,Requis,Verifie)
SELECT I.ID,IT.Nom,IT.Requis,0
FROM @InsertedIdsTa I
INNER JOIN T_Ref_ProcessusEtapeTache T ON I.IdProcessusEtapeTache = T.ID
INNER JOIN T_Ref_ListeVerificationItem IT ON T.IdListeVerification = IT.IdListeVerification
您可以通過周圍用'SET IDENTITY_INSERT [表名] ON'和'SET IDENTITY_INSERT [表名] OFF'你'INSERT'語句插入自己的標識值。此外,可以將此結構簡化爲具有自引用外鍵的單個表,並且可以使用自連接遞歸地選擇數據。 – 2012-04-25 18:48:14
嗨,這可能看起來很漂亮,但這是因爲它是一個例子。實際圖表有6個表格。另外我不能使用我自己的身份,因爲我想能夠創建多個副本。我將用更多信息編輯我的帖子。 – 2012-04-25 18:53:57
以下是對同一問題的有趣解決方案,但它只適用於SQL Server 2008或更高版本:http://stackoverflow.com/questions/5365629 – Chad 2012-04-25 19:04:51