You could more easily do this via SSIS (whereby you could set the "destination" of the SQL Task to a file destination, setting all kinds of options), however in TSQL you could try something like this:D-ECLARE @FileName varchar(50),
@bcpCommand varchar(2000)
INSERT INTO #Results
EXEC sp_SQLskills_CheckPlanCache @Percent output, @WastedMB output
SET @FileName = REPLACE('C:\CheckPlanCacheResults_'+ CONVERT(char(8),GETDATE(),1)+'.txt', '/', '-')
SET @bcpCommand = 'bcp "SELECT * FROM #Results" queryout "' + @FileName + '" -T -c -t,'
BEGIN TRY
EXEC xp_cmdshell @bcpCommand
END TRY
BEGIN CATCH
PRINT 'There was an error...'
END CATCH
You would need to create a temporary holding table for the results, and "D-ECLARE" typed this way to avoid my proxy snagging the code, so remove the hyphen)
______________________________________________________________________________Never argue with an idiot; Theyll drag you down to their level and beat you with experience