Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

sql query insert

Posted on 2011-03-22
9
Medium Priority
?
264 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
  • 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
Veeam and MySQL: How to Perform Backup & Recovery

MySQL and the MariaDB variant are among the most used databases in Linux environments, and many critical applications support their data on them. Watch this recorded webinar to find out how Veeam Backup & Replication allows you to get consistent backups of MySQL databases.

 
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 2000 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
This month, Experts Exchange sat down with resident SQL expert, Jim Horn, for an in-depth look into the makings of a successful career in SQL.
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

916 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