Incorrect syntax sql server 2005

Hi,

I have the sql statment down below but i am getting incorrect syntax near test please help

EXEC master..xp_cmdshell 'BCP "SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.Columns where TABLE_NAME = 'test'" queryout c:\temp1.csv -c -r, -T -S(local)'
R8VIAsked:
Who is Participating?
 
baretreeConnect With a Mentor Commented:
sorry I'm late on this but, what do you mean by temporal?
The message you get is weird to me. By Temp table do you mean a "#" table? or a "temp table" because it has just been created and you might be dropping it a bit after?
Anyway, I guess that is not the problem.
When you create a temporal table its info is not stored within the current database (Schema) but on the tempdb / test schema, so that's why maybe you are using the "test" thing in there...

A couple of hints:
If it is an actual temp table or that has just been created just right before doing the bcp, then query for it on the "test" schema instead:

EXEC master..xp_cmdshell 'BCP "SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.Columns where SCHEMA_NAME = 'test' AND TABLE_NAME = 'test'" queryout c:\temp1.csv -c -r, -T -S(local);'

if that doesn't work and your "test" table can be actually seen on the SCHEMA_INFORMATION view, then just specify the SCHEMA_TABLE field (the table's owner):

EXEC master..xp_cmdshell 'BCP "SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.Columns where SCHEMA_NAME = 'YOUR_TABLE_OWNER' TABLE_NAME = 'test'" queryout c:\temp1.csv -c -r, -T -S(local);'

if that don't work, then you might like to check if the "test" info can be actually retrieved from the SCHEMA_INFORMATION view:

SELECT TABLE_NAME, COLUMN_NAME FROM INFORMATION_SCHEMA.Columns  ORDER BY ORDINAL_POSITION ASC

and check if TABLE_NAME contains "test" anywhere.
0
 
madginoCommented:
What are you trying to obtain?
0
 
baretreeCommented:
Hello,
I have always had problems when using single and double quotes on those functions.
Try the following:
1. The same query as you're stating but using just single quotes
2. The same query but using a file as the "select" statement just to check if there's something wrong with the query (I guess it is not)
3. Check the server option you're using, you're enclosing it in "()" as well as the row terminator, I know it is valid, but you can try not specifying the -r just to check if it works out :)


EXEC master..xp_cmdshell 'BCP 'SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.Columns where TABLE_NAME = 'test'' queryout c:\temp1.csv -c -r, -T -S(local);'

OR


EXEC master..xp_cmdshell 'BCP c:\query.sql queryout c:\temp1.csv -c -r, -T -S(local);'

where c:\query.sql is a text file containing:
SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.Columns where TABLE_NAME = 'test'

Open in new window

0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
madginoCommented:
try doubling the apostrophe surrounding test:
EXEC master..xp_cmdshell 'BCP "SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.Columns where TABLE_NAME = ''test''" queryout c:\temp1.csv -c -r, -T -S(local)'

0
 
R8VIAuthor Commented:
hi baretree:

I tried your first example and nothign happens no output, now regarding the second example if i can output the coloum name headers then i owuldnt need the second part of the query if i am not msitaken is that right?

0
 
R8VIAuthor Commented:
Hi Hi Madigion,

I tried your example as well and in the CSV file i get no output, this is becasue i think it thinks test is a coloum when it is actually a table

0
 
baretreeCommented:
You can try the command from a command prompt and see what happens.
You can also execute the SELECT * FROM INFORMATION_SCHEMA.Columns without any filter, just to be sure the problem is with the "test" value.
You commented that the problem might be because xp_cmdshell believes "test" is a column when it is a table... what do you mean by that?
As far as I can think about the query you're executing, you want to carry out all the COLUMN_NAME values from the "Columns" TABLE for the table "test" but "test" is to be found in that table as a value for the column TABLE_NAME
Try it without the WHERE clause just to see what happens, it is mostly a quotation thing for sure
0
 
R8VIAuthor Commented:
hi i tried

SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.Columns where TABLE_NAME = 'test'

which brings back the column names back from the table test this is right,

but if i use

EXEC master..xp_cmdshell 'BCP 'SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.Columns where TABLE_NAME = 'test'' queryout c:\temp1.csv -c -r, -T -S(local);'


Does not work as the CSV file has no data
0
 
baretreeCommented:
since you're not using the "no_output" part it is suppose that a command prompt is opened while the execution... can you see something?
Is the Sytnax Error near "test" showing?
It is supposed that when using a BCP with a query as in your case, you'd surround the query part in double quotes and the values within the query in single quotes but that not always works...
I can understand that your query works but the problem is with the BCP execution.... what I wanted was you to execute your BCP without the "WHERE" part, just to check if the problem is the "test" value

You can also include the -o   option, like -oc:\result.txt  so that you can store in a file whatever happened while executing, and even a -ec:errors.txt in case some errors are thrown... can you try that?


without any filter:
EXEC master..xp_cmdshell 'BCP "SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.Columns" queryout c:\temp1.csv -c -r, -T -S(local) -oc:\theoutput.txt -ec:\theerrors.txt;'

with the filter on:
EXEC master..xp_cmdshell 'BCP "SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.Columns where TABLE_NAME = 'test'" queryout c:\temp1.csv -c -r, -T -S(local) -oc:\theoutput.txt -ec:\theerrors.txt;'

Open in new window

0
 
R8VIAuthor Commented:
hi,

Thanks for the help so ok so i executed without the filter and it works fine no problem and the csv file gets populated wrong column headers but it gets populated.
In the output.txt was this

Starting copy...

419 rows copied.
Network packet size (bytes): 4096
Clock Time (ms.): total       16

In theerrors.txt was empty.

Then tried the one with the filter and got Incorrect syntax near 'test'.
test is a temp table that is created before the EXEC

Thanks again for all the help
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.