SmashAndGrab
asked on
SQL server 2008 (WHERE NOT EXIST)
hi,
I am having a few issues getting my SQL statement to work and I am unsure why as it looks ok to me?!?!
Here is the statement:
-- keep the usrprintlog table up to date
INSERT INTO usrUsersPrintLog (UserID)
Values (SELECT UserID FROM ZSD_RFUSERS where NOT EXISTS (Select ZSD_RFUSERS.UserID = usrUsersPrintLog.UserID))
So basically it should insert into 'usrUsersPrintLog' (just the UserID). any entries that are in 'ZSD_RFUSERS' that dont already exist in 'usrUsersPrintLog'.
I'm sure there is a clearer way for me to explain that!! haha
Here is the error:
I am having a few issues getting my SQL statement to work and I am unsure why as it looks ok to me?!?!
Here is the statement:
-- keep the usrprintlog table up to date
INSERT INTO usrUsersPrintLog (UserID)
Values (SELECT UserID FROM ZSD_RFUSERS where NOT EXISTS (Select ZSD_RFUSERS.UserID = usrUsersPrintLog.UserID))
So basically it should insert into 'usrUsersPrintLog' (just the UserID). any entries that are in 'ZSD_RFUSERS' that dont already exist in 'usrUsersPrintLog'.
I'm sure there is a clearer way for me to explain that!! haha
Here is the error:
or ..
INSERT INTO PrintLog (UserId)
SELECT UserId From 2sd_Users U LEFT JOIN PrintLog p ON p.UserId = U.UserId
WHERE p.p.UserId IS NULL
more specific..
Or
INSERT INTO usrUsersPrintLog (UserID)
SELECT UserID FROM ZSD_RFUSERS
where NOT EXISTS (Select NULL FROM usrUsersPrintLog WHERE ZSD_RFUSERS.UserID = usrUsersPrintLog.UserID)
Or
INSERT INTO usrUsersPrintLog (UserID)
SELECT UserID FROM ZSD_RFUSERS
LEFT JOIN usrUsersPrintLog ON ZSD_RFUSERS.UserID = usrUsersPrintLog.UserID
WHERE usrUsersPrintLog.UserID IS NULL
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
@Author - Did you faced any issues with the tested solution i gave. ?
I propose split between ID: 42264435 & ID: 42263181. Thanks
I don't mind splitting the points.
I am just having fun helping out. :-)
I am just having fun helping out. :-)
INSERT INTO PrintLog (UserId)
SELECT UserId From 2sd_Users U
WHERE NOT EXISTS ( SELECT NULL FROM PrintLog p WHERE p.UserId = U.UserId )