Click here to Skip to main content
15,884,975 members
Please Sign up or sign in to vote.
1.20/5 (2 votes)
See more:
I have this simple table in my database
----------------------------
table_Items
----------------------------
Item1 | Item2| Item3 | Item4
A     | B    | C     | D
E     | F    | G     | Null
H     | I    | Null  | Null
J     | Null | Null  | Null

------------------
table_Item_Shelves
------------------
Item_Name | Item_ID
A	  |  Null
B	  |  Null
C	  |  Null
D	  |  Null
E	  |  Null


This is what i need to do.
For each row in table_items, first check if row has values in all(item 1,2,3,4) or three of the fields,
than in table_Item_Shelves insert '1' for each item in Item_ID field. For next row conduct the same check
and get maximum value from Item_ID field and increment by 1. I also need to check that maximum four Item_ID's can be same.
Any help in C# and SQL will be great. Thanks.

Kindly no database design suggestions.

Edit: If table_Item_Shelves column Item_ID already has a value than i should not insert new ID
table_Item_Shelves already contain records, and i need to update against those records.
Posted
Updated 9-Oct-13 6:00am
v8
Comments
Torakami 9-Oct-13 5:56am    
This is bad programming idea that you want to go for such design without using any database.Atlest go for my sql
Mubshir 9-Oct-13 6:01am    
Hey man, i understand that and i don't have options to go for mysql or change database design. Thanks.
Azee 9-Oct-13 6:18am    
Does table_Items_Shelves already contains records? and you want to update against those records?
Or you want to insert new records from table_items?
Mubshir 9-Oct-13 6:19am    
Yes, sorry i forgot to mention that. Thanks
Azee 9-Oct-13 6:22am    
which one is it? :)

SQL
-- empty target table
DELETE TABLE_ITEM_SHELVES
GO
-- create entry for every item with item_id 0
INSERT INTO TABLE_ITEM_SHELVES
	SELECT ITEM1, 0 FROM TABLE_ITEMS WHERE LEN(ISNULL(ITEM1,'') + ISNULL(ITEM2,'') + ISNULL(ITEM3,'') + ISNULL(ITEM4,'')) > 2 AND NOT ITEM1 IS NULL
	UNION
	SELECT ITEM2, 0 FROM TABLE_ITEMS WHERE LEN(ISNULL(ITEM1,'') + ISNULL(ITEM2,'') + ISNULL(ITEM3,'') + ISNULL(ITEM4,'')) > 2 AND NOT ITEM2 IS NULL
	UNION
	SELECT ITEM3, 0 FROM TABLE_ITEMS WHERE LEN(ISNULL(ITEM1,'') + ISNULL(ITEM2,'') + ISNULL(ITEM3,'') + ISNULL(ITEM4,'')) > 2 AND NOT ITEM3 IS NULL
	UNION
	SELECT ITEM4, 0 FROM TABLE_ITEMS WHERE LEN(ISNULL(ITEM1,'') + ISNULL(ITEM2,'') + ISNULL(ITEM3,'') + ISNULL(ITEM4,'')) > 2 AND NOT ITEM4 IS NULL
GO
-- update for item1
UPDATE TABLE_ITEM_SHELVES SET ITEM_ID = ITEM_ID + ITMS.CNT
FROM 
(SELECT ITEM1, COUNT(*) AS CNT FROM TABLE_ITEMS WHERE LEN(ISNULL(ITEM1,'') + ISNULL(ITEM2,'') + ISNULL(ITEM3,'') + ISNULL(ITEM4,'')) > 2 AND NOT ITEM1 IS NULL
GROUP BY ITEM1) AS ITMS
WHERE TABLE_ITEM_SHELVES.ITEM_NAME = ITMS.ITEM1
GO
-- update for item2
UPDATE TABLE_ITEM_SHELVES SET ITEM_ID = ITEM_ID + ITMS.CNT
FROM 
(SELECT ITEM2, COUNT(*) AS CNT FROM TABLE_ITEMS WHERE LEN(ISNULL(ITEM1,'') + ISNULL(ITEM2,'') + ISNULL(ITEM3,'') + ISNULL(ITEM4,'')) > 2 AND NOT ITEM2 IS NULL
GROUP BY ITEM2) AS ITMS
WHERE TABLE_ITEM_SHELVES.ITEM_NAME = ITMS.ITEM2
GO
-- update for item3
UPDATE TABLE_ITEM_SHELVES SET ITEM_ID = ITEM_ID + ITMS.CNT
FROM 
(SELECT ITEM3, COUNT(*) AS CNT FROM TABLE_ITEMS WHERE LEN(ISNULL(ITEM1,'') + ISNULL(ITEM2,'') + ISNULL(ITEM3,'') + ISNULL(ITEM4,'')) > 2 AND NOT ITEM3 IS NULL
GROUP BY ITEM3) AS ITMS
WHERE TABLE_ITEM_SHELVES.ITEM_NAME = ITMS.ITEM3
GO
-- update for item4
UPDATE TABLE_ITEM_SHELVES SET ITEM_ID = ITEM_ID + ITMS.CNT
FROM 
(SELECT ITEM4, COUNT(*) AS CNT FROM TABLE_ITEMS WHERE LEN(ISNULL(ITEM1,'') + ISNULL(ITEM2,'') + ISNULL(ITEM3,'') + ISNULL(ITEM4,'')) > 2 AND NOT ITEM4 IS NULL
GROUP BY ITEM4) AS ITMS
WHERE TABLE_ITEM_SHELVES.ITEM_NAME = ITMS.ITEM4
GO
-- get results
SELECT * FROM TABLE_ITEM_SHELVES
GO
 
Share this answer
 
Hey there,

Here try this and let me know if this is what you were looking for:
[EDIT]
C#:
C#
SqlConnection connection = new SqlConnection("YourConnectionString");

                SqlCommand com = new SqlCommand("UpdateItemIDs", connection);
                com.CommandType = CommandType.StoredProcedure;
                connection.Open();
                com.ExecuteNonQuery();
                connection.Close();


Stored Procedure:
SQL
	CREATE PROCEDURE UpdateItemIDs
AS 
    BEGIN
        SELECT  ROW_NUMBER() OVER ( ORDER BY Item1 ) AS RowIndex ,
                IT.* ,
                0 AS processed
        INTO    #TempTable
        FROM    dbo.table_items IT
        WHERE   ( Item1 IS NOT NULL
                  AND Item2 IS NOT NULL
                  AND item3 IS NOT NULL
                )
                OR ( Item1 IS NOT NULL
                     AND Item2 IS NOT NULL
                     AND item4 IS NOT NULL
                   )
                OR ( Item1 IS NOT NULL
                     AND Item3 IS NOT NULL
                     AND item4 IS NOT NULL
                   )
                OR ( Item2 IS NOT NULL
                     AND Item3 IS NOT NULL
                     AND item4 IS NOT NULL
                   )
	
	
        DECLARE @ITEM1 VARCHAR(50)
        DECLARE @ITEM2 VARCHAR(50)
        DECLARE @ITEM3 VARCHAR(50)
        DECLARE @ITEM4 VARCHAR(50)
        DECLARE @RowIndex INT
        DECLARE @NewItemID INT
	
        WHILE ( SELECT  COUNT(*)
                FROM    #TempTable
                WHERE   processed = 0
              ) > 0 
            BEGIN
                SELECT TOP 1
                        @ITEM1 = Item1 ,
                        @ITEM2 = Item2 ,
                        @ITEM3 = item3 ,
                        @ITEM4 = Item4 ,
                        @RowIndex = RowIndex
                FROM    #TempTable
                WHERE   processed = 0
	
                UPDATE  #TempTable
                SET     processed = 1
                WHERE   RowIndex = @RowIndex
	
                SET @NewItemID = ( SELECT   ISNULL(MAX(Item_ID), 0) + 1
                                   FROM     dbo.table_items_shelves
                                 ) ;
	
                UPDATE  dbo.table_items_shelves
                SET     Item_ID = @NewItemID
                WHERE   Item_Name IN ( @ITEM1, @ITEM2, @ITEM3, @ITEM4 )
                        AND Item_ID IS NULL
            END
    END


Hope it helps

Azee...
 
Share this answer
 
v2
Comments
Mubshir 9-Oct-13 8:27am    
Azee i am trying your solution, i run the sql but it didn't work first time. I tried to check again but now it's saying There is already an object named '#TempTable' in the database. Thanks for your effort, really appreciated
Azee 9-Oct-13 8:36am    
Add Drop Table #TempTable at the start of the script .. remove it when you use it in a stored procedure
Azee 9-Oct-13 8:48am    
Sorry I missed the C# part, I have added a basic code for this operation, and converted the script to a stored procedure.
Mubshir 9-Oct-13 9:14am    
Azee works Perfect man. Which parts i will have to change if instead of Item1,Item2,Item3,Item4 i want to do it for 8 items. Only WHERE and DECLARE parts?
Also we have maximum 4 Item_Name can have same Item_ID,
Will that be easy to implement the minimum option as well, say minimum 2 items should have a same ID other wise combine rows get an item from next row and assign them the same ID.
I was thinking if somehow i can order the table in such a way that where all fields have values on top, than-1, than-2 etc.
I have already asked too much and you have done a great favor, if you don't have time, no problem. Cheers
Azee 9-Oct-13 9:17am    
Let me see what I can do.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900