We have a program that picks up csv files and then puts them into our ERP system. The CSV has "header" rows and "item" rows. The order within this CSV is crucial as all the "item" rows under a "header" row pertain to that "header", thus I am required to ORDER the result before export. If it don't order, then all the "header" rows are at the top and all the "Item" rows at the bottom associate to the last "header row"
what i want:
H, order number1, customer1, address1
I, item1, 6, $3.00
I, item2, 2, $6.00
H, order number2, customer2, address2
I, item1, 5, $2.00
I, item2, 3, $8.00
what i get without ORDER BY:
H, order number1, customer1, address1
H, order number2, customer2, address2
I, item1, 6, $3.00
I, item2, 2, $6.00
I, item1, 5, $2.00
I, item2, 3, $8.00
Unfortunately when i use order by, i get a "The ORDER BY clause is invalid in views"
SET @location = 'c:\exportfile.csv'
SET @query = 'SELECT [TYPE], [ORDER_ITEM], [CUSTOMER_QUANTITY], [ADDRESS_PRICE], [SORTER] FROM NewOrders S ORDER BY [SORTER], [Type]'
SELECT @sqlcmd = CONCAT('SQLCMD -S SERVER -E -Q "SET NOCOUNT ON; ', @query, '" -h -1 -s "," -W -b -o ', @location)
EXEC master..xp_cmdshell @sqlcmd, no_output
is there any way around this?
Question The ORDER BY clause is invalid in views, inline functions, derived tables, subqueries - Where the top should be? does not answer my question as I'm not counting anything, the query is just a Select * but the order of the rows is crucial.