kevman123
asked on
Conditional Insert Into Statement
I need to check if a record exists in a particular table and add it if not: i.e.
INSERT INTO CustomerInfo (CustomerID, FirstName)
VALUES (0,"NoSale"0
WHERE.....
I've tried a couple of Not exisits statements but I can't get it to work. Thanks in advance!
INSERT INTO CustomerInfo (CustomerID, FirstName)
VALUES (0,"NoSale"0
WHERE.....
I've tried a couple of Not exisits statements but I can't get it to work. Thanks in advance!
ASKER
How do I assign what CustomerTransactions is? It gives an error when I use what you entered.
The above is just an example. You say you want to see if a record exists in the table - are you looking for a particular customerID in that table? If so, which customerID?
ASKER
Yes, I have a generic CustomerID of 0 that I insert with FirstName = QuickSale. I want to check if the value is there, then add it if it's not. Thanks for your help!
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Yes, it is the primary key-- It errors out when I add it again. I would like to know how to conditionally add it through SQL.
You can't really, not in Access SQL (although you could with SQL Server using a stored procedure). If you are doing it in VBA code, you could use a lookup to see if the value already exists and then run the query if it doesn't.
ASKER
Ok thanks for the info!
INSERT INTO CustomerTransactions (CustomerID, FirstName)
SELECT CustomerID, "NoSale" FROM CustomerInfo
WHERE CustomerID NOT IN
(SELECT A.CustomerID FROM CustomerTransactions As A WHERE A.CustomerID=CustomerInfo.