Solved

Sql 2008 Table duplication

Posted on 2011-09-14
10
165 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
ID: 36539370
select contract_information_grower as Farm
into plantname
from plantname_foods_USA_INC_Foodinfo
where 1 = 0
0
 
LVL 33

Expert Comment

by:knightEknight
ID: 36539379
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
ID: 36539383
also, the above assumes that the table plantname doesn't exist yet.
0
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 

Author Comment

by:HDM
ID: 36539399
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
ID: 36539406
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
 
LVL 33

Expert Comment

by:knightEknight
ID: 36539407
... 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
ID: 36539416
... 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
ID: 36539454
It will take a me a few to assimulate that info.

Thanks!!
0
 
LVL 33

Accepted Solution

by:
knightEknight earned 500 total points
ID: 36539493
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
ID: 36539722
SWEET!!  Found "Alias"  :-)

That fixes EVERYTHING.  :-)

Thanks SO much!!
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
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…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

803 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