Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
209 views
in Technique[技术] by (71.8m points)

sql - Generate script for both schema and data

I have a SQL Server database for which I want to generate script of data as well as schema both. I tried Tasks -> Generate Scripts -> Script all objects in the selected database but it does not give the .sql for the data itself.

How do we generate the .sql database for both data as well schema? Please suggest some easy method such as a tool or something that can be used easily

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Backing up/Exporting database

There are two ways to Back up/Export a SQL Server database using SQL Server Management Studio:

  1. Right click database → Tasks → Generate Scripts → Choose DB → Change “Script Data” option to true → …

  2. Right click database → Tasks → Backup → ...

The first method creates a .sql file that then we need to run. The problem with this method is that the .sql file can be too big to be opened with SQL Server Management Studio. In that case we need to use the sqlcmd utility (should be already installed if we have SQL Server MS). Instructions below.

The second method creates a .back file that is then easy to import into an empty database.

Importing Database

If we have a .sql file and it’s not too big we can just open it with SQL Server MS and run it.

If we have a .sql file but it’s too big to be opened with SQL Server MS we have to use sqlcmd like this:

>sqlcmd -i C:panels_QA28July11.sql -o C:PanelsImportResult.txt

The parameter after -i is the file to import. The parameter after -o is where to save the output. We can omit the second parameter if we want to see the process on the screen.

By default it will use the local machine and local database server. If we want to use a different machine and server we use the -S option.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...