Solved

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

Posted on 2011-03-22
7
314 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Guide to Performance: Optimization & Monitoring

Nowadays, monitoring is a mixture of tools, systems, and codes—making it a very complex process. And with this complexity, comes variables for failure. Get DZone’s new Guide to Performance to learn how to proactively find these variables and solve them before a disruption occurs.

 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
GeoClustering  and AOG 25 43
Need help in debugging a UDF results 7 54
Using this function 4 42
Can I skip a node in XML? 9 35
If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

733 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