Question

I am using the BCP utility to export data from SQL to flat file, and export is done in batches (using the -F and -L attributes). But I want to write an Update query to identify the exported records that will run just after the BCP (in the same Stored proc).

Since I am just passing only the first and last row numbers (not tale Id) to export data, how can I use this data to make the table update?

What I am looking for : Update 'table' Where 'Row from..To'

Was it helpful?

Solution

You can use ROW_NUMBER() to get row numbers, but note that you have to specify an ordering for that, so your bcp will need to use the same ordering.

ROW_NUMBER() can't occur outside a SELECT or ORDER BY, so you will need a subquery or CTE

E.g.

bcp "select * from `table` order by ID" <other bcp params> -F @First -L @Last

WITH rows AS (
SELECT <columns to be updated>, ROW_NUMBER() OVER ( ORDER BY ID ) rn FROM `table`
)
UPDATE rows
SET <some indicator>
WHERE rn BETWEEN @First AND @Last

SQL Fiddle Example

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top