SSMS
 sql >> Base de Dados >  >> Database Tools >> SSMS

Gere scripts com novos ids (também para dependências)


Algo assim? Eu uso uma variável com valor de tabela para "adicionar" uma coluna à tabela de formulários original.
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