?
Solved

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

Posted on 2013-02-01
3
Medium Priority
?
193 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:Murray Brown
[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
3 Comments
 
LVL 18

Accepted Solution

by:
deighton earned 1000 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 1000 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:Murray Brown
ID: 38843310
Thanks for the help
0

Featured Post

Percona Live Europe 2017 | Sep 25 - 27, 2017

The Percona Live Open Source Database Conference Europe 2017 is the premier event for the diverse and active European open source database community, as well as businesses that develop and use open source database software.

Question has a verified solution.

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

For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
I have a large data set and a SSIS package. How can I load this file in multi threading?
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Suggested Courses

762 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