Solved

sql query insert

Posted on 2011-03-22
9
261 Views
Last Modified: 2012-06-22
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            
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
  • 5
  • 3
9 Comments
 

Author Comment

by:vbnetcoder
ID: 35189609
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

0
 
LVL 32

Expert Comment

by:Ephraim Wangoya
ID: 35189656


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      
0
 

Author Comment

by:vbnetcoder
ID: 35189711
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
0
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 
LVL 2

Expert Comment

by:CoolBurn28
ID: 35189764
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

test scrnshot
0
 

Author Comment

by:vbnetcoder
ID: 35189796
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
0
 

Author Comment

by:vbnetcoder
ID: 35189804
for some reason when the entire procedure (attached earlier) is run  this does not happen..
0
 
LVL 2

Accepted Solution

by:
CoolBurn28 earned 500 total points
ID: 35189982
oww.. ic... ok well then lets base it in ur code if it makes u comfortable a traditional way of if else command.. cheers ;)
here it is
--create table Product_Categories(Product_ID int, Category_ID int)
-- create table Product(Product_ID int)
/* product_ID = PRoduct_ID and category_ID = 0 WHERE there is a record in the PRoducts table */
insert into Product values(4)
go
/* but NOT a record in the PRoduct_Categories table */
ALTER procedure ins_prod_cat
@Product_ID int = NULL
as
begin
declare @Count1 int
declare @Count2 int
set @Count1 = 0;set @Count1 = 0
Select @Count1=COUNT(*) from Product WHERE Product_ID = @Product_ID 
Select @Count2=COUNT(*) from Product_Categories WHERE Product_ID = @Product_ID                                      
      IF   (@Count1 > 0 )
      BEGIN
         IF  (@Count2 = 0) BEGIN
            INSERT INTO Product_Categories(Product_ID, Category_ID) 
            VALUES(@Product_ID, 0)
         END
      END 
end
go
EXEC ins_prod_cat 4
go
select * from Product
select * from Product_Categories

Open in new window

0
 

Author Closing Comment

by:vbnetcoder
ID: 35190014
ty
0
 
LVL 2

Expert Comment

by:CoolBurn28
ID: 35190046
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.. ;)
0

Featured Post

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

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

In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

623 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