• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 337
  • Last Modified:

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

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
vbnetcoder
Asked:
vbnetcoder
1 Solution
 
MuffyBunnyCommented:
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
 
TheTullCommented:
Have you tried adding a where clause like this:

where product_id not in (select distinct product_Id from product_categories)

0
 
jimtpowersCommented:
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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
Roman GhermanCommented:
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
 
MuffyBunnyCommented:
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
 
gery128Commented:
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
 
vbnetcoderAuthor Commented:
THANK YOU everbody but this is the solution I used
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now