From: Brian Conner via SQLMonster.com on
I have a Query that returns a lot of Data and I would like to have it Export
to a Text File with No More than 60,000 records in each file. So if my Query
contains 200,000 records I would like the Routine to break it up into 4 text
files, 60,000 records in 3 files and 20,000 in the fourth. What would be the
code for this? Any help is greatly appreciated.

--
Brian Conner

Message posted via SQLMonster.com
http://www.sqlmonster.com/Uwe/Forums.aspx/sql-server-programming/201001/1

From: Itzik Ben-Gan on
If you need to address which row goes to which file as part of the querying
logic (as opposed to controlling this in the export process) you can use the
ROW_NUMBER function. If ordering matters, you can use something like:

SELECT *,
(ROW_NUMBER() OVER(ORDER BY <your_ordering>) - 1) / 60000 + 1 AS filenum
FROM YourTable
ORDER BY <your_ordering>;

If ordering doesn't matter, use:

SELECT *,
(ROW_NUMBER() OVER(ORDER BY (SELECT NULL)) - 1) / 60000 + 1 AS filenum
FROM YourTable;

--
Itzik Ben-Gan | SQL Server MVP | www.SolidQ.com | www.InsideTSQL.com


"Brian Conner via SQLMonster.com" <u47161(a)uwe> wrote in message
news:a200e8cd3cced(a)uwe...
> I have a Query that returns a lot of Data and I would like to have it
> Export
> to a Text File with No More than 60,000 records in each file. So if my
> Query
> contains 200,000 records I would like the Routine to break it up into 4
> text
> files, 60,000 records in 3 files and 20,000 in the fourth. What would be
> the
> code for this? Any help is greatly appreciated.
>
> --
> Brian Conner
>
> Message posted via SQLMonster.com
> http://www.sqlmonster.com/Uwe/Forums.aspx/sql-server-programming/201001/1
>