Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. How to copy tables?

How to copy tables?

Scheduled Pinned Locked Moved C#
databasequestioncsharpsql-serversysadmin
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    ben2
    wrote on last edited by
    #1

    -------------------------------------------------------------------------------- Hi I have a basic question. I want to copy a table in a db to another db (in the destination the table has to be created first with proper columns). the table name and schema will remain same. (I'm using sql server 2000). Can you provide sample ado.net code, by which I can achieve that? Thanks

    T A 2 Replies Last reply
    0
    • B ben2

      -------------------------------------------------------------------------------- Hi I have a basic question. I want to copy a table in a db to another db (in the destination the table has to be created first with proper columns). the table name and schema will remain same. (I'm using sql server 2000). Can you provide sample ado.net code, by which I can achieve that? Thanks

      T Offline
      T Offline
      turbochimp
      wrote on last edited by
      #2

      This question belongs on the ADO/SQL board. Since I wrote the reply to it before stopping to consider that fact, I'll post it here, but in the future, try to keep the questions on-topic, please. Assuming your tables are on the same server instance (otherwise you'll have to further qualify the table objects, link servers etc.): INSERT INTO [new database name].[owner id].[new table name] SELECT * FROM [old database name].[owner id].[old table name] Example: INSERT INTO sampledatabase2.dbo.table1 SELECT * FROM sampledatabase1.dbo.table1 This example uses the '*' wildcard for brevity. You really should list out the columns in your own code. As for what code you might use, I would suggest using a SqlConnection and a SqlCommand (using the ExecuteNonQuery method of the SqlCommand to execute the above syntax). As an alternative to creating the schema by hand, and assuming you have appropriate permissions, you could also do something like: SELECT [old database name].[owner id].[old table name].* INTO [new database name].[owner id].[new table name] FROM [old database name].[owner id].[old table name] Example: SELECT sampledatabase2.dbo.table1.* INTO sampledatabase1.dbo.tablefoo FROM sampledatabase2.dbo.table1 but explicitly creating the schema, or using a script created from the original is safer.

      The most exciting phrase to hear in science, the one that heralds the most discoveries, is not 'Eureka!' ('I found it!') but 'That's funny...’

      B 1 Reply Last reply
      0
      • B ben2

        -------------------------------------------------------------------------------- Hi I have a basic question. I want to copy a table in a db to another db (in the destination the table has to be created first with proper columns). the table name and schema will remain same. (I'm using sql server 2000). Can you provide sample ado.net code, by which I can achieve that? Thanks

        A Offline
        A Offline
        Alex Korchemniy
        wrote on last edited by
        #3

        If you are doing a local replication task I would suggest using DTS or BCP. Alex Korchemniy

        1 Reply Last reply
        0
        • T turbochimp

          This question belongs on the ADO/SQL board. Since I wrote the reply to it before stopping to consider that fact, I'll post it here, but in the future, try to keep the questions on-topic, please. Assuming your tables are on the same server instance (otherwise you'll have to further qualify the table objects, link servers etc.): INSERT INTO [new database name].[owner id].[new table name] SELECT * FROM [old database name].[owner id].[old table name] Example: INSERT INTO sampledatabase2.dbo.table1 SELECT * FROM sampledatabase1.dbo.table1 This example uses the '*' wildcard for brevity. You really should list out the columns in your own code. As for what code you might use, I would suggest using a SqlConnection and a SqlCommand (using the ExecuteNonQuery method of the SqlCommand to execute the above syntax). As an alternative to creating the schema by hand, and assuming you have appropriate permissions, you could also do something like: SELECT [old database name].[owner id].[old table name].* INTO [new database name].[owner id].[new table name] FROM [old database name].[owner id].[old table name] Example: SELECT sampledatabase2.dbo.table1.* INTO sampledatabase1.dbo.tablefoo FROM sampledatabase2.dbo.table1 but explicitly creating the schema, or using a script created from the original is safer.

          The most exciting phrase to hear in science, the one that heralds the most discoveries, is not 'Eureka!' ('I found it!') but 'That's funny...’

          B Offline
          B Offline
          ben2
          wrote on last edited by
          #4

          This method is fine only when source and destination tables are in the same server. What if they are in different servers?

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups