Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

T-SQL to perform a specialized INSERT INTO

Posted on 2009-04-04
2
Medium Priority
?
237 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 1000 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

Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

Question has a verified solution.

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

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

661 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