Friday, February 24, 2012

Dynamic Table Creation

Hi
I wish to create a table through sql code which picks up fields from
different tables in the database.
Lets suppose I have TableA with fields 'W' and 'X'. TableB has fields 'Y'
and 'Z'. I want to create a TableC with fields 'W', 'X', 'Y' and 'Z'.
Is this possible? Any help is greatly appreciated. Thanks!
MJTry,
select a.w, a.x, b.y, b.z
into tablec
from tablaA as a inner join tableb as b on 0 = 1
This will not create constraints. You have to alter tablc and add them
manually.
AMB
"MJ" wrote:

> Hi
> I wish to create a table through sql code which picks up fields from
> different tables in the database.
> Lets suppose I have TableA with fields 'W' and 'X'. TableB has fields 'Y'
> and 'Z'. I want to create a TableC with fields 'W', 'X', 'Y' and 'Z'.
> Is this possible? Any help is greatly appreciated. Thanks!
> MJ|||MJ,
In order to give a query for this, you need to provide the rule
that explains when to put TableA.W and TableA.X in the same
output row as TableB.Y and TableB.Z.
For example, say TableA is PeoplePhoneNumbers, and has columns
(Name,Phone), and TableB is CarLicenses with tables (VINnumber, License),
how do you match up name,phone pairs with VINnumber, license pairs?
Even if you don't care how things are matched up, you still need to decide
on a specific rule to follow.
Steve Kass
Drew University
MJ wrote:

>Hi
>I wish to create a table through sql code which picks up fields from
>different tables in the database.
>Lets suppose I have TableA with fields 'W' and 'X'. TableB has fields 'Y'
>and 'Z'. I want to create a TableC with fields 'W', 'X', 'Y' and 'Z'.
>Is this possible? Any help is greatly appreciated. Thanks!
>MJ
>|||Hi yes using AMB's code it worked fine. I dont really care how they match
up and I think the constaint 0 = 1 allows for that.
Thanks both you guys!
MJ
"Steve Kass" wrote:

> MJ,
> In order to give a query for this, you need to provide the rule
> that explains when to put TableA.W and TableA.X in the same
> output row as TableB.Y and TableB.Z.
> For example, say TableA is PeoplePhoneNumbers, and has columns
> (Name,Phone), and TableB is CarLicenses with tables (VINnumber, License),
> how do you match up name,phone pairs with VINnumber, license pairs?
> Even if you don't care how things are matched up, you still need to deci
de
> on a specific rule to follow.
> Steve Kass
> Drew University
> MJ wrote:
>
>|||Columns are not fields; rows are not records. This is basic.
It sounds like you want to create a table on the fly, after the data
model is implemented. Surely not! That would mean that you have no
data model yet and should not have implemented a schema. You can
kludge it, but yu can also learn to be a good SQL programmer instead.|||Ah - I was under the assumption you wanted to put data into the
table, not just create an empty table. Alejandro's suggestion is
an excellent one for what you want.
SK
MJ wrote:
>Hi yes using AMB's code it worked fine. I dont really care how they match
>up and I think the constaint 0 = 1 allows for that.
>Thanks both you guys!
>MJ
>"Steve Kass" wrote:
>
>

No comments:

Post a Comment