Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 xp_cmdshell with CTE

Author  Topic 

yosiasz
Master Smack Fu Yak Hacker

1635 Posts

Posted - 2008-05-06 : 15:29:00
Hi

Why am I not able to use a table created via CTE with xp_cmdshell to dump it to a text file?
cPRLDetails AS
(
SELECT 1,2,3
FROM [dbo].[VmyVIew] vsp
)
EXEC master..xp_cmdshell 'bcp "Select * FROM cPRLDetails ORDER BY PartExt05" queryout "p:\targetfile.txt" -c -T'

I get the following error
Error = [Microsoft][ODBC SQL Server Driver][SQL Server]Invalid object name 'cPRLDetails'.

Thanks

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2008-05-06 : 15:30:21
It's out of scope.



E 12°55'05.25"
N 56°04'39.16"
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2008-05-06 : 15:31:16
Your bcp command is running in the default database of your windows account, which is probably master.

SELECT * FROM dbName.dbo.cPRLDetails

I haven't a clue if it'll work with a CTE though.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Database maintenance routines:
http://weblogs.sqlteam.com/tarad/archive/2004/07/02/1705.aspx
Go to Top of Page

yosiasz
Master Smack Fu Yak Hacker

1635 Posts

Posted - 2008-05-06 : 17:12:49
Ok it is working with CTE had to do the following to it in case anybody needs to try this

cPRLDetails AS
(
SELECT 1,2,3
FROM [dbo].[VmyVIew] vsp
)
SELECT PartExt05,
PartMat,
PartD,
PartL,
PartW,
PartQty
INTO PRLDetails
FROM cPRLDetails

EXEC master..xp_cmdshell 'bcp "SELECT * FROM [dbname].dbo.PRLDetails Order By PartExt05 DESC" queryout "\\domainname\ShareFolder\Export.txt" -T -c -S[SQLServerName]'
BEGIN
IF OBJECT_ID('PRLDetails') IS NOT NULL
DROP TABLE dbo.PRLDetails ;
END
Go to Top of Page
   

- Advertisement -