Ejemplo n.º 1
0
 public static void CreateTable(SqlTable table, SyncScript syncScript)
 {
     syncScript.Add(new SyncAction
     {
         Name = table.QualifiedName,
         Text = table.HeaderCreateScript(),
         Type = SyncActionType.CreateTable
     });
     CreateTableSubObjects(table, syncScript);
 }
Ejemplo n.º 2
0
        private void ReCreateScript(SyncScript syncScript)
        {
            String newName     = $"{_dest.Name}_temp{( Guid.NewGuid().ToString("N") )}";
            String fullNewName = $"[{_dest.SchemaName}].[{newName}]";

            foreach (var key in _dest.Dependencies)
            {
                syncScript.Add(new SyncAction
                {
                    Name = key.Name,
                    Text = key.DropScript(),
                    Type = SyncActionType.DropFK
                });
            }

            DropTableSubObjects(_dest, syncScript);


            StringBuilder script = new StringBuilder();

            script.AppendLine($"EXEC sp_rename N'{_dest.QualifiedName}', N'{newName}' ");
            script.AppendLine(SqlStatement.GO);
            script.Append(_source.HeaderCreateScript());

            var columns = _source.Columns.Intersect(_dest.Columns, _columnNameComparer)
                          .Where(c => !c.IsComputed).Select(c => c.Name);

            String columnsText = String.Join(",", columns);

            if (_source.HasIdentity)
            {
                script.AppendLine($"SET IDENTITY_INSERT {_source.QualifiedName} ON");
            }
            script.AppendLine($"INSERT INTO {_dest.QualifiedName} ({columnsText}) ");
            script.AppendLine($"SELECT {columnsText} FROM {fullNewName}");
            if (_source.HasIdentity)
            {
                script.AppendLine($"SET IDENTITY_INSERT {_source.QualifiedName} OFF");
            }
            script.AppendLine(SqlStatement.GO);
            script.AppendLine($"DROP TABLE {fullNewName}");
            script.AppendLine(SqlStatement.GO);

            syncScript.Add(new SyncAction
            {
                Name = _source.QualifiedName,
                Text = script.ToString(),
                Type = SyncActionType.CreateTable
            });

            CreateTableSubObjects(_source, syncScript);

            foreach (var key in _dest.Dependencies.Intersect(_source.Dependencies, _depNameComparer))
            {
                syncScript.Add(new SyncAction
                {
                    Name = key.Name,
                    Text = key.CreateScript(true),
                    Type = SyncActionType.CreateFK
                });
            }
        }