Solved

T-SQL to perform a specialized INSERT INTO

Posted on 2009-04-04
2
234 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
[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
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

Business Impact of IT Communications

What are the business impacts of how well businesses communicate during an IT incident? Targeting, speed, and transparency all matter. Find out more in this infographic.

Question has a verified solution.

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

'Between' is such a common word we rarely think about it but in SQL it has a very specific definition we should be aware of. While most database vendors will have their own unique phrases to describe it (see references at end) the concept in common …
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…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

734 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