Link to home
Start Free TrialLog in
Avatar of vbnetcoder
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            
Avatar of vbnetcoder
vbnetcoder

ASKER

attach entire procedure
USE [Store]
GO
/****** Object:  StoredProcedure [dbo].[spInsertProductCategory]    Script Date: 03/22/2011 07:52:42 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[spInsertProductCategory]
	-- Add the parameters for the stored procedure here
	@Product_ID int,
	@Category_Name nvarchar(500)
	

AS
BEGIN
	SET NOCOUNT ON;
	
		
	
	Declare @CategoryID int
	Declare @Countt int
	DECLARE @Count2 int

	SELECT @CategoryID=Category_ID FROM Categories WHERE Category_Name = @Category_Name
	Select @Countt=COUNT(Category_ID) from Product_Categories where Category_id  = @categoryID and Product_ID = @Product_ID		 
	
	IF  @Countt < 1 
		BEGIN
		INSERT INTO Product_Categories(Product_ID, Category_ID) 
		VALUES(@Product_ID, @CategoryID)
	END
		

	DELETE FROM Product_Categories
			WHERE Category_ID = 0
			  AND Product_ID in (
					 select Product_ID
					 from Product_Categories
					 group by Product_ID
					 having count(*) > 1)
					 
	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

Open in new window

Avatar of Ephraim Wangoya


you can replace that with
                         
     
      IF  NOT EXISTS( SELECT 1 FROM Product_Categories WHERE Product_ID = @Product_ID)
      BEGIN
            INSERT INTO Product_Categories(Product_ID, Category_ID)
            VALUES(@Product_ID, 0)
      END      
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
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

Open in new window

User generated image
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
for some reason when the entire procedure (attached earlier) is run  this does not happen..
ASKER CERTIFIED SOLUTION
Avatar of CoolBurn28
CoolBurn28

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.. ;)