Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

sql query insert

Posted on 2011-03-22
9
Medium Priority
?
263 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
Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

 
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

Will your db performance match your db growth?

In Percona’s white paper “Performance at Scale: Keeping Your Database on Its Toes,” we take a high-level approach to what you need to think about when planning for database scalability.

Question has a verified solution.

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

SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
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…

670 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