Solved

Sql 2008 Table duplication

Posted on 2011-09-14
10
167 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

 

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

After restoring a Microsoft SQL Server database (.bak) from backup or attaching .mdf file, you may run into "Error '15023' User or role already exists in the current database" when you use the "User Mapping" SQL Management Studio functionality to al…
In this article I will describe the Detach & Attach 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.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

738 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