How to copy views from one database to another dat

2019-01-22 13:00发布

I have two databases with same structure in MS SQL server.

I'd like to copy all views another database.

I tried to use Export data functionality by DTS (that works with the table objects).

But that executes the SQL & creates the table object.

I don't want to execute that just want to copy the view so that I can open them in design view.

I tried to use create new view in destination database & copy SQL query of the view of the source database & save the view. That works works exactly same that I want, But I have number of views & number of copies!

5条回答
叼着烟拽天下
2楼-- · 2019-01-22 13:16

Right click the database, choose Tasks, and then Generate Script. This will allow you to generate a single script containing all views in the database.

查看更多
兄弟一词,经得起流年.
3楼-- · 2019-01-22 13:17

Right click on your database and say Tasks->Generate scripts. SQL Server Management Studio is able to generate the CREATE scripts for you.

Then you simple copy this script and execute it on the target server/database.

查看更多
Animai°情兽
4楼-- · 2019-01-22 13:22

simple code to copy one view

USE DatabaseA;
GO

DECLARE @sql NVARCHAR(MAX);

SELECT @sql = definition
FROM sys.sql_modules
WHERE [object_id] = OBJECT_ID('dbo.ViewName');

EXEC DatabaseB..sp_executesql @sql;
查看更多
▲ chillily
5楼-- · 2019-01-22 13:28

If you have access to Visual Studio and have a database project type, you can 1) Import all the ddl, views and tables included 2) Easily add these to integrated source control 3) Migrate whole or part to new database

After the initial creation of a database project, you will be prompted for connection to SQL Server instance and a database name. When done importing, the ddl for the entire database will be available in a tree very similar to SSMS tree but with the DDL files rather than the objects from which it was derived.

查看更多
够拽才男人
6楼-- · 2019-01-22 13:40

I know this is a VERY late answer, however i think this might prove usefull for some (if you do not have a gui like sql server management studio)

select * 
from INFORMATION_SCHEMA.VIEWS

here you get a column named "view_definition" in sql server, (this works on databases from other vendors too)

查看更多
登录 后发表回答