Solved

SQL Insert statement using Join

Posted on 2008-10-14
3
10,586 Views
Last Modified: 2012-08-14
Hi all,

Let's say you have 2 tables (UserSecFunctions and UserInfo). The UserInfo table holds all UserIDs you need to insert into the UserSecFunctions along with some other minute data. How would this statement be written?

I would assume something like the folowing, but I know it's wrong:

--Insert Security Records
INSERT INTO UserSecFunctions UF Join UserInfo U on U.UserID = UF.UserID
(USERID,FUNCTIONNAME,CREATEDATE,CREATEUSER,MODIFYDATE,MODIFYUSER)
VALUES
(U.UserID,'LookupAccountManager',GetDate(),'ADMIN',GetDate(),'ADMIN')
WHERE (UF.FunctionName <> 'LookupAccountManager') AND (UF.UserID = '' or UF.UserID is null)

I hope by backwards logic makes sence. I really only need a way of getting the value 'LookupAccountManager' into UserSecFuctions for all the users in UserUnfo... Sounds real simple though as usual I like to complicate things...

Thanks in advance,

-RJ
0
Comment
Question by:netadmin2004
  • 2
3 Comments
 
LVL 39

Expert Comment

by:appari
ID: 22711082
try

INSERT INTO UserSecFunctions
(USERID,FUNCTIONNAME,CREATEDATE,CREATEUSER,MODIFYDATE,MODIFYUSER)
Select
U.UserID,'LookupAccountManager',GetDate(),'ADMIN',GetDate(),'ADMIN'
From
UserSecFunctions UF Join UserInfo U on U.UserID = UF.UserID
WHERE (UF.FunctionName <> 'LookupAccountManager') AND (UF.UserID = '' or UF.UserID is null)

0
 
LVL 39

Accepted Solution

by:
appari earned 500 total points
ID: 22711109
do you want to insert all users not existing in UserSecFunction if LookupAccountManager entry is not existing?
if it is so try this

try

INSERT INTO UserSecFunctions
(USERID,FUNCTIONNAME,CREATEDATE,CREATEUSER,MODIFYDATE,MODIFYUSER)
Select
U.UserID,'LookupAccountManager',GetDate(),'ADMIN',GetDate(),'ADMIN'
From
UserInfo U
WHERE
not exists (Select 1 from UserSecFunctions UF
Where UF.FunctionName = 'LookupAccountManager' AND UF.UserID = U.UserID )
0
 

Author Comment

by:netadmin2004
ID: 22711344
Awesome thank you!
0

Featured Post

Free Gift Card with Acronis Backup Purchase!

Backup any data in any location: local and remote systems, physical and virtual servers, private and public clouds, Macs and PCs, tablets and mobile devices, & more! For limited time only, buy any Acronis backup products and get a FREE Amazon/Best Buy gift card worth up to $200!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
How to query date ranges with SQL 6 26
Test a query 23 19
SQL - Join 2 Tables Based on Ranges 8 26
Authentication error 1 0
Introduced in Microsoft SQL Server 2005, the Copy Database Wizard (http://msdn.microsoft.com/en-us/library/ms188664.aspx) is useful in copying databases and associated objects between SQL instances; therefore, it is a good migration and upgrade tool…
I wrote this interesting script that really help me find jobs or procedures when working in a huge environment. I could I have written it as a Procedure but then I would have to have it on each machine or have a link to a server-related search that …
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

747 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

10 Experts available now in Live!

Get 1:1 Help Now