Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
my Actual table

MIVID      Quantity         Value
------     ----------       --------
   1           10             3000
   1           20             3500
   1           15             2000
   2           20             3000
   2           50             7500
   3           25             2000


my requirement

MIVID           Quantity        Value
-------         ----------     ---------
  1                10             3000
  1                20             3500
  1                15             2000


MIVID           Quantity        Value
-------         ----------     ---------
  2                20             3000
  2                50             7500


MIVID           Quantity        Value
 -------         ----------     ---------
    3               25             2000


In asp.net how to split the table in to multiple tables based on column values.
Posted
Updated 28-Jun-13 20:28pm
v3
Comments
Maciej Los 28-Jun-13 12:19pm    
What you mean "how to split table in to mulitple tables"?
ASP.NET? WinForms?
Thomas Daniels 28-Jun-13 12:22pm    
ASP.NET (see subject: Table splitting in asp.net)
Maciej Los 28-Jun-13 12:26pm    
I feel like a blind man, now... ;)
Thomas Daniels 28-Jun-13 12:27pm    
:laugh:
+5, if I could!
Maciej Los 28-Jun-13 12:36pm    
;)

1 solution

Have a look at similar questions and answers:
split data into 2 gridview tables[^]
How do I split a table over 2 pages?[^]
Split datatable into multiple tables[^]

I hope this is what you're looking for...


[EDIT #1]
This day was a rainy sunday, so i had a time for fun with SQL. This is what i had done:
SQL
CREATE TABLE #tmp (MIVID INT, Quantity INT, [Value] INT)

INSERT INTO #tmp (MIVID, Quantity, [Value])
SELECT 1 AS MIVID, 10 AS Quantity, 3000 AS [Value]
UNION ALL SELECT 1, 20, 3500
UNION ALL SELECT 1, 15, 2000
UNION ALL SELECT 2, 20, 3000
UNION ALL SELECT 2, 50, 7500
UNION ALL SELECT 3, 25, 2000

--declare temporary table (variable)
DECLARE @commands TABLE (ID INT, command VARCHAR(MAX))

--insert data
INSERT INTO @commands (ID, command)
SELECT DISTINCT MIVID AS ID, 'SELECT * FROM #tmp WHERE MIVID = ' + CONVERT(VARCHAR(10),MIVID) AS command
FROM #tmp

--declare variable to store command to execute
DECLARE @sql VARCHAR(255)

--declare cursor to go through the temporary table to execute command
DECLARE sql_cursor CURSOR
	FOR SELECT command FROM @commands
OPEN sql_cursor
FETCH NEXT FROM sql_cursor INTO @sql;

WHILE @@FETCH_STATUS=0
BEGIN
	PRINT @sql		
	EXEC(@sql)
	FETCH NEXT FROM sql_cursor INTO @sql;
END

CLOSE sql_cursor
DEALLOCATE sql_cursor

DROP TABLE #tmp

Result: 3 data-sets - exactly as you expect ;)
 
Share this answer
 
v2
Comments
Espen Harlinn 30-Jun-13 17:46pm    
Well answered :-D
Maciej Los 30-Jun-13 17:53pm    
Thank you, Espen ;)

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