• SET @bcpCommand = 'bcp "SELECT * FROM #Results" queryout "' + @FileName + '" -T -c -t,'

    -- BEGIN TRY

    EXEC xp_cmdshell @bcpCommand

    as far as I'm aware bcp will open new connection which will not find your temp table #Results ...

    How you are planing to execute your script? Is it going to be scheduled by some scheduling tool? It may have in-build functionality to dump the results to text file, so simple select * from #Results could do....

    _____________________________________________
    "The only true wisdom is in knowing you know nothing"
    "O skol'ko nam otkrytiy chudnyh prevnosit microsofta duh!":-D
    (So many miracle inventions provided by MS to us...)

    How to post your question to get the best and quick help[/url]