SQL Server Data Tools - How do I preserve data along with schema? - sql-server-data-tools

I have been using a SQL Server project to manager the structure of a database.
First I created the project, then imported a database.
Then, when I need to make a schema change, say change a field name, I do it in the SQL Server Project, and then publish it to the actual database using the Schema Compare Option.
I would like to take this a step further to hold basic data that the database needs. Say for example a OrderType table with 2 records "PHONE", "WEB"
This data will be needed on all new instances of the database. Is there a way to keep these in the SQL Server project as well so that they don't get lost?
It seems the only way to do this now, is to keep an actual copy of the master database with the metadata in it, and then use a data-compare. But it would be great if the data could just get published at the same time as the schema so the resulting database is complete.

There are two ways to preserve static data and publish it with a database.
Have a "reference" database with static data populated. At the time of publishing a new instance, SQL Server Data tools has a "Data Compare" tool which allows you to compare to live databases, and creates a custom script to update one database with data from the other.
Create scripts that contain insert statements, and then run these scripts at publish time. SQL Server Data tools has two tools to assist in this.
a. Open the data table (right click on SQL Server object explorer, and select "View Data"), and then click on the "Script" button at the top. It will create an insert script for all rows in the table. More on Comparing Data from MSDN
b. Then take this created script, and add it to the Database Project as a "Post Deployment" script. When you create a publish script for the database, any Post Deployment scripts in the project are automatically included in the master script. More on post deployment scripts from MSDN

Related

Copy some entries from one server database to other server database using asp.net mvc

We are doing some CRUD operation in DEV environment and data is saved in database. For other environments(like Staging/Prod) we want to copy those records from DEV database and paste to Staging/Prod when required using asp.net MVC. Is it possible? Could you please suggest some pathway in order to accomplish this?
You can accomplish this using a linked server:
https://learn.microsoft.com/en-us/sql/relational-databases/linked-servers/create-linked-servers-sql-server-database-engine?view=sql-server-ver15
This involves creating a link between one database server and another (they must be able to see each other on the network). You can then reference the linked server like this:
INSERT INTO [LinkedServerName].[Database].[dbo].[Table] (......
Alternatively you could use a paid for tool like SSMS tools which can generate insert statements on a per table basis: https://www.ssmstoolspack.com/

how do I save my database tables and values?

I have an asp.net mvc website, and I would like to know if/how to save my database tables and values to my migrations/models so when I publish my website the first time it runs it creates the database on the iis sql server and fills the tables with the data? I am using sql server 2016 on my computer and on the server. right now it creates a default database on first run but no tables get created. I have just been making the database and tables manually when I publish, but I want to know if its possible to have it done automatically.

Copying Business Setup Data to a new instance

Re: AX 2012 R2 Service Pack 2
I am looking for options in copying Business Setup data (eg Companies, Chart of Accounts, Location, etc) from one AX instance to another instance. The instances are currently in development so we don't have to worry about wiping out data. As of now one single instance has a master copy of all Business Setup information. What I want to do is copy that data to multiple development instances.
I can refresh the Model Store and Business Data with no problem. But the Business Setup data is a different beast entirely. In AX 2009, there was a feature to copy this information, but it has since been removed in AX 2012. I've read multiple articles on this but each one warns that it's either not recommended or only for testing. I need something that is safe to use.
Has anyone found a clean process to copy business setups from one instance to another in AX 2012?
The really simple solution is to have an AX setup database/environment without any transactions.
To deploy do a SQL Server, do a backup of the setup database and restore to the target database followed by a full AX dictionary synchronize in the target application.
This will of cause wipe out any transactions, setup etc. done in the target database.
After import a script is run to change file locations not to point to setup locations etc.
You may need to save some data beforehand using the Administration, Periodic, Data Export/Import, Export then import after the wipe.
Below is the actual procedure used by one of my customers. The setup is done in the production environment and the target may be the flow testing environment.
Sketch list:
Backup setup
Export target data
Stop target AOS
Restore to target
Run SQL script
Start target AOS
Database synchronization
Run AX script
Import target data
This procedure is also great for making a test environment of a production environment.
There is a Microsoft document, "Microsoft Dynamics AX 2012: Tech Domain New Features Module 5: Moving Between Environments" on this subject (Google that entire title and it will be the first hit).
The procedure that Jan outlines in his answer is correct but more high level. There is data in several tables that store configuration information that you want to wipe out after the restore, and insert the rows that were there prior to the restore. I.e., there are some tables that you need to export before you do the restore, so that you can create SQL scripts to insert that data.
You can do it without these procedures, but then you have a messy environment with AOS servers listed that aren't in the environment, and if you have a reporting server, help server, etc., you have to go through and change all of that manually. If you save the data prior to the restore, then you have scripts that will get the configuration where it needs to be and it just takes one second.
You can copy commonly used values from an existing company to a new one, as long as both companies are in the same database. For example, if you have a standard list of symptom codes that is common to all your service management implementations, you can copy the codes easily from one company to another.
To copy data to a new company using RapidStart Services:
Open the new company.
Choose the Lightbulb that opens the Tell Me feature icon, enter Configuration Worksheet, and then choose the related link.
Choose the Copy Data from Company.
On the Copy Company Data page, choose a company to copy from in the Copy From field, and then choose the OK button.
Select a table from one of the configuration packages that you have imported, and then choose the Copy Data action.
This works for me with Microsoft

Is there anyway to "add" tables to existing database when using Code First?

I want to add a module build on Code First to a running website, which already has an database. If I set my module to run on separated database, it works fine.
But my friend want to just "add" tables created by my modules to existing database, without adding a new database.
Is there anyway to archive that?
The solution is simple and very easy:
Run the module to create a new, clean database
Use SQL Server to generate sql scripts that create tables of that database. I personally think the metadata table is not needed
Run those scripts against target database
Modify the connection string to point to target database
Now it runs flawless! I'm so happy
Thank you.

Importing data into new database configuration

I had a website, with an sql server database. I decided to create a new version of the site, so I downloaded the database + website onto my local dev PC, and added a whole bunch of stuff to both - in particular, I added lots of new stored procedures, columns and tables to the database, while leaving the existing data for the site in place while doing this.
It is now time to launch the new version. Of course, while working on the new version, the data in the database on the live site has changed - new users have signed up and so on, so I can't just push the dev enviroment database live, as this would lose data.
What is the best way to import all the data from the existing database into the new database configuration? Should I take the existing database, and then add all the columns, procs, tables, indexes and so on in to it, or is there a better way?
You can use SQL Compare or other comparison tools to make the production database look like your dev database. If budget is a concern you can see plenty of alternatives in this blog post.
In SQL Server Management Studio , right click on your local database -> Task -> Generate Scripts, and then you'll be able to select your SP/Functions and then execute these script against the production database

Resources