Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7905193
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T10:23:10+00:00 2026-06-03T10:23:10+00:00

I have 2 SQL Server atabases A and B and want to copy a

  • 0

I have 2 SQL Server atabases A and B and want to copy a view from database A to database B using SQL only. B is used as history db, all the tables from A are also in B.

The existent views from A should be copied (re-created) into B in a way that they point to data in B.

The task is easy to solve using SQL Management Studio (e.g. here), but I need to be able to copy these views using my own program.

Where is the necessary information stored to generate the CREATE VIEW statements?

Could SELECT INTO or INSERT INTO provide a solution to my problem?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T10:23:12+00:00Added an answer on June 3, 2026 at 10:23 am
    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;
    

    If you want to create all views, then:

    USE DatabaseA;
    GO
    
    DECLARE @sql NVARCHAR(MAX);
    
    SET @sql = N'';
    
    SELECT @sql = @sql + CHAR(13) + CHAR(10) + 'GO'
        + CHAR(13) + CHAR(10) + m.definition
    FROM sys.sql_modules AS s
    INNER JOIN sys.objects AS o
    ON s.[object_id] = o.[object_id]
    WHERE o.type_desc = 'VIEW';
    
    EXEC DatabaseB..sp_executesql @sql;
    

    If you want this to be repeatable, you need to also drop each view first, if it already exists in the destination database. So for example:

    USE DatabaseA;
    GO
    
    DECLARE @sql NVARCHAR(MAX);
    
    SET @sql = N'';
    
    SELECT @sql = @sql + CHAR(13) + CHAR(10) + 'IF OBJECT_ID('''
        + QUOTENAME(SCHEMA_NAME(o.[schema_id]))
        + '.' + QUOTENAME(o.name) + ''') IS NOT NULL
          BEGIN
            DROP VIEW ' 
            + QUOTENAME(SCHEMA_NAME(o.[schema_id]))
            + '.' + QUOTENAME(o.name) + ';
          END'
        + CHAR(13) + CHAR(10) + 'GO'
        + CHAR(13) + CHAR(10) + s.definition
    FROM sys.sql_modules AS s
    INNER JOIN sys.objects AS o
    ON s.[object_id] = o.[object_id]
    WHERE o.type_desc = 'VIEW';
    
    EXEC DatabaseB..sp_executesql @sql;
    

    Now keep in mind that this will not create indexes if any of these are indexed views, it also doesn’t validate that the view can actually be created in the other database (unless you are positive that all the dependent objects are identical between the two databases), and doesn’t ensure the order of creation (in the case where ViewA calls ViewB, it may not create them in the correct order). Much better to use a schema comparison tool to automate this for you. I wrote about this here:

    http://bertrandaaron.wordpress.com/2012/04/20/re-blog-the-cost-of-reinventing-the-wheel/

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have sql server database with numerous tables, some no longer used so I
I have SQL Server 2008 Ent and OLTP database with two big tables. How
I have SQL Server databases and do changes in them. Some database tables have
I have SQL Server 2008 database with two tables. The first table is called
I have taken a backup from a database(Sql server 2008 R2).But in machine i
I have sql server database files (Mdf/Ldf) and i want to convert them to
I have: SQL Server 2008 Database Name: database1 I had taken backup from database1
I have SQL Server 2008 database with 2 tables: Table A has columns ID
I am using SQL Server 2008. I have some dates in my database that
I have SQL Server 2005 and all dates are stored using a DATETIME column

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.