Link to home
Start Free TrialLog in
Avatar of SmashAndGrab
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:

User generated image
Avatar of Pawan Kumar
Pawan Kumar
Flag of India image

Pls try-

INSERT INTO PrintLog (UserId)
SELECT UserId From 2sd_Users U
WHERE NOT EXISTS ( SELECT NULL FROM PrintLog  p WHERE p.UserId = U.UserId )
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

Open in new window

more specific..

INSERT INTO usrUsersPrintLog (UserID)
SELECT UserID FROM ZSD_RFUSERS 
where NOT EXISTS (Select NULL FROM usrUsersPrintLog WHERE ZSD_RFUSERS.UserID = usrUsersPrintLog.UserID)

Open in new window


Or

INSERT INTO usrUsersPrintLog (UserID)
SELECT UserID FROM ZSD_RFUSERS 
LEFT JOIN usrUsersPrintLog ON ZSD_RFUSERS.UserID = usrUsersPrintLog.UserID
WHERE usrUsersPrintLog.UserID IS NULL

Open in new window

SOLUTION
Avatar of Pawan Kumar
Pawan Kumar
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
@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.  :-)