I need a stored procedure to copy a table with indexes

Posted on 2010-01-06
Last Modified: 2012-05-08
I would like 2 session variable to be used if possible:

There are tables in DB1 that I need to copy to @DBname.
I want a procedure where I just have to modify @TableName and @DBname at the top of the procedure, and then Execute.
I then want the procedure to copy DB1.@TableName to @DBname.@TableName.
The only way I know to do this would be to create @DBname.@TableName using the CREATE STATEMENT from DB1.@TableName to preserve indexes/comments/other characteristics of the table.  Then use an 'insert' to copy the records.

In any event - can someone write me the procedure?

With Regards-
Question by:SAbboushi
    LVL 26

    Accepted Solution

    Try this...
    CREATE PROCEDURE uCopyTable(IN NewSchema VARCHAR(50),IN OldSchema VARCHAR(50),IN OldTable VARCHAR(50))
            SET @tmp_sql= CONCAT("CREATE TABLE ",NewSchema,'.',OldTable,' LIKE ',OldSchema,'.',OldTable); 
            PREPARE s1 FROM @tmp_sql; 
            EXECUTE s1; 
            DEALLOCATE PREPARE s1; 
            SET @tmp_sql= CONCAT("INSERT INTO ",NewSchema,'.',OldTable,' SELECT * FROM ',OldSchema,'.',OldTable); 
            PREPARE s1 FROM @tmp_sql; 
            EXECUTE s1; 
            DEALLOCATE PREPARE s1; 
    ### Usage...
    call uCopyTable('DBname','Db1','tableName')

    Open in new window


    Author Closing Comment


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Better Security Awareness With Threat Intelligence

    See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

    CCModeler offers a way to enter basic information like entities, attributes and relationships and export them as yEd or erviz diagram. It also can import existing Access or SQL Server tables with relationships.
    Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
    Video by: Steve
    Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now