English 中文(简体)
Does bcp out maintain row order while exporting into a data file?
原标题:

I need to export data to a file from a huge table with only one column but thousands of rows where the ordering of row is important. I am using bcp command as below
bcp DBNAME.dbo.tblName out mydata.dat -Uusername -Ppassword -c
I checked with the table having 10 rows and I see that the order of the rows is maintained in the data file. But can I assume bcp would maintain the order if the number of rows is say more than 10000?

最佳回答

I think it will if you use a sql statement (with ORDER BY) in your bcp command:

http://www.sqlteam.com/article/exporting-data-programatically-with-bcp-and-xp_cmdshell

where they have the following example:

SET @bcpCommand =  bcp "SELECT * FROM pubs..authors 
   ORDER BY au_lname" queryout "  
问题回答

You should never assume that a SQL SELECT (or bcp) will return values in a certain order or the same order unless you use an ORDER BY clause.

Generally, the values are returned in order based on an index on the table.

However, I have seen cases where the values are not returned in index order. It appears that rows in the cache may be returned before rows from disk even if the rows on disk are the first in the index.





相关问题
SQL Server database is not visible

I have installed ASP.NET application with database on our server. ASP.NET application created database using connection string below. The problem is that I do not see database in SQL Server Management ...

Most efficient way to store number 11.111 in SQL Server

What datatype is the most efficient to store a number such as 11.111. The numbers will have up 2 digits before the point and up to three after. Currently the column is a bigint , I am guessing that ...

表格和数据表

是否需要在提瓜表之后更新表格统计数据,还是自动更新?

Inconsistent Generate Change Script

I add a column of type tinyint and being set to not allow nulls in a table and generate the change scripts. The table has data in it at this time. The script has code that creates a temp table and ...

Performance of Sql subqueriesfunctions

I am currently working on a particularly complex use-case. Simplifying below :) First, a client record has a many-to-one relationship with a collection of services, that is, a single client may have ...

selecting one value out of an xml column

I have a particularly troublesome xml column to query data from. The schema is fixed by the Quebec Ministry of Revenue so "it is what it is" The important part of the query looks like this: with ...

Selecting records during recursive stored procedure

I ve got a content management system that contains a hierarchical structure of categories, with sub-categories subject to different ordering options at each level. Currently, that s retrieved by a (...

热门标签