while exists(select name from sys.tables where SCHEMA_NAME(schema_id)=N'dbo'and@tblname< name) begin SELECT@tblname=min(name) from sys.tables where SCHEMA_NAME(schema_id)=N'dbo'and@tblname< name set@str='BCP '+@dbname+'..'+@tblname+' out '+@dir+@tblname+'.xls -c -T' exec master..xp_cmdshell @str end