Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

NOT EXISTS Query 1

Status
Not open for further replies.

Jesi523

Programmer
Nov 26, 2010
3
US
I have a stored procedure where I need to insert from some other tables when those items do not exist in the original table. Here’s the stored procedure that I wrote.

ALTER PROCEDURE [dbo].[sp_CreateRequestedItems]

AS
BEGIN
INSERT INTO dbo.RequestItems
([request], [applicationProfile])

--select from requests and profiles table into requestitems table based on dept and job codes
SELECT Requests.rAuid,
Profiles.applicationProfile
FROM Requests
LEFT JOIN Profiles
ON Requests.departmentCode = Profiles.deptCode AND Requests.jobCode = Profiles.jobCode
WHERE NOT EXISTS (SELECT * FROM RequestItems WHERE request <> rAuid)
END

Here’s the error I am getting:
Cannot insert the value NULL into column 'applicationProfile', table 'UserAccess.dbo.RequestItems'; column does not allow nulls. INSERT fails.

The 2 columns I am trying to insert are foreign keys and they cannot be null true, but I don’t know what I am doing wrong. Please help.
 
I tried to change it to an = sign but I got the same error. :(
 
Why you're using LEFT JOIN with Profiles if you want to insert existing profiles only? Change LEFT JOIN to Inner JOIN and also add condition
AND Profiles.applicationProfile IS NOT NULL

PluralSight Learning Library
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top