Copy table data between databases in SQL Server

Source: Internet
Author: User
When using database development, data replication between database tables often imports data from some tables to each other. Of course, you can write programs for implementation. However, programs often require the development environment, which is inconvenient. It is most convenient to directly import data using the SQL language. It is both convenient and easy to modify. The following is the import method. 1. Table with the same structure and in the same database (for example, Table1, table2) SQL: insert into Table1 select * From Table2 (full copy) insert into Table1 select distinct * From Table2 (do not copy duplicate records) insert into Table1 select top 5 * From Table2 (first five records) 2. Not in the same database (for example, db1 Table1, DB2 table2) SQL: insert into db1 .. table1 select * From db2 .. table2 (full replication) insert into db1 .. table1 select distinct * From db2table2 (do not copy duplicate records) insert into tdb1 .. able1 select top 5 * From db2table2 (first five records) 3. table with different structures or copying some records (e.g., dn_user, dn_user2). create a new table [dn_usertemp] (add a column on the old table dn_user) Create Table [dn_usertemp] ([num] [numeric] (18, 0) Identity (1, 1) not null) [ID] [idtype] not null, [name] [fntype] not null, [descript] [dstype] Null, [logonnm] [idtype] not null, [Password] [idtype] Null, [gender] [char] (1) null, [quited] [booltype] not null, [offduty] [booltype] not null, [stopped] [booltype] not null, [osbind] [booltype] not null, [domain] [idtype] Null, [email] [fntype] Null, [unitid] [idtype] Null, [branchid] [idtype] Null, [dutyid] [idtype] Null, [levelid] [idtype] Null, [classid] [idtype] Null, [typeid] [idtype] Null, [IP] [varchar] (15) Collate chinese_prc_ci_as null, [expiredt] [datetime] Null, [sort] [int] not null, [allowdel] [booltype] not null, [unitchief] [booltype] not null, [branchchief] [booltype] not null, [unitdeputy] [booltype] not null, [branchdeputy] [booltype] not null, [num] [numeric] (18, 0) Identity (1, 1) not null) on [primary] B. insert dn_uer2 data into dn_usertempsql: insert into dn_usertemp select * From dn_user2c. insert dn_usertemp into dn_usersql: declare @ I intdeclare @ J intdeclare @ name fntypeset @ I = 1 select @ J = count (*) from dn_usertempwhile @ I <@ J 1 beginselect @ name = Name from dn_usertemp where num = @ iPrint @ nameinsert into dn_user (name) values (@ name) where num = @ iselect @ I = @ I 1end ----------------------------- creat -- change the database name to the name of the database to be copied.
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.