Solved

T-SQL to perform a specialized INSERT INTO

Posted on 2009-04-04
2
229 Views
Last Modified: 2012-05-06
I'm not too familiar with T-SQL and was wondering if I could get some help with an INSERT INTO statement..  

I have two tables, tblAssets and tblADComputers.  I am writing a job that creates entries in tblAssets for every computer in tblADComputers where my FK in tblAssets is set to tblADComputers.objectGUID

I understand you can do an INSERT INTO tblAssets (<fields>) SELECT <fields> FROM tblADComputers but there are a couple required fields for the new tblAssets entries that I need to set in the statement as well..  So I was hoping to make it look something like

INSERT INTO tblAssets (field1,field2) (field1value, (SELECT <field2> FROM tblADComputers))

Is this feasible?

Additionally, I don't want it to attempt to create duplicate entries, so if the objectGUID FK already exists in tblAssets, I don't want it to attempt to reinsert that objectGUID entry from tblADComputers.. I assume this would include some sort of WHERE NOT EXISTS clause but Im not sure how to put it together.  Any help with this would be greatly appreciated.
0
Comment
Question by:CantonIT
2 Comments
 
LVL 22

Assisted Solution

by:dportas
dportas earned 250 total points
ID: 24068023
INSERT INTO tblAssets (field1, field2)
SELECT 'field1value', C.field2
 FROM tblADComputers C
 LEFT JOIN tblAssets A
 ON C.field2 = A.field2
 AND A.field1 = 'field1value'
 WHERE A.field1 IS NULL ;
0
 

Accepted Solution

by:
CantonIT earned 0 total points
ID: 24068651
Didn't work..  However the following SQL snippet did.
INSERT INTO tblAssets 

     (assetModelID, ADLinked, ADObjectGUID, assetName, networkName)

SELECT 

     'bfae1bee-3ffd-4c59-8b4d-f2055b2dc4a2', 'true',  tblADComputers.objectGUID, 

     tblADComputers.dnsHostName, tblADComputers.dnsHostName

FROM tblADComputers

WHERE NOT EXISTS (

     SELECT 

          tblAssets.ADObjectGUID 

          FROM tblAssets 

          WHERE tblADComputers.objectGUID = tblAssets.ADObjectGUID

);

 

Open in new window

0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

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.
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 tutorial demonstrates a quick way of adding group price to multiple Magento products.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

705 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

18 Experts available now in Live!

Get 1:1 Help Now