How to copy/recreate indexes, constraints, triggers etc in SQL server? - sql-server

Background:I needed to copy 2 tables from a backup to a production SQL Server database. Being new to SQL, I thought that I could just drop and insert into and it would work. So naive.
Is there any simple way to copy everything about the good tables (I restored them into a separate backup) into the tables I created in the production DB? I know how to view constraints using "right click on table - tasks - create script - create script using CREATE", but I don't know what to do with this information.

As far as I understood correctly, go to database and:
1. Script Table As -> Create to... Now you got your table with all indexes and other stuff which in the table.
2. Create these table in your new database
3. Copy your data from backup tables to the new.
You can do this with (tablock) for example. Before copying info drop constraints and indexes in new table and then copy your data.
Or without dropping any objects update your index and stats with ALTER
https://learn.microsoft.com/ru-ru/sql/t-sql/statements/alter-index-transact-sql?view=sql-server-2017

Related

How can I add columnstore index to existing tables in SQL Server?

I always use this method, which is "existing table right click script table as > clipboard" when I create a new table in SSMS. Today, I realized this method doesn't create columnstore index.
I realized that this method doesn't add "columnstore index" to script. Shortly, any table doesn't have csi. Therefore I have to add csi to existing tables in SQL Server. What is the fastest way to add csi to existing tables?

Delete database and drop tables SSIS

I have a scheduled SSIS package where it loads the data overnight into the data warehouse. Before loading, it drops the entire database and drops all the tables. But now I had a situation where I don't want to drop one table and want to do an incremental load using merge SQL statement. Because it is dropping the entire database, I won't be able to do that in the current scenario. If I change drop database to delete database, I think, I should be able to do incremental load on the table I want. Are there any possible complications of doing that. Can you foresee any problems if I change drop database to delete database, will I be missing something. Any thoughts highly appreciated. Thanks for your time.
As far as I know with delete database you only delete the rows whereas with drop database you delete all tables incl. the rows. If your logic works, you could do a delete database, then drop all tables except the one you want to keep.
A drop/delete of the database will remove all of the contents of the database. If the requirement is to retain a single table, you'll need to retain the schema and database that holds it as well.
If I'm understanding correctly, you're dropping the target database. Is this a STAGE database for the data warehouse? If so, you'll also have a TARGET (the main tables of the warehouse) that are loaded from STAGE. If this is the case, you should be able to run a MERGE statement from the newly STAGED table to the TARGET table.

Is it possible to move a schema to another database in SQL Server?

We have a legacy database that has dozens of schemas in it, and we're looking to split that database up into several smaller distinct databases instead.
Is there any way I can create a new database on the same physical server, and then transfer an entire schema over to the new database?
Our tables look like:
Foo.Table1
Foo.Table2
Foo.Table3
...
Bar.Table1
Bar.Table2
...
Xxx.Table1
Xxx.Table2
...
...and I want to move Foo.* to a new database.
Typically this is recommended to be done by some kind of per-table export/import, but that's quite cumbersome with the 150+ tables in the schema.
As far as my trivial research goes the options appear to be:
Export/import each table individually.
Backup the entire database, restore in a different destination and delete everything else (painful, since the entire database is ~900GB).
Deploy the dacpac of the single schema to the new database, and do a cross database initial seeding, aka:
INSERT INTO newDb.Foo.Table1 SELECT * FROM oldDb.Foo.Table1;
INSERT INTO newDb.Foo.Table2 SELECT * FROM oldDb.Foo.Table2;
INSERT INTO newDb.Foo.Table3 SELECT * FROM oldDb.Foo.Table3;
...
All of these options are a lot of effort... is there any other approach that will simply move an entire schema into a new database?
I am not aware of any fully automated way but this can be done relatively simply with the help of Excel.
In SSMS you can use "Object Explorer Details" to easily (with few mouse clicks) script schema of multiple tables.
With the help of system views (sys.tables, sys.columns etc.) and Excel you should be able to generate 'INSERT INTO .... SELECT ...' scripts for all of your tables in minutes.
In Excel (or a similar application) you paste the list of your tables (obtained using sys.tables) and then write a formula to generate a script for each table.
you can create a filegroup for each schema and move the tables of a schema into the related filegroup. after that you backup each filegroup and restore.

Database Diff Script without table drops

I need to move my teams database changes from our development environment to our test environment.
I know that Visual Studio can diff two databases and output a script. But for tables that we have added columns it is dropping the table and re-inserting in with the new columns.
It tries to keep the data, but it is not going to work. It will cause FK issues, and when I try to move this to production, I will lose all the statistics on the table.
Is there a way to get it to script the table with an alter script? (So that it alters the table to add the new column?)
I see this happen when columns are added to the middle of a table. If you're doing that, don't.

Is it possible to move the __MigrationHistory System table to a new server?

We recently moved our database to a new server. However, at the time, we did not allow the Code First migrations to create the database. We used another tool to migrate the tables and data. The __MigrationHistory table was not moved during this time. The __MigrationHistory is a system table in our original DB.
I cannot seem to find a way to import or export the __MigrationHistory table so we can allow future migrations to take place.
The only other thought we had, is to have the application recreate the database and migrate the copied data to the new version of the DB. The only issue is we have millions of records to move and it is quite a long process.
I use the following script to move the EF MigrationHistory table from the system tables to the user tables (of a database tree structure):
SELECT * INTO [dbo].[TempMigrationHistory] FROM [dbo].[__MigrationHistory];
DROP TABLE [dbo].[__MigrationHistory];
EXEC sp_rename 'TempMigrationHistory', '__MigrationHistory';
This way I can export the table by choosing standard script/export option under SSMS.
(The complete description of handling this issue is here)

Resources