BCP data extract issue

Padmanabhan, Venkatesh 241 Reputation points
2021-05-21T17:04:39.44+00:00

Hi.
I am trying to use BCP command in a C# console application.

The sql query is as below:

BCP "SELECT 1 as First, [IDENTITY], QUOTENAME(columnName,'""') from tablename" queryout "C:\Data\FILEname.del" -T -S Server -d DBInstance -b 500 -c -C 65001 -t~

since the query contains [] , the bcp returns no rows. Directly running this in SQL, rows are shown in results.

How to fix this ?

SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
12,675 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,219 questions
Transact-SQL
Transact-SQL
A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.
4,552 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Erland Sommarskog 100.9K Reputation points MVP
    2021-05-21T22:01:57.17+00:00

    What do you mean "with returns no rows". BCP never returns any rows, it writes to a file.

    Why do think that the brackets are a problem?

    I do spot an issue in your command though. The double quotes you pass to quotename can cause problems with the command-line window. Change this to:

    QUOTENAME(columnName, char(34))
    

    Beware that the quotename function will return NULL if the input exceeds 128 characters!

    0 comments No comments