Link to home
Start Free TrialLog in
Avatar of Curtis Long
Curtis LongFlag for United States of America

asked on

Sql 2008 Table duplication

If I wanted to create a table and make the columns equal the data from another table, can i do this??

Example:

I have a table called plantname_foods_USA_INC_Foodinfo
Under that table I have a column called contract_information_grower
The data in that column is:   grower 1

I want to create a table called plantname
a column called Farm
and data that equals the data from the contract_information_grower column in the plantname_foods_USA_INC_Foodinfo table.

Thanks!!

Avatar of knightEknight
knightEknight
Flag of United States of America image

select contract_information_grower as Farm
into plantname
from plantname_foods_USA_INC_Foodinfo
where 1 = 0
The above will create the single-column table empty ... to create it with the actual data, remove the WHERE clause.
also, the above assumes that the table plantname doesn't exist yet.
Avatar of Curtis Long

ASKER

Will this create a "Live" link??

If the data in the first table changes will it reflect to the new table??
no, what you are talking about is a view ... do this:

create view plantname
as
select contract_information_grower as Farm
from plantname_foods_USA_INC_Foodinfo
... then you can do this:  select * from plantname
and it will reflect changes in the actual table plantname_foods_USA_INC_Foodinfo
... and of course you can include additional columns (if you want) from the original table.

OR, if what you are looking for is all the distinct values from the original column (e.g. without repeats) then include the distinct keyword:

create view plantname
as
select distinct contract_information_grower as Farm
from plantname_foods_USA_INC_Foodinfo
It will take a me a few to assimulate that info.

Thanks!!
ASKER CERTIFIED SOLUTION
Avatar of knightEknight
knightEknight
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SWEET!!  Found "Alias"  :-)

That fixes EVERYTHING.  :-)

Thanks SO much!!