SQL, Insertar datos con identity y constraint
Se ejecuta el siguiente query para generar el query de inserción de datos
-- --******************************************** CAMPOS *************************************** declare @ServerS as varchar(50) = 'srvbattanet' declare @BDataS as varchar(50) = 'battanet' declare @BData as varchar(100) = 'battanet' ----baasedatos destino declare @Tabla as varchar(200) = 'tipos' declare @Regs as varchar(600) = '(' DECLARE objects_cursor CURSOR LOCAL FAST_FORWARD READ_ONLY FOR SELECT c.name FROM sys.columns c JOIN sys.tables t ON c.object_id = t.object_id WHERE t.name = @Tabla DECLARE @object_name AS varchar(100) OPEN objects_cursor FETCH NEXT FROM objects_cursor into @object_name WHILE @@FETCH_STATUS = 0 BEGIN set @Regs = @Regs + @object_name + ',' FETCH NEXT FROM objects_cursor into @object_name end; CLOSE objects_cursor; DEALLOCATE objects_cursor; set @Regs = substring(@Regs,1,LEN(@Regs)-1) + ')' print '--ALTER TABLE '+ @BData + '.dbo.[' + @Tabla + '] NOCHECK CONSTRAINT ALL' print 'set identity_insert '+ @BData + '.dbo.[' + @Tabla + '] on' print 'insert into '+ @BData + '.dbo.[' + @Tabla + ']' + @Regs print 'SELECT T.* FROM ' + @ServerS + '.' + @BDataS + '.dbo.' + @Tabla + ' T WITH (NOLOCK)' print 'set identity_insert '+ @BData + '.dbo.[' + @Tabla + '] oFF' print '--ALTER TABLE '+ @BData + '.dbo.[' + @Tabla + '] CHECK CONSTRAINT ALL' --********************************************************************************************** --******************************************* EJEMPLO **************************************** ALTER TABLE battanet.dbo.[tipos] NOCHECK CONSTRAINT ALL set identity_insert battanet.dbo.[tipos] on insert into battanet.dbo.[tipos](id_tipo,tipo_grupo,tipo_subgrupo,nombre,descripcion,id_estatus) SELECT T.* FROM srvbattanet.battanet.dbo.tipos T WITH (NOLOCK) set identity_insert battanet.dbo.[tipos] oFF ALTER TABLE battanet.dbo.[tipos] CHECK CONSTRAINT ALL --Caracteristica: conectado a la base de datos donde se insertaran los datos y tener un linked server de donde se extraera la informacion}}