I have two tables for e.g. tblStudents and tblSchools. Both are having different columns. For a reason I need to merge the two tables into one temporary table.
For example “tblStudents” contains the following design.
StudentID | FirstName | LastName | Address | DOB |
Where as “tblSchools” contains the following design.
SchoolID | Name | Address | IsSecodarySchool |
Now, you would like to merge this two table columns into one temporary table then you can use the following sql script.
SELECT * INTO #tbl_Temp
FROM tblStudent, tblSchool where 1>2
Here, I have mentioned the “where” clause because I wanted to copy only the columns and not the data. You can ignore this clause if you want to copy the entire data too.
You can add as many tables you want to merge by separating with comma (,) between the table names.
You can add specific columns from specific tables by using table alias and column name combination.
For example
For example
SELECT tblStudent.FirstName,tblStudent.LastName,tblSchool.Name INTO #tbl_Temp
FROM tblStudent, tblSchool where 1>2
Happy Programming!
Comments
Post a Comment