Solved

Sql 2008 Table duplication

Posted on 2011-09-14
10
163 Views
Last Modified: 2012-05-12
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!!

0
Comment
Question by:HDM
  • 7
  • 3
10 Comments
 
LVL 33

Expert Comment

by:knightEknight
Comment Utility
select contract_information_grower as Farm
into plantname
from plantname_foods_USA_INC_Foodinfo
where 1 = 0
0
 
LVL 33

Expert Comment

by:knightEknight
Comment Utility
The above will create the single-column table empty ... to create it with the actual data, remove the WHERE clause.
0
 
LVL 33

Expert Comment

by:knightEknight
Comment Utility
also, the above assumes that the table plantname doesn't exist yet.
0
 

Author Comment

by:HDM
Comment Utility
Will this create a "Live" link??

If the data in the first table changes will it reflect to the new table??
0
 
LVL 33

Expert Comment

by:knightEknight
Comment Utility
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
0
Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

 
LVL 33

Expert Comment

by:knightEknight
Comment Utility
... then you can do this:  select * from plantname
and it will reflect changes in the actual table plantname_foods_USA_INC_Foodinfo
0
 
LVL 33

Expert Comment

by:knightEknight
Comment Utility
... 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
0
 

Author Comment

by:HDM
Comment Utility
It will take a me a few to assimulate that info.

Thanks!!
0
 
LVL 33

Accepted Solution

by:
knightEknight earned 500 total points
Comment Utility
Take your time!  Here's more ...

From your perspective a view is really nothing more than a named query.  So you can include in a view pretty much anything you can include in a SELECT query ... this includes GROUP BY, MIN, MAX, COUNT, as well as table joins.  In your case, if you want to include additional columns in your simple view, that's easy enough (this is just an example)...

create view plantname  -- fyi - use alter instead of create to change an existing view
as
select T.contract_information_grower as Farm,  T.columnA as MyNewNameA,  T.columnB as MyNewNameB
from plantname_foods_USA_INC_Foodinfo T

GO

select * from plantname
0
 

Author Closing Comment

by:HDM
Comment Utility
SWEET!!  Found "Alias"  :-)

That fixes EVERYTHING.  :-)

Thanks SO much!!
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now