i have a dinamically table (it could have 10 or 13 or 17 or 20 rows or ..)
but im goint to use a table with 5 rows
INSERT INTO [dbo].[parts] ([idpart] ,[part_name] )
select 1, 'door' union select 2, 'window' union select 3, 'table' union select 4, 'chair' union select 5, 'pencil'
that i need to do its divide the table in 2 parts if the number of rows are multiple of 2 , both tables will have the same number of rows , but if not one table have more rows than the other
the result that i need is this
table 1
1, 'door'
2, 'window'
3, 'table'
table 2 4, 'chair'
5, 'pencil'
i need a query to do this dinamically, beacuse the number of rows are variable
DECLARE @n INT;
SELECT @n = COUNT(*) FROM Tbl;
INSERT INTO TBL1 SELECT TOP ((@n+1)/2) * FROM Tbl ORDER BY idpart ASC
INSERT INTO TBL2 SELECT TOP (@n/2) * FROM Tbl ORDER BY idpart DESC