Solved

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

Posted on 2011-03-22
7
281 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 9

Expert Comment

by:Roman Gherman
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
THANK YOU everbody but this is the solution I used
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

by Mark Wills Attending one of Rob Farley's seminars the other day, I heard the phrase "The Accidental DBA" and fell in love with it. It got me thinking about the plight of the newcomer to SQL Server...  So if you are the accidental DBA, or, simp…
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 …
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

728 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now