Link to home
Start Free TrialLog in
Avatar of R8VI
R8VI

asked on

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)'
Avatar of madgino
madgino
Flag of Romania image

What are you trying to obtain?
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

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)'

Avatar of R8VI
R8VI

ASKER

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?

Avatar of R8VI

ASKER

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

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
Avatar of R8VI

ASKER

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
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

Avatar of R8VI

ASKER

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
ASKER CERTIFIED SOLUTION
Avatar of baretree
baretree
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial