Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 10613
  • Last Modified:

SQL Insert statement using Join

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
netadmin2004
Asked:
netadmin2004
  • 2
1 Solution
 
appariCommented:
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
 
appariCommented:
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
 
netadmin2004Author Commented:
Awesome thank you!
0

Featured Post

NEW Veeam Backup for Microsoft Office 365 1.5

With Office 365, it’s your data and your responsibility to protect it. NEW Veeam Backup for Microsoft Office 365 eliminates the risk of losing access to your Office 365 data.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now