Solved

Incorrect syntax sql server 2005

Posted on 2010-09-02
10
426 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
  • 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
 

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
Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 
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

Complete Microsoft Windows PC® & Mac Backup

Backup and recovery solutions to protect all your PCs & Mac– on-premises or in remote locations. Acronis backs up entire PC or Mac with patented reliable disk imaging technology and you will be able to restore workstations to a new, dissimilar hardware in minutes.

Join & Write a Comment

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

747 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now