dbTalk Databases Forums  

Appending tables

comp.databases.ms-sqlserver comp.databases.ms-sqlserver


Discuss Appending tables in the comp.databases.ms-sqlserver forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Shwetabh
 
Posts: n/a

Default Appending tables - 03-08-2006 , 01:38 AM






Hi,
I wanted to know if it is possible to do to append two tables into a
third table.
For example, consider these two tables

Table 1
--------------------------------------------------------------
Quote:
Part_num | Prt_name | Desc1 | Desc2 |
--------------------------------------------------------------
PRT1 | PartA | abc | xyz |
PRT2 | PartB | def | aaa |
PRT3 | PartC | ghi | bbb |
--------------------------------------------------------------

Table 2
---------------------------------------------------------------
Quote:
Cat_num | Cat_name | SDsc1 | SDsc2 |
---------------------------------------------------------------
CAT1 | CatalogA | abc | xyz |
CAT2 | CatalogB | def | aaa |
CAT3 | CatalogC | ghi | bbb |
---------------------------------------------------------------


Now, I want to append them to get this :


Table 3

-----------------------------------------------------------------------------------------------------------------------------
Quote:
Part_num | Prt_name | Desc1 | Desc2 | Cat_num | Cat_name |
SDsc1 | SDsc2 |
-----------------------------------------------------------------------------------------------------------------------------
Quote:
PRT1 | PartA | abc | xyz |

PRT2 | PartB | def | aaa |

PRT3 | PartC | ghi | bbb |

| | | | CAT1
CatalogA | abc | xyz |
| | | | CAT2
CatalogB | def | aaa |
| | | | CAT3
CatalogC | ghi | bbb |
----------------------------------------------------------------------------------------------------------------------------


The blanks in Table 3 are , well ,blank.

Now can it be done or not?

Awaiting your replies,
Regards,
Shwetabh



Reply With Quote
  #2  
Old   
David Portas
 
Posts: n/a

Default Re: Appending tables - 03-08-2006 , 04:09 AM






Shwetabh wrote:
Quote:
Hi,
I wanted to know if it is possible to do to append two tables into a
third table.
For example, consider these two tables

Table 1
--------------------------------------------------------------
| Part_num | Prt_name | Desc1 | Desc2 |
--------------------------------------------------------------
| PRT1 | PartA | abc | xyz |
| PRT2 | PartB | def | aaa |
| PRT3 | PartC | ghi | bbb |
--------------------------------------------------------------

Table 2
---------------------------------------------------------------
| Cat_num | Cat_name | SDsc1 | SDsc2 |
---------------------------------------------------------------
| CAT1 | CatalogA | abc | xyz |
| CAT2 | CatalogB | def | aaa |
| CAT3 | CatalogC | ghi | bbb |
---------------------------------------------------------------


Now, I want to append them to get this :


Table 3

-----------------------------------------------------------------------------------------------------------------------------
| Part_num | Prt_name | Desc1 | Desc2 | Cat_num | Cat_name |
SDsc1 | SDsc2 |
-----------------------------------------------------------------------------------------------------------------------------
| PRT1 | PartA | abc | xyz |
|
| PRT2 | PartB | def | aaa |
|
| PRT3 | PartC | ghi | bbb |
|
| | | | | CAT1
| CatalogA | abc | xyz |
| | | | | CAT2
| CatalogB | def | aaa |
| | | | | CAT3
| CatalogC | ghi | bbb |
----------------------------------------------------------------------------------------------------------------------------


The blanks in Table 3 are , well ,blank.

Now can it be done or not?

Awaiting your replies,
Regards,
Shwetabh
My browser isn't displaying your Table 3 very well so I'm not quite
certain which data is going into which column. In general you can merge
tables like this using UNION:

SELECT part_num, prt_name, desc1, ...
FROM Table1
UNION ALL
SELECT NULL, NULL, cat_name, ...
FROM Table2 ;

Each SELECT list in the UNION has to have the same number of columns
and each column has to be made up of compatible datatypes. In your case
it seems like the big question is what will be the key of Table3? It
isn't clear to me whether it has a key at all.

--
David Portas, SQL Server MVP

Whenever possible please post enough code to reproduce your problem.
Including CREATE TABLE and INSERT statements usually helps.
State what version of SQL Server you are using and specify the content
of any error messages.

SQL Server Books Online:
http://msdn2.microsoft.com/library/m...S,SQL.90).aspx
--



Reply With Quote
  #3  
Old   
Erland Sommarskog
 
Posts: n/a

Default Re: Appending tables - 03-08-2006 , 04:21 AM



Shwetabh (shwetabhgoel (AT) gmail (DOT) com) writes:
Quote:
Table 3

--------------------------------------------------------------------------
---------------------------------------------------
| Part_num | Prt_name | Desc1 | Desc2 | Cat_num | Cat_name |
SDsc1 | SDsc2 |
--------------------------------------------------------------------------
---------------------------------------------------
| PRT1 | PartA | abc | xyz |
|
| PRT2 | PartB | def | aaa |
|
| PRT3 | PartC | ghi | bbb |
|
| | | | | CAT1
| CatalogA | abc | xyz |
| | | | | CAT2
| CatalogB | def | aaa |
| | | | | CAT3
| CatalogC | ghi | bbb |
--------------------------------------------------------------------------
--------------------------------------------------


The blanks in Table 3 are , well ,blank.

Now can it be done or not?
Judging from the sample data you posted, what you want is

SELECT a.Part_num, a.Prt_name, a.Desc1, a.Desc2, b.Cat_num,
b.Cat_name, b.SDsc1, b.SDcs2
FROM table1 a
JOIN table2 b ON a.Desc1 = b.SDsc1
AND b.Desc2 = b.SDcs2

But I cannot say that it make much sense to join over a description column.

Maybe you need to consider a little more what you are actually looking
for and what you want to achieve.

--
Erland Sommarskog, SQL Server MVP, esquel (AT) sommarskog (DOT) se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx


Reply With Quote
  #4  
Old   
Shwetabh
 
Posts: n/a

Default Re: Appending tables - 03-08-2006 , 04:31 AM



Hi,
it seems Table 3 got pretty messed up.
So I will give the schema definations of the tables here:

Table 1:

CREATE TABLE TABLE1
(
PART_NUM varchar(10) primary key,
PRT_NAME VARCHAR(10),
DESC1 VARCHAR(20),
DESC2 VARCHAR(20)
)

Table 2:

CREATE TABLE TABLE2
(
PART_NUM varchar(10) primary key,
CAT_NUM VARCHAR(10),
CAT_NAME VARCHAR(10),
SDESC1 VARCHAR(20),
SDESC2 VARCHAR(20)
)

Now the resultant table should have the following schema:

CREATE TABLE TABLE3
(
PART_NUM varchar(10) primary key,
PRT_NAME VARCHAR(10),
DESC1 VARCHAR(20),
DESC2 VARCHAR(20),
CAT_NUM VARCHAR(10),
CAT_NAME VARCHAR(10),
SDESC1 VARCHAR(20),
SDESC2 VARCHAR(20)
)

This schema will be created programmatically.

Now my question is, if it is possible, how can I
insert records from table1 and table2 in table3?
I hope I have now made the things clearer.

Awaiting your reply,
Regards,
Shwetabh


Reply With Quote
  #5  
Old   
David Portas
 
Posts: n/a

Default Re: Appending tables - 03-08-2006 , 04:47 AM



Shwetabh wrote:
Quote:
Hi,
it seems Table 3 got pretty messed up.
So I will give the schema definations of the tables here:

Table 1:

CREATE TABLE TABLE1
(
PART_NUM varchar(10) primary key,
PRT_NAME VARCHAR(10),
DESC1 VARCHAR(20),
DESC2 VARCHAR(20)
)

Table 2:

CREATE TABLE TABLE2
(
PART_NUM varchar(10) primary key,
CAT_NUM VARCHAR(10),
CAT_NAME VARCHAR(10),
SDESC1 VARCHAR(20),
SDESC2 VARCHAR(20)
)

Now the resultant table should have the following schema:

CREATE TABLE TABLE3
(
PART_NUM varchar(10) primary key,
PRT_NAME VARCHAR(10),
DESC1 VARCHAR(20),
DESC2 VARCHAR(20),
CAT_NUM VARCHAR(10),
CAT_NAME VARCHAR(10),
SDESC1 VARCHAR(20),
SDESC2 VARCHAR(20)
)

This schema will be created programmatically.

Now my question is, if it is possible, how can I
insert records from table1 and table2 in table3?
I hope I have now made the things clearer.

Awaiting your reply,
Regards,
Shwetabh
It looks like you'll want something like this:

INSERT INTO Table3
(part_num, prt_name, desc1, desc2,
cat_num, cat_name, sdesc1, sdesc2)
SELECT COALESCE(T1.part_num, T2.part_num),
T1.prt_name, T1.desc1, T1.desc2,
T2.cat_num, T2.cat_name, T2.sdesc1, T2.sdesc2
FROM Table1 AS T1
FULL JOIN Table2 AS T2
ON T1.part_num = T2.part_num ;

It still seems at least questionable whether Table3 or even Table2
represent "good" designs but as I only have your column names to go on
there isn't much point in me speculating about that.

--
David Portas, SQL Server MVP

Whenever possible please post enough code to reproduce your problem.
Including CREATE TABLE and INSERT statements usually helps.
State what version of SQL Server you are using and specify the content
of any error messages.

SQL Server Books Online:
http://msdn2.microsoft.com/library/m...S,SQL.90).aspx
--



Reply With Quote
  #6  
Old   
Shwetabh
 
Posts: n/a

Default Re: Appending tables - 03-08-2006 , 05:33 AM



Hi,
Well I cant really do anything about the design for table2 because that
is the way the client wants it to be.

As far as table3 goes, I wanted to check the possiblility of appending
the
records of all the tables into a single table and check the performance
and
efficiency. I understand that table3 is a poor database design
but the main motivation for doing this is to check the reaction of the
application
which will using such database.

Also, till now, I have converted DBASE database into SQL database using

OPENROWSET to import the data, I was wondering if the same database
can be entered into a new table which can hold the data from all
tables.

Any ideas?


Reply With Quote
  #7  
Old   
Shwetabh
 
Posts: n/a

Default Re: Appending tables - 03-08-2006 , 05:34 AM



Hi,
Well I cant really do anything about the design for table2 because that
is the way the client wants it to be.

As far as table3 goes, I wanted to check the possiblility of appending
the
records of all the tables into a single table and check the performance
and
efficiency. I understand that table3 is a poor database design
but the main motivation for doing this is to check the reaction of the
application
which will using such database.

Also, till now, I have converted DBASE database into SQL database using

OPENROWSET to import the data, I was wondering if the same database
can be entered into a new table which can hold the data from all
tables.

Any ideas?

Regards,
Shwetabh


P.S: If you want I can mail you the code I have written in VB for this
purpose.


Reply With Quote
  #8  
Old   
Doug
 
Posts: n/a

Default Re: Appending tables - 03-08-2006 , 04:38 PM



In table one, there are rows or records. Take the "first one."

Now in table two there are a bunch of rows or records.

how do we know which record or records from the second to table to
combine with the first row of the first table?

In other words, if there are 100 rows in the first table, and 10 rows
in the second, how many rows are you expecting in the third table?


Reply With Quote
  #9  
Old   
Erland Sommarskog
 
Posts: n/a

Default Re: Appending tables - 03-08-2006 , 04:45 PM



Shwetabh (shwetabhgoel (AT) gmail (DOT) com) writes:
Quote:
Well I cant really do anything about the design for table2 because that
is the way the client wants it to be.

As far as table3 goes, I wanted to check the possiblility of appending
the
records of all the tables into a single table and check the performance
and
efficiency. I understand that table3 is a poor database design
but the main motivation for doing this is to check the reaction of the
application
which will using such database.

Also, till now, I have converted DBASE database into SQL database using

OPENROWSET to import the data, I was wondering if the same database
can be entered into a new table which can hold the data from all
tables.

Any ideas?
For it to be meaningful to merge table1 and table2 into one table,
there must be some relation between the data. Is there any such relation?

In your sample data PRT1 went with CAT1, but you did not indicate what
rule said that these two should go together.

If you don't know what you want, we will not know either.

--
Erland Sommarskog, SQL Server MVP, esquel (AT) sommarskog (DOT) se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx


Reply With Quote
  #10  
Old   
Shwetabh
 
Posts: n/a

Default Re: Appending tables - 03-08-2006 , 10:03 PM




Erland Sommarskog wrote:
Quote:
Shwetabh (shwetabhgoel (AT) gmail (DOT) com) writes:
Well I cant really do anything about the design for table2 because that
is the way the client wants it to be.

As far as table3 goes, I wanted to check the possiblility of appending
the
records of all the tables into a single table and check the performance
and
efficiency. I understand that table3 is a poor database design
but the main motivation for doing this is to check the reaction of the
application
which will using such database.

Also, till now, I have converted DBASE database into SQL database using

OPENROWSET to import the data, I was wondering if the same database
can be entered into a new table which can hold the data from all
tables.

Any ideas?

For it to be meaningful to merge table1 and table2 into one table,
there must be some relation between the data. Is there any such relation?

In your sample data PRT1 went with CAT1, but you did not indicate what
rule said that these two should go together.

If you don't know what you want, we will not know either.

--
Erland Sommarskog, SQL Server MVP, esquel (AT) sommarskog (DOT) se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
Hi,

Please disregard the sample data for now. I had missed a field in table
2 there.
Let's look at the schema I have given for table 1,2,3. Here, I am able
to create
table 3 programmatically and it successfully creates all the columns in
table 1
and table 2.

As for the rules, let me make it clearer. The application (EasyLabel
from www.tharo.com) will be using this database. This application is
used to create labels. Each label consists of various components like
barcodes,images etc. The data for this label is mapped to the fields in
the database. The user enters the Part_num (which is unique for all
records) and the software retrieves the fields *which* are mapped from
the database and loads them in the label. In case there are some other
fields which are not mapped to the database, the application does not
care about them. In other words, they are as good as blank.

Now, what I have in mind is to create a table such that all Database is
stored in it. Since
the application will load only those fields into the labels which are
mapped, it wouldnt create a problem.
Now if I have 100 records in table 1 and 10 records in table 2, the
table 3 should have 110 records at the end of operation. This table
alone will be accessed by the application to do its work.
My question is how to do this? I mean how can I take the records from
table 1, put them in table 3, then take the records from table 2 and
put them in table 3 , and so on?

Awaiting replies,
Regards,
Shwetabh



Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.