vbnetcoder
asked on
sql query insert
I have this query attached:
I am noticing that the following section never gets run
Select @Count2=COUNT(*) from Product_Categories WHERE Product_ID = @Product_ID
IF @Count2 = 0
BEGIN
INSERT INTO Product_Categories(Product _ID, Category_ID)
VALUES(@Product_ID, 0)
END
I am noticing that the following section never gets run
Select @Count2=COUNT(*) from Product_Categories WHERE Product_ID = @Product_ID
IF @Count2 = 0
BEGIN
INSERT INTO Product_Categories(Product
VALUES(@Product_ID, 0)
END
you can replace that with
IF NOT EXISTS( SELECT 1 FROM Product_Categories WHERE Product_ID = @Product_ID)
BEGIN
INSERT INTO Product_Categories(Product
VALUES(@Product_ID, 0)
END
ASKER
That does not make a differene .. the procedure is not being hit at all
PErhaps, somebody could give me the code where table Product_Categories is inserted so that product_ID = PRoduct_ID and category_ID = 0 WHERE there is a record in the PRoducts table but NOT a record in the PRoduct_Categories table
PErhaps, somebody could give me the code where table Product_Categories is inserted so that product_ID = PRoduct_ID and category_ID = 0 WHERE there is a record in the PRoducts table but NOT a record in the PRoduct_Categories table
I'm not quite sure what u mean by "never gets run"
--create table Product_Categories(Product_ID int, Category_ID int)
ALTER procedure ins_prod_cat
@Product_ID int = NULL
as
begin
declare @Count2 int
Select @Count2=COUNT(*) from Product_Categories WHERE Product_ID = @Product_ID
IF @Count2 = 0
BEGIN
INSERT INTO Product_Categories(Product_ID, Category_ID)
VALUES(@Product_ID, 0)
END
end
go
EXEC ins_prod_cat 2
go
select * from Product_Categories
ASKER
CoolBurn28:
I am expecting that if there is no record in product_cAtegory table that I will insert in the product_ID that was passed in and a 0 for category_ID
I am expecting that if there is no record in product_cAtegory table that I will insert in the product_ID that was passed in and a 0 for category_ID
ASKER
for some reason when the entire procedure (attached earlier) is run this does not happen..
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
ty
To test it insert first a value in product table that is not on product_categories table;
Next test the procedure by inserting the same value that you've inserted on product table
Not a good code but it will do the trick.. ;)
Next test the procedure by inserting the same value that you've inserted on product table
Not a good code but it will do the trick.. ;)
ASKER
Open in new window