alter procedure [dbo].[File2Table2]
@servername varchar(200) --服务器名
,@username varchar(200) --用户名,如果用NT验证方式,则为空''
,@password varchar(200) --密码
,@tbname nvarchar(500) --数据库.dbo.表名,如果不指定:.dbo.表名,则导出数据库的所有用户表
,@filename nvarchar(1000) --导入/导出路径/文件名,如果@tbname参数指明是导出整个数据库,则这个参数是文件存放路径,文件名自动用表名.txt
,@isout bit --1为导出,0为导入
as
declare @sql nvarchar(4000)
if @tbname like '%.%.%' --如果指定了表名,则直接导出单个表
begin
set @sql=N'BULK INSERT '+@tbname
+N' FROM '''+@filename+''''
+N'WITH
(
FIELDTERMINATOR = ''|'',
ROWTERMINATOR = ''|\n''
)'
exec sp_executesql @sql
end
else
begin --导出整个数据库,定义游标,取出所有的用户表
declare @m_tbname nvarchar(250)
if right(@filename,1)<>'\' set @filename=@filename+'\'
set @m_tbname='declare #tb cursor for select name from '+@tbname+'..sysobjects where xtype=''U'''
exec(@m_tbname)
open #tb
fetch next from #tb into @m_tbname
while @@fetch_status=0
begin
set @sql=N'BULK INSERT '+@m_tbname
+N' FROM '''+@filename+@m_tbname+'.txt'''
+N' WITH
(
FIELDTERMINATOR = ''|'',
ROWTERMINATOR = ''|\n''
)'
print @sql
exec sp_executesql @sql
fetch next from #tb into @m_tbname
end
close #tb
deallocate #tb
end
@servername varchar(200) --服务器名
,@username varchar(200) --用户名,如果用NT验证方式,则为空''
,@password varchar(200) --密码
,@tbname nvarchar(500) --数据库.dbo.表名,如果不指定:.dbo.表名,则导出数据库的所有用户表
,@filename nvarchar(1000) --导入/导出路径/文件名,如果@tbname参数指明是导出整个数据库,则这个参数是文件存放路径,文件名自动用表名.txt
,@isout bit --1为导出,0为导入
as
declare @sql nvarchar(4000)
if @tbname like '%.%.%' --如果指定了表名,则直接导出单个表
begin
set @sql=N'BULK INSERT '+@tbname
+N' FROM '''+@filename+''''
+N'WITH
(
FIELDTERMINATOR = ''|'',
ROWTERMINATOR = ''|\n''
)'
exec sp_executesql @sql
end
else
begin --导出整个数据库,定义游标,取出所有的用户表
declare @m_tbname nvarchar(250)
if right(@filename,1)<>'\' set @filename=@filename+'\'
set @m_tbname='declare #tb cursor for select name from '+@tbname+'..sysobjects where xtype=''U'''
exec(@m_tbname)
open #tb
fetch next from #tb into @m_tbname
while @@fetch_status=0
begin
set @sql=N'BULK INSERT '+@m_tbname
+N' FROM '''+@filename+@m_tbname+'.txt'''
+N' WITH
(
FIELDTERMINATOR = ''|'',
ROWTERMINATOR = ''|\n''
)'
print @sql
exec sp_executesql @sql
fetch next from #tb into @m_tbname
end
close #tb
deallocate #tb
end