SQL Server/SQL statement database query across tables/or databases

Source: Internet
Author: User

Merge the data obtained from the two tables to form a dataset. 1. When two tables with different structures in the same database// Create table # TEM (FID int, username varchar (50), address varchar (50), fax varchar (50 )) go // insert data from Table T1 to the temporary table # TEM insert into # TEM (FID, username, address) select * from T1 insert into # TEM select * from T3 // insert T3 data to the temporary table # TEM

Select * from # TEM // get all data from the temporary table drop table # TEM // Delete the temporary table

2. Retrieve data sets from two tables with different structures under the same database and merge them into the same data set.Select * into # tempdt from T1 insert into # tempdt ([name], address) Select T3. [name], t3.address from T3 select * from # tempdt drop table # tempdt

3. Fetch data from two associated tables from different databasesSelect * from [DBO]. [database name]. [Table name] inner join [DOB]. [database name]. [Table name] On t1.id = t2.id Note: You must specify the DBO and database name here.

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.