Results 1 to 12 of 12

Thread: Copy DB to another DB

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2003
    Posts
    263

    Copy DB to another DB

    Hi All

    After a trawl around the net i just need some pointers. I have Local DB that i want to make an empty copy of on the same machine in Vb.NET. I am wondering if there are some SQL commands to achieve this?

    Regards

    Danny

  2. #2
    Frenzied Member Asgorath's Avatar
    Join Date
    Sep 2004
    Location
    Saturn
    Posts
    2,036

    Re: Copy DB to another DB

    Hi
    Just generate all sql scripts you need (tables,stored procedures,etc...) and run it on destination sql sever.

    Regards
    Jorge
    "The dark side clouds everything. Impossible to see the future is."

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2003
    Posts
    263

    Re: Copy DB to another DB

    Quote Originally Posted by Asgorath
    Hi
    Just generate all sql scripts you need (tables,stored procedures,etc...) and run it on destination sql sever.

    Regards
    Jorge
    In a SP how do i loop the SysObjects for the table names?

    Regards

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2003
    Posts
    263

    Re: Copy DB to another DB

    Im Nearly there, can anyone help i have an error near the .* on the Select INTO statement

    VB Code:
    1. --Create New DB
    2. CREATE DATABASE BufferDB
    3. ON
    4. (NAME = BufferDB, FILENAME = 'C:\Program Files\Microsoft SQL Server\MSSQL\data\BufferDB.mdf',
    5. SIZE = 4, MAXSIZE = 10, FILEGROWTH = 1)
    6.  
    7. --Loop the Tables
    8. DECLARE Source_Tables CURSOR FOR
    9. SELECT * FROM SysObjects WHERE [xType] = 'u' AND [NAME] <> 'dtProperties'
    10.  
    11. OPEN Source_Tables
    12.  
    13. BEGIN
    14. DECLARE @tablename VarChar
    15.   FETCH NEXT FROM Source_Tables INTO @tablename
    16.     WHILE (@@FETCH_STATUS = 0)
    17.  
    18.       --Copy table across to new buffer
    19.       SELECT @tablename.* INTO BufferDB.@tablename FROM @tablename
    20.  
    21.       --Delete all records
    22.       DELETE * FROM BufferDB.@tablename
    23.  
    24.     FETCH NEXT FROM Source_Tables INTO @tablename
    25.  
    26.   CLOSE Source_Tables
    27.  
    28.   DEALLOCATE Source_Tables
    29. END

  5. #5
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Copy DB to another DB

    Simply do a BACKUP on the server and RESTORE on another server - scripting does not get everything (permissions for example).

    Here's an example of making BACKUP and restoring to the SAME SERVER with a different DATABASE name.

    Code:
    BACKUP DATABASE Funds 
       TO DISK = 'd:\Program Files\Microsoft SQL Server\MSSQL\BACKUP\Funds_Copy.bak'
    RESTORE FILELISTONLY 
       FROM DISK = 'd:\Program Files\Microsoft SQL Server\MSSQL\BACKUP\Funds_Copy.bak'
    RESTORE DATABASE Funds_Copy
       FROM DISK = 'd:\Program Files\Microsoft SQL Server\MSSQL\BACKUP\Funds_Copy.bak'
       WITH MOVE 'Funds_data' TO 'd:\Program Files\Microsoft SQL Server\MSSQL\BACKUP\Funds_Copy.mdf',
       MOVE 'Funds_log' TO 'd:\Program Files\Microsoft SQL Server\MSSQL\BACKUP\Funds_Copy.ldf'
    GO

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2003
    Posts
    263

    Re: Copy DB to another DB

    Quote Originally Posted by szlamany
    Simply do a BACKUP on the server and RESTORE on another server - scripting does not get everything (permissions for example).

    Here's an example of making BACKUP and restoring to the SAME SERVER with a different DATABASE name.

    Code:
    BACKUP DATABASE Funds 
       TO DISK = 'd:\Program Files\Microsoft SQL Server\MSSQL\BACKUP\Funds_Copy.bak'
    RESTORE FILELISTONLY 
       FROM DISK = 'd:\Program Files\Microsoft SQL Server\MSSQL\BACKUP\Funds_Copy.bak'
    RESTORE DATABASE Funds_Copy
       FROM DISK = 'd:\Program Files\Microsoft SQL Server\MSSQL\BACKUP\Funds_Copy.bak'
       WITH MOVE 'Funds_data' TO 'd:\Program Files\Microsoft SQL Server\MSSQL\BACKUP\Funds_Copy.mdf',
       MOVE 'Funds_log' TO 'd:\Program Files\Microsoft SQL Server\MSSQL\BACKUP\Funds_Copy.ldf'
    GO
    Excellent, what about clearing out the Data?

    Also i get an Error saying 'Funds_data' doesn't exist! What is it?
    Last edited by dannywooly; Apr 6th, 2005 at 06:50 AM.

  7. #7
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Copy DB to another DB

    FUNDS is the name of my database...put your own name in the first BACKUP statement...

    FUNDS_COPY is the new name I chose for the database for when it gets restored...

    If you don't know the names of the DATA and LOG objects of your database, then this command will list them - but only after the BACKUP is done.

    Code:
    RESTORE FILELISTONLY 
       FROM DISK = 'd:\Program Files\Microsoft SQL Server\MSSQL\BACKUP\Funds_Copy.bak'
    Another way to know these names is to get into ENTERPRISE MANAGER and look at the DATABASE properties.

    Deleting the data is up to you - probably using a method like you have shown - looping through sysobjects cursor works - I've done that before to remove sensitive data from a DB we were giving to someone else.

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2003
    Posts
    263

    Re: Copy DB to another DB

    Cheers for the reply figure that bit out! For everyones info tho

    Code:
    BACKUP DATABASE YourDBName
       TO DISK = 'C:\Program Files\Microsoft SQL Server\MSSQL\BACKUP\CopiedDB.bak'
    RESTORE FILELISTONLY 
       FROM DISK = 'C:\Program Files\Microsoft SQL Server\MSSQL\BACKUP\CopiedDB.bak'
    RESTORE DATABASE CopiedDB
       FROM DISK = 'C:\Program Files\Microsoft SQL Server\MSSQL\BACKUP\CopiedDB.bak'
       WITH MOVE YourDBName_Data' TO 'C:\Program Files\Microsoft SQL Server\MSSQL\BACKUP\CopiedDB.mdf',
       MOVE YourDBName_Log' TO 'C:\Program Files\Microsoft SQL Server\MSSQL\BACKUP\CopiedDB.ldf'
    
    --Loop the Tables
    DECLARE @tablename SYSNAME
    DECLARE Source_Tables CURSOR FOR
    SELECT [Name] FROM SysObjects WHERE [xType] = 'u' AND [NAME] <> 'dtProperties'
    
    OPEN Source_Tables
    
    BEGIN
      FETCH NEXT FROM Source_Tables INTO @tablename
        WHILE (@@FETCH_STATUS = 0)
          --Delete all records
          EXECUTE ('DELETE * FROM ' + @tablename)  
        FETCH NEXT FROM Source_Tables INTO @tablename
      CLOSE Source_Tables
      
      DEALLOCATE Source_Tables
    END

  9. #9
    Frenzied Member Asgorath's Avatar
    Join Date
    Sep 2004
    Location
    Saturn
    Posts
    2,036

    Re: Copy DB to another DB

    You don't need to use a cursor
    Try this

    Code:
    Declare @sql NVarChar(4000)
    Select @sql= isnull(@sql,'') +'Delete From ' + Name + ';' From sysObjects Where xType='U'
    Exec sp_executesql @sql
    Regards
    Jorge
    "The dark side clouds everything. Impossible to see the future is."

  10. #10

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2003
    Posts
    263

    Re: Copy DB to another DB

    Quote Originally Posted by Asgorath
    You don't need to use a cursor
    Try this

    Code:
    Declare @sql NVarChar(4000)
    Select @sql= isnull(@sql,'') +'Delete From ' + Name + ';' From sysObjects Where xType='U'
    Exec sp_executesql @sql
    Regards
    Jorge
    LOL i spent 2 hours learning TSQL to do that loop, and you do it in 3 lines LOL Cheers mate

  11. #11
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Copy DB to another DB

    Quote Originally Posted by dannywooly
    LOL i spent 2 hours learning TSQL to do that loop, and you do it in 3 lines LOL Cheers mate
    In general CURSORS are considered not set-based logic and should be avoided.

    Then again, executing a dynamic SQL string is considered bad practice also.

    But in your case - needing to do "system" cleanup/DBA work means you can ignore those two rules - you could use the CURSOR or use the dynamic SQL - both fit the bill in this case.

    It's good to know both techniques - the SELECT @SQL=... "loop" that Asgorath posted is a very nice trick that can be used for all kinds of purposes.

    As far as I'm concerned, if I can take a mainframe BASIC program calculation and get it into T-SQL and it requires a cursor to pull it off, I've still made a huge leap towards getting all my business logic into the database.

    So, in the end, cursors are not so bad - just make sure they are required to pull of the end goal, and not that it's simply easier to think in the sequential processing model as opposed to the SQL set-based processing model.

    Have I talked too much yet?

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  12. #12

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2003
    Posts
    263

    Re: Copy DB to another DB

    Quote Originally Posted by szlamany
    In general CURSORS are considered not set-based logic and should be avoided.

    Then again, executing a dynamic SQL string is considered bad practice also.

    But in your case - needing to do "system" cleanup/DBA work means you can ignore those two rules - you could use the CURSOR or use the dynamic SQL - both fit the bill in this case.

    It's good to know both techniques - the SELECT @SQL=... "loop" that Asgorath posted is a very nice trick that can be used for all kinds of purposes.

    As far as I'm concerned, if I can take a mainframe BASIC program calculation and get it into T-SQL and it requires a cursor to pull it off, I've still made a huge leap towards getting all my business logic into the database.

    So, in the end, cursors are not so bad - just make sure they are required to pull of the end goal, and not that it's simply easier to think in the sequential processing model as opposed to the SQL set-based processing model.

    Have I talked too much yet?
    Its all good mate, im a learner and like new information, i have learnt loads this morning trying to fumble through these techniques!

    Cheers All

    have a good day

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width