SET @FilePath='D:\TEST.xls' SET @sql='bcp "SELECT * FROM XXX.dbo.XXX WITH(NOLOCK) WHERE XXX=''XXX'' AND XXX = '+@XXX+'" QueryOut '+@FilePath+' -c -T ' EXEC master..xp_cmdshell @SQL