Solved

Incorrect syntax sql server 2005

Posted on 2010-09-02
10
458 Views
Last Modified: 2012-05-10
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)'
0
Comment
Question by:R8VI
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
  • 2
10 Comments
 
LVL 11

Expert Comment

by:madgino
ID: 33587418
What are you trying to obtain?
0
 
LVL 6

Expert Comment

by:baretree
ID: 33587421
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
 
LVL 11

Expert Comment

by:madgino
ID: 33587462
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
Backup Solution for AWS

Read about how CloudBerry Backup fully integrates your backups with Amazon S3 and Amazon Glacier to provide military-grade encryption and dramatically cut storage costs on any platform.

 

Author Comment

by:R8VI
ID: 33587645
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
 

Author Comment

by:R8VI
ID: 33587657
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
 
LVL 6

Expert Comment

by:baretree
ID: 33587851
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
 

Author Comment

by:R8VI
ID: 33587979
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
 
LVL 6

Expert Comment

by:baretree
ID: 33588538
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
 

Author Comment

by:R8VI
ID: 33590668
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
 
LVL 6

Accepted Solution

by:
baretree earned 500 total points
ID: 33601750
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

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Via a live example, show how to shrink a transaction log file down to a reasonable size.

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question