Click here to Skip to main content
15,887,776 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hello, i want to ask if i have 5 tables with same column names but different records,
can i add the records form all tables into 1 table and display it?

What I have tried:

i tried query builder but i didn't know , so some can plz help me with this or suggest anything
Posted
Updated 17-Apr-17 19:25pm
Comments
Karthik_Mahalingam 10-Apr-17 7:03am    
post the code
ZurdoDev 10-Apr-17 7:04am    
Yes, where are you stuck?

Quote: (sic)
i have 5 tables with same column names but different records,
can i add the records form all tables into 1 table and display it?
If you have 5 tables with the same column names but different records then what you do have is a very bad database design.

Revisit your database design.

For example: You might have some tables like [Student], [Teacher], [Parent],[CateringStaff] etc.
All of those tables will have things like [FirstName],[LastName],[DateOfBirth], etc etc. All the same.
The better design is to have a table called [Person] with columns like [FirstName],[LastName],[DateOfBirth], etc etc. plus an additional column [PersonType] which can link to another table
SQL
Create table [PersonType](Id int, PersonType nvarchar(125))
This latter table has a row for "Student", "Teacher", "Parent", "Caterer" etc etc.

If you want to get all the people on your database it is a simple one-table query :
SQL
SELECT * FROM Person
If you want to get all of the teachers it is another simple query:
SQL
SELECT * FROM Person P 
INNER JOIN PersonType PT on P.PersonType = PT.Id 
WHERE PT.PersonType = 'Teacher'
and so forth.

If you really, really want to keep your five tables (please don't do it that way) then you have no recourse other than to use UNION. However, you do not have to use a temporary table, and you absolutely do not need to use MERGE. UNION ALL does not remove duplicate rows but UNION will. So ALL you need is
SQL
SELECT * FROM Table1
UNION
SELECT * FROM Table2
UNION
SELECT * FROM Table3
UNION 
SELECT * FROM Table4
UNION 
SELECT * FROM Table5

(With the caveat that you should never use SELECT * in situations like this, list the columns you want explicitly)
 
Share this answer
 
Comments
Maciej Los 10-Apr-17 16:44pm    
5ed!
Learn.net37 11-Apr-17 2:46am    
thx sir, but i need to know what is the negative things to use the last method u mention to union or how this will affect the database ?
CHill60 11-Apr-17 4:53am    
Five tables with exactly the same columns is not a good design (see The Basics of Database Normalization[^]). This is especially true as you are trying to get the information from all 5 tables into a single recordset, which means that those rows belong to the same set of information, which means they should all be in the same table.
The last method I mentioned (UNION) has nothing negative about it IF you have those 5 tables that are identical. It won't affect the database. If you don't change your database then that is the query you will need to use.

By the way, if you want to create a single table you use that UNION query to do it using INSERT
Learn.net37 11-Apr-17 5:05am    
ok what if in another page i use one of those table to insert record using sqldatasource or edit , this will affect anything, or will the insert or edit item will appear in the union query ?
CHill60 11-Apr-17 5:09am    
Any items on any of the tables will appear in the query wherever they are added from - even if someone else adds them from somewhere else (if your database is multi-user). You may need to have some sort of refresh if you are already displaying the data in a UI control
--Craete one temp table.
IF OBJECT_ID('tempdb..#tmpTemp') IS NOT NULL
BEGIN
DROP TABLE #tmpTemp;
END

CREATE TABLE #tmpTemp (
ID INT identity(1, 1)
,YourColumn VARCHAR(200)
)

--- Insert all table records into this temp table
INSERT INTO #tmpTemp (YourColumn)
SELECT Column1
FROM FirstTable

UNION

SELECT Column1
FROM SecondTable

UNION

SELECT Column1
FROM ThirdTable

UNION

SELECT Column1
FROM FourthTable

--------------------
MERGE YourMainTable AS T
USING #tmpTemp AS S
ON (T.YourMainColumnName = S.YourColumn)
WHEN NOT MATCHED BY TARGET
THEN
INSERT (YourMainColumnName)
VALUES (S.YourColumn)
WHEN MATCHED
THEN
UPDATE
SET T.YourMainColumnName = S.YourColumn
OUTPUT $ACTION
,inserted.*;

Check this code and replace your tables.. :)
 
Share this answer
 
v2
Comments
CHill60 10-Apr-17 16:43pm    
Reason for my downvote: There is no need for a temporary table, if UNION is used instead of UNION ALL then duplicates will not appear in the final record set.
Ramesh Kumar Barik 10-Apr-17 21:51pm    
Dear Chill60,

Its fine. I just forgot to use Union instead of Union ALl.

Thnx.
There is no need for a temporary table
 
Share this answer
 
Comments
Maciej Los 18-Apr-17 2:32am    
This is not an answer. Please delete it to avoid downvoting. To post comment, please use "Have a Question or Comment" widget.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900