SSMS
 sql >> Base de données >  >> Database Tools >> SSMS

Générer des scripts avec de nouveaux identifiants (également pour les dépendances)

Quelque chose comme ça? J'utilise une variable de table pour "ajouter" une colonne à la table des formulaires d'origine.

DECLARE @Form1 UNIQUEIDENTIFIER=NEWID();
DECLARE @Form2 UNIQUEIDENTIFIER=NEWID();

DECLARE @tblForms TABLE(id UNIQUEIDENTIFIER,FormName VARCHAR(100));
INSERT INTO @tblForms VALUES(@Form1,'test1'),(@Form2,'test2');

DECLARE @tblFields TABLE(id UNIQUEIDENTIFIER,FormId UNIQUEIDENTIFIER,FieldName VARCHAR(100));
INSERT INTO @tblFields VALUES(NEWID(),@Form1,'test1.1'),(NEWID(),@Form1,'test1.2'),(NEWID(),@Form1,'test1.3')
                            ,(NEWID(),@Form2,'test2.1'),(NEWID(),@Form2,'test2.2'),(NEWID(),@Form2,'test2.3');

--These are "your original IDs"
SELECT frms.id,frms.FormName
      ,flds.id,flds.FieldName
FROM @tblForms AS frms
INNER JOIN @tblFields AS flds ON frms.id=flds.FormId ;                           

--Copy forms into a new table with an extra column
DECLARE @tblFormsNeu TABLE(id UNIQUEIDENTIFIER,FormName VARCHAR(100),myNewID UNIQUEIDENTIFIER);
INSERT INTO @tblFormsNeu
SELECT id,FormName,NEWID() FROM @tblForms;

SELECT frms.myNewID, frms.FormName
      ,NEWID() AS myNewFieldID,flds.FieldName
FROM @tblFormsNeu AS frms
INNER JOIN @tblFields AS flds ON frms.id=flds.FormId