Solved

sql query insert

Posted on 2011-03-22
9
255 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:ewangoya
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
 
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
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 

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

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Join & Write a Comment

Suggested Solutions

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video discusses moving either the default database or any database to a new volume.

762 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now