Eugene, you are quite right and I completely forgot about that. In this case, the OP should just create a physical table and select the results from that (then it could be dropped again).
I also had a typo in the previous example (an extra "comma") - the example below should work
Example:C-REATE TABLE MyDatabase.dbo.BcpTest (col1 int, col2 varchar(25))
INSERT INTO dbo.BcpTest
SELECT 1, 'Ananda Murugesan' UNION ALL
SELECT 2, 'MyDoggieJessie' UNION ALL
SELECT 3, 'Eugene Elutin'
D-ECLARE @FileName varchar(50), @bcpCommand varchar(2000), @Result varchar(max)
SET @FileName = REPLACE('C:\CheckPlanCacheResults_' + CONVERT(char(8), GETDATE(), 1) + '.txt', '/', '-')
SET @bcpCommand = 'bcp "SELECT * FROM MyDatabase.dbo.BcpTest" queryout "' + @FileName + '" -c -S' + RTRIM(@@servername) + ' -T -t '
BEGIN TRY
EXEC @result = xp_cmdshell @bcpCommand
END TRY
BEGIN CATCH
PRINT 'There was an error...'
END CATCH
D-ROP TABLE MyDatabase.dbo.BcpTest
______________________________________________________________________________Never argue with an idiot; Theyll drag you down to their level and beat you with experience