Showing posts with label products. Show all posts
Showing posts with label products. Show all posts

Friday, March 30, 2012

IF exists UPDATE ELSE INSERT problem

Hi,

I have a 'Products' table (with: 'uid' and 'CatName' columns) and 'ProductCategory' table (with: 'uid', 'ProductID', 'CategoryID' columns).

I got stored procedure below to update or insert new row to 'ProductCategory' table whenever 'Products' table has been updated or new products has been added to it.

Update part works just fine but when new row has been added to 'Products' this storedProc dosn't insert it into 'ProductCategory' table, it does that only when 'ProductCategory' table is empty, I'm afraid it's because first column 'uid' in 'ProductCategory' table is an Identity column... I'm not sure how should I go about that problem. This is my stored procedure:


DECLARE @.CatNo INT, @.CatName varchar(10)
SET @.CatNo = 2
SET @.CatName = 'bracket'

IF exists (SELECT ProductID from ProductCategory, Products where ProductCategory.ProductID = Products.uid and Products.CatName = @.CatName )
BEGIN
UPDATE ProductCategory SET CategoryID = @.CatNo
FROM Products WHERE Products.CatName = @.CatName and ProductCategory.ProductID = Products.uid
END
ELSE
BEGIN
INSERT INTO ProductCategory ( ProductID, CategoryID)
SELECT uid, @.CatNo FROM Products
WHERE Products.CatName = @.CatName
END

SET @.CatNo = 3
SET @.CatName = 'cable'

IF exists (SELECT ProductID from ProductCategory, Products where ProductCategory.ProductID = Products.uid and Products.CatName = @.CatName )
BEGIN
UPDATE ProductCategory SET CategoryID = @.CatNo
FROM Products WHERE Products.CatName = @.CatName and ProductCategory.ProductID = Products.uid
END
ELSE
BEGIN
INSERT INTO ProductCategory ( ProductID, CategoryID)
SELECT uid, @.CatNo FROM Products
WHERE Products.CatName = @.CatName
END
(... Goes for another 37 categories)

Thank you for help.

KoobaWhat kind of error message returned?
Check if there are unique index? or referencial integratiies ?

If you insert Identity column, make sure turn on IDENTITY_INSERT.|||Do you actually have that exact same code 39 times? That's not good. You can cut out all of that excess code with a few smart statements and a table with your category names.

Example:

Create a table:
Cat (CatNo, CatName)

Data:
(2, 'bracket')
(3, 'cable')
etc...

with your 39 cats. Then you can get rid of all those tedious repeated SQL statements with just 2 SQL statements:


update ProductCategory
set CategoryID = Cat.CatNo
from Products
join ProductCategory
on ProductCategory.ProductID = Products.uid
join Cat
on Products.CatName = Cat.CatName

insert into ProductCategory
(ProductID,
CategoryID)
select uid,
Cat.CatNo
from Products
join Cat
on Products.CatName = Cat.CatName
left join ProductCategory
on ProductCategory.ProductID = Products.uid
where ProductCategory.ProductID is null


This 2 statements will do exactly the same as your 78.|||Looking at your question again I'm pretty sure your DB is not normalised. From your vague description, I believe your tables should be:

ProductCategory (ProductID, CategoryID)
Category (CategoryID, CategoryName) -- CategoryID should be auto-increment identity
Product (ProductID, CategoryID) --ProductID should be auto-increment identity

I also understand from your question and your existing stored proc that you link all categories into all products, in which case the ProdutCategory table becomes redundant unless it stores another column or two.

Monday, March 26, 2012

if cross tab report goes beyond one page?

Hello friends,

I designed a report that shows sales of products datewise -

product 1/12 2/12 3/12 4/12 ...
------------------
abc 100 93 94 109 ...
xyz 7 100 56 98 ...
...
...

if i select 2 or 3 months then it shows 90 columns (per day one column) and i can view the report on screen in correct format. But when i send print, it prints product names and some 18 columns only on first page and on each consecutive pages it shows remaining date columns only (with data) and that is also in cut format (half column on previous page and half column on next page)

In a nut shell, is there any setting so that cross tab will work like EXCEL sheet of fixed row / fixed column facility so that on each new page it should print the fixed column.

Since title row is printing on each new page.

Pls advice.

Thanks in adv.
KSee if you find solution here
http://support.businessobjects.com/|||Hi kirankul,

If u found the solution please update this...

Thanks
Sanju|||Hi All,

I encounter the same problem and found the solution, I am not sure whetehr this solution is really suitable for your case, but I think it will.

You go to Cross-Tab expert by right click the cross tab that you have created then go to Customised style tab --> go to the grid option --> check the keep columns together and check the repeat row labels option

By choosing these options, the cross tab that you printed will have the row header name and column header names at every pages.

Cheers
CS