Solved

sql select and insert from the same table

Posted on 2013-12-06
3
357 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

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

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

896 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now