--初始化数据处理语句
select @sql='bcp "'+@dbname+'..'
,@tbstation=len(@sql)+1
,@sql=@sql
+case when @isout=1 then '" out' else '" in' end
+' "'+@path
,@filestation=len(@sql)+1
,@sql=@sql
+'.txt" /c' +' /S"'+@servername
+case when isnull(@username,'')='' then ''
else '" /U"'+@username end
+'" /P"'+isnull(@password,'')+'"'
--数据导入/导出处理
--定义数据处理的游标
set @sql1='declare #tb cursor for select name from '
+@dbname+'..sysobjects where xtype=''U'''
+case when isnull(@tbname,'')='' then ''
else ' and name in ('''+replace(@tbname,',',''',''')+''')' end
print @sql1
exec(@sql1)
open #tb
fetch next from #tb into @tbname
while @@fetch_status=0
begin
select @sql1=stuff(@sql,@tbstation,0,@tbname)
,@sql1=stuff(@sql1,@filestation+len(@tbname),0,@tbname)
print @sql1
exec master..xp_cmdshell @sql1
fetch next from #tb into @tbname
end
close #tb
deallocate #tb
go
set @sql='bcp "'+@tbname
+case when @isout=1 then '" out' else '" in' end
+' "'+@filename+'" /c' +' /S"'+@servername
+case when isnull(@username,'')='' then ''
else '" /U"'+@username end
+'" /P"'+isnull(@password,'')+'"'
exec master..xp_cmdshell @sql
go