Solved

Using xp_cmdshell and getting error when using multiple tables, whats wrong?

Posted on 2006-06-12
4
261 Views
Last Modified: 2008-03-06
When I try to select from 2 different tables then join them together I get an error. When I only select from 1 table it works fine
This is similar to my statement:

This Works
EXEC master..xp_cmdshell 'bcp "select * from MyDB..inv where style = ''101x''" queryout c:\test.xls -U -P -c'

This doesnt work
EXEC master..xp_cmdshell 'bcp "select * from MyDB..inv,inv_dtl where inv.inv_id = inv_dtl.inv_id and style = ''101x''" queryout c:\test.xls -U -P -c'

I get these errors when executing the second one
SQLState = S0002, NativeError = 208
Error = [Microsoft][ODBC SQL Server Driver][SQL Server]Invalid object name 'inv_dtl'.
SQLState = 37000, NativeError = 8180
Error = [Microsoft][ODBC SQL Server Driver][SQL Server]Statement(s) could not be prepared.
NULL

0
Comment
Question by:byteboy1
  • 2
  • 2
4 Comments
 
LVL 39

Expert Comment

by:appari
ID: 16890541
try adding dbname (MyDB..) to inv_dtl also.

EXEC master..xp_cmdshell 'bcp "select * from MyDB..inv, MyDB..inv_dtl where inv.inv_id = inv_dtl.inv_id and style = ''101x''" queryout c:\test.xls -U -P -c'
0
 
LVL 39

Accepted Solution

by:
appari earned 250 total points
ID: 16890564
or this

EXEC master..xp_cmdshell 'bcp "select * from MyDB..inv as inv, MyDB..inv_dtl as inv_dtl where inv.inv_id = inv_dtl.inv_id and style = ''101x''" queryout c:\test.xls -U -P -c'

0
 

Author Comment

by:byteboy1
ID: 16890567
When I do that it still fails with:

Error = [Microsoft][ODBC SQL Server Driver][SQL Server]The column prefix 'inv' does not match with a table name or alias name used in the query.
0
 

Author Comment

by:byteboy1
ID: 16890582
Thank you appari , it works when I add the aliases!!.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Nowadays, some of developer are too much worried about data. Who is using data, who is updating it etc. etc. Because, data is more costlier in term of money and information. So security of data is focusing concern in days. Lets' understand the Au…
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

911 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

17 Experts available now in Live!

Get 1:1 Help Now