Generate database script with structure and data

Asked

Viewed 3,342 times

0

Backup is not possible to do, because it depends on permission from France and etc. So, the solution we found here was to try to generate a script that copies not only the structure of the tables, I already do and did, but like an INSERT loading all the data for this script. Is that possible? I tried using Sql Server DTS, but it only copies structure.

  • Well the solution that I found here, was to generate the select of the main tables, save in csv and then at home, load into the tables the data. This was the solution found so far.

  • there are better solutions for this, I will post a

1 answer

7


A good solution to generate the structure of a database and import the data is to use the data itself Microsoft SQL Server Management Studio, he gives all the support for this.

  1. First let’s generate the script of the entire base structure.

Open the Microsoft SQL Server Management Studio, Connect to your base and right-click on it

Then go to Tasks -> Generate Script, according to the photo above, if it is in English.

Click on next.

Then choose what you want to export, all or only some specific things, in my case only have created tables, usually do not recommend recreating the permissions of users, since they will not always be on the new server and this will generate error.

Finally, just choose where you want to save the script. Normally I saved in a new SQL window.

After that just create your database with the generated script.

inserir a descrição da imagem aqui

  1. Now let’s import the data

Before starting this step it is good to disable all constraints of the database, so there are no problems when importing the data, because it does not check the dependencies of FK before, to know which table to import first, and this can give problems, then just activate again.

To disable the Constraints just run the script below:

EXEC sp_msforeachtable 'ALTER TABLE ? NOCHECK CONSTRAINT all' 

Right click on the database you created and then on "import" data.

Click "Next", after this goes to a screen for you to parameterize the data from the data source that you will import the information.

In your case in data source choose Sql Server Native Client, which in this case is a connection to an SQL Server database. On behalf of the server pass inform the server’s ip/dns, or what you use to connect to it. Fill in the authentication data and finally choose the database you will pull the information.

Now you go to the parameter setting screen of the target database, its parameterization and equal to the previous one, only in the case with the data of the target database, which in your case is the database created through your script.

Choose "Copy data from one or more tables or views" and "Next".

Select All Table and click "Edit Maps".

Leave the option "Enable identity insertion", this option will keep the identitys of the old database when you migrate the data, after that click "OK"

With all the tables you want to migrate the selected information click on "next" and then on "finish"

After imported data rehabilitates all constraints:

EXEC sp_msforeachtable 'ALTER TABLE ? CHECK CONSTRAINT all'

inserir a descrição da imagem aqui

  • Well, I guess I couldn’t express myself well. I am in the company, full of restrictions and I need to finish at home the change in sources and for that I need to have the bank working. Generate the table script and procs I’ve already done. Just need to take home the data and that’s where cow coughs. I needed to generate an Insert script with all the data in this Insert and I can’t do that. So, what I did, I ran the select of the main tables, took the result and exported to . csv and at home I import everything from the generated spreadsheets. This solution is the one I have at the moment.

  • @pnet It’s just vc create a base on your localdb the way I reported and create the backup of it and take it home

  • Jeferson, good morning. Backup here is forbidden, no user can create. You need permission from France and I don’t have time for that. Today ends my contract and only the change in the sources in VB6, what I need this backup to run at home. I’m doing with spreadsheet. I generated 15 spreadsheets, which are the ones I need. Everything that involves Backup and Download, it’s no use. I don’t know VB and its bone debug.

  • @pnet I am not talking to you to make a backup of the server and yes of your machine, install a sql server on your machine with localdb, there in it vc will be Syadmin and will be able to do everything on your machine, then it is only seg]uir the above steps to be a bank on your machine, after that just take backup of it

  • I got it, Jeferson, the company policy here is very tight, the consultants here have no mobility for anything. Nothing can be installed, backpeado. No download of any kind is allowed. I had to install a VM to run VB6, because on the machine I couldn’t install the Crystal version of the fonts. But your solution will remain for me as a study and other opportunities. I’ll mark your answer, but I’ll keep the spreadsheets or I’ll waste a couple of hours and assemble select insert... of all tables.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.