Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Sql 2008 Table duplication

Posted on 2011-09-14
10
Medium Priority
?
170 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:Curtis Long
  • 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
Fill in the form and get your FREE NFR key NOW!

Veeam is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

 

Author Comment

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

Thanks!!
0
 
LVL 33

Accepted Solution

by:
knightEknight earned 2000 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:Curtis Long
ID: 36539722
SWEET!!  Found "Alias"  :-)

That fixes EVERYTHING.  :-)

Thanks SO much!!
0

Featured Post

Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

Question has a verified solution.

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

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
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 …
Integration Management Part 2
Screencast - Getting to Know the Pipeline

971 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