Solved

SQL Populate a table with its own data but change a field

Posted on 2013-02-01
3
180 Views
Last Modified: 2013-02-01
Hi

I have a table Table1 that contains a CompanyID column.
What SQL would I use to Select * From the Table and Add it back to the
table with a different Company ID
0
Comment
Question by:murbro
3 Comments
 
LVL 18

Accepted Solution

by:
deighton earned 250 total points
ID: 38842969
you should perform an update normally

UPDATE Table1 SET CompanyId = YourNewCompanyId

e.g

UPDATE Table1 SET CompanyId = 56


if you only want to change certain records, you can use a WHERE clause with that

e.g

UPDATE Table1 SET CompanyId = 56 WHERE CompanyId  = 99
0
 
LVL 23

Assisted Solution

by:Ioannis Paraskevopoulos
Ioannis Paraskevopoulos earned 250 total points
ID: 38842981
Hi,

If your intention is to add the same data for another company then do the following

DECLARE @NewCompanyID AS INT
SET @NewCompanyID = 2 --or whatever is your new company id
INSERT INTO Table1
SELECT @NewCompanyID,
            Col2,
            Col3,
            .
            .
            .
            Coln
FROM   Table1
WHERE  CompanyId = 1 --provided that 1 is the company id that it is already in that table

Open in new window


Giannis
0
 

Author Closing Comment

by:murbro
ID: 38843310
Thanks for the help
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

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…
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

770 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