Solved

T-SQL to perform a specialized INSERT INTO

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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
T-SQL Query to include null values 3 29
replicated - directional or bidirectional? 3 29
SYbase 4 27
SQL Server - Set Field Values ito Zero Based on Related Table 4 22
Naughty Me. While I was changing the database name from DB1 to DB_PROD1 (yep it's not real database name ^v^), I changed the database name and notified my application fellows that I did it. They turn on the application, and everything is working. A …
In this article I will describe the Backup & Restore 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.
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

786 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