Solved

insert a record into a table where there IS a record in another

Posted on 2011-03-22
7
306 Views
Last Modified: 2012-05-11
I have two tables:

Products & Product_Categories

I want to INSERT a record into Product_Categories if there is not already one for that product_ID in the Product_categories table for each product_ID in the Products table.

Product_Categories field should have the product_ID and a value of 0 for Category_ID



0
Comment
Question by:vbnetcoder
7 Comments
 
LVL 6

Expert Comment

by:MuffyBunny
ID: 35190091
INSERT INTO Product_Categories(whatever fields are in this table) 
SELECT whatever fields you need for Product_Categories 
FROM Products p LEFT OUTER JOIN Product_Categories pc ON (p.productId = pc.productId) 
WHERE some_field_from_ProductCategories IS NULL

Open in new window

0
 
LVL 7

Expert Comment

by:TheTull
ID: 35190093
Have you tried adding a where clause like this:

where product_id not in (select distinct product_Id from product_categories)

0
 
LVL 4

Accepted Solution

by:
jimtpowers earned 500 total points
ID: 35190097
I have two tables:

Products & Product_Categories

I want to INSERT a record into Product_Categories if there is not already one for that product_ID in the Product_categories table for each product_ID in the Products table.

Product_Categories field should have the product_ID and a value of 0 for Category_ID

INSERT INTO Product_Categories (Category_ID, Product_ID)
SELECT '0',
p.Product_ID
FROM Products AS p
WHERE (p.Product_ID NOT IN (SELECT DISTINCT Product_ID FROM Product_Categories))
0
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 
LVL 9

Expert Comment

by:Roman Gherman
ID: 35190098
Hi,
Try this

INSERT INTO Product_Categories
(col1, col2)
SELECT
 col1,
 col2
FROM Products
WHERE
product_ID  NOT IN (SELECT product_ID FROM Product_Categories )
0
 
LVL 6

Expert Comment

by:MuffyBunny
ID: 35190130
Depending on the number of records you expect to be returned from Products, NOT IN is a really inefficient way to do that. At the very least you should use NOT EXISTS. The most efficient way is with a LEFT OUTER JOIN and then checking for NULL in the WHERE.
0
 
LVL 9

Expert Comment

by:gery128
ID: 35190144
you can use ADO.NET command with ExecuteScalar method of command object to check if the returning Category_ID is 0 or something other and if not then use another query with ExecuteNonQuery to insert the record.

like this:
cmd.CommandText = "select Category_ID from Product_Categories where Product_ID="@prodId";
int catId = (int)cmd.ExecuteScalar();
if(catId==0)
cmd.CommandText =@insert into Product_catergories...";
cmd.ExecuteNonQuery();


0
 

Author Closing Comment

by:vbnetcoder
ID: 35190396
THANK YOU everbody but this is the solution I used
0

Featured Post

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Hi all, It is important and often overlooked to understand “Database properties”. Often we see questions about "log files" or "where is the database" and one of the easiest ways to get general information about your database is to use “Database p…
In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question