Solved

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

Posted on 2006-06-12
4
260 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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

707 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

18 Experts available now in Live!

Get 1:1 Help Now