Brett Kaiser (x002548) Blog

Not Just a Number - Brett Kaiser

Error = [Microsoft][ODBC SQL Server Driver][SQL Server]Incorrect syntax near the keyword 'DESC'.

bcp fails to import data near reserved word.

[Doooh]Alrighty then.  Thanks Tara (again) for pointing out the obvious.  The resolution to this problem is the -q option.  As BOL states, it sets quoted identifiers on in the context of the bcp thread.  So that solves that problem, and I'm sure it's one that I won't forget.  It's curious why there isn't a problem with the bcp out though.[/Doooh]

BOL

-q

Executes the SET QUOTED_IDENTIFIERS ON statement in the connection between the bcp utility and an instance of SQL Server. Use this option to specify a database, owner, table, or view name that contains a space or a quotation mark. Enclose the entire three-part table or view name in double quotation marks (" ").

Who knew?  OK, probably everyone.  I have never run into this before (mostly because I don't use reserve words as column names).  The work around I used was to create a view.  If anyone has a slicker way, or better, an explanation as to why bcp does fail, I'm all ears...or is that eyes?  In any case....something for future reference so I don't have to remember it, and hopefully someone else might benefit.

USE Northwind
GO

SET NOCOUNT ON
CREATE TABLE myTable99(Col1 int, [DESC] varchar(8000))
GO

INSERT INTO myTable99 (Col1, [DESC])
SELECT 1, 'I' UNION ALL
SELECT 2, 'Me' UNION ALL
SELECT 3, 'Mine'
GO

SELECT * FROM myTable99

EXEC master..xp_cmdshell 'bcp Northwind.dbo.myTable99 out d:\data\Northwind_dbo_myTable99.dat -Usa -P -S -c'
GO

TRUNCATE TABLE myTable99
GO

EXEC master..xp_cmdshell 'bcp Northwind.dbo.myTable99 in d:\data\Northwind_dbo_myTable99.dat -Usa -P -S -c -q'
GO

SELECT * FROM myTable99
GO

CREATE VIEW myView99
AS
SELECT Col1,  [DESC] AS Col2 FROM myTable99
GO

EXEC master..xp_cmdshell 'bcp Northwind.dbo.myView99 in d:\data\Northwind_dbo_myTable99.dat -Usa -P -S -c'
GO

SELECT * FROM myTable99
GO

SET NOCOUNT OFF
DROP VIEW myView99
DROP TABLE myTable99
GO

 

Legacy Comments


mehal
2005-04-25
re: Error = [Microsoft][ODBC SQL Server Driver][SQL Server]Incorrect syntax near the keyword 'DESC'.
error occured when you try to execute bcp, maybe this 'bcb' contains some errors ? SQL statment is valid

Brett
2005-04-25
The Error is that a reserve word causes bcp to fail
That's what the example is for. It's suppose to fail. You'll notice there is very little difference in the bcp's.

Syntactically they are all correct. I don't understand the internals of the bulk copy program (bcp) but I'm sure it's reading the sql server catalog and is not handling the reserve words correctly.

I wonder if Kalen Delaney's internals book cover's bcp.


Tara
2005-04-25
re: Error = [Microsoft][ODBC SQL Server Driver][SQL Server]Incorrect syntax near the keyword 'DESC'.
Brett, just use the -q option:

EXEC master..xp_cmdshell 'bcp Northwind.dbo.myView99 in d:\data\Northwind_dbo_myTable99.dat -Usa -P -S -c -q'
GO

eric taylor
2005-10-30
re: Error = [Microsoft][ODBC SQL Server Driver][SQL Server]Incorrect syntax near the keyword 'DESC'.
help

eric taylor
2005-10-30
re: Error = [Microsoft][ODBC SQL Server Driver][SQL Server]Incorrect syntax near the keyword 'DESC'.
help

miguel
2006-01-10
re: Error = [Microsoft][ODBC SQL Server Driver][SQL Server]Incorrect syntax near the keyword 'DESC'.
how do I fix this

virendra
2006-04-18
re: Error = [Microsoft][ODBC SQL Server Driver][SQL Server]Incorrect syntax near the keyword 'DESC'.
i have problem to connect the database

s
2006-07-27
re: Error = [Microsoft][ODBC SQL Server Driver][SQL Server]Incorrect syntax near the keyword 'DESC'.
a

d
2006-08-23
re: Error = [Microsoft][ODBC SQL Server Driver][SQL Server]Incorrect syntax near the keyword 'exec'.
d

Isaac
2007-01-12
re: Error = [Microsoft][ODBC SQL Server Driver][SQL Server]Incorrect syntax near the keyword 'DESC'.
Having a similar problem, and this worked great!!!