Solved

sql select and insert from the same table

Posted on 2013-12-06
3
358 Views
Last Modified: 2013-12-07
HI,

I have the code below but it creates duplicates.
I want to be able to select the values from the mastercompany and insert it into the same table but with a different company id.

Would be grateful for any advice

Insert into tbl_M08_PageObjectLangContent
(pageObjLangContId, objectId, companyId, sysLanguageId, objectText, objectAlign, createdBy, createdDate, lastModifiedBy, lastModifiedDate)
(SELECT newid(), objectId, 'c6a21d21-a55a-4d50-a4a7-10c5a746460d', sysLanguageId, objectText, objectAlign, 'b81ff3cc-811f-49c8-ab22-9d9fc377d0f5', createdDate, 'b81ff3cc-811f-49c8-ab22-9d9fc377d0f5', lastModifiedDate
FROM tbl_M08_PageObjectLangContent
WHERE companyId = '141a9a54-a3ab-440f-91c1-38eb16310b26')
0
Comment
Question by:Soluga
3 Comments
 
LVL 9

Expert Comment

by:QuinnDex
ID: 39701542
instead of selecting companyID from the table, this is where you would add the new companyid that you want the record to have.. is the new  company id available in a table, if so select it from there
0
 
LVL 15

Accepted Solution

by:
JimFive earned 500 total points
ID: 39702152
I assume you mean that you want to add it if it doesn't already exist for the new company.

So you add
AND NOT EXISTS (SELECT * FROM tbl_M08_PageObjectLangContent t2 WHERE tbl_M08_PageObjectLangContent.objectid = t2.objectID
AND t2.companyID =  'c6a21d21-a55a-4d50-a4a7-10c5a746460d')

Open in new window

0
 
LVL 1

Author Closing Comment

by:Soluga
ID: 39702958
That's great thanks
0

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Suggested Solutions

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Via a live example, show how to shrink a transaction log file down to a reasonable size.

776 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