set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go
--導出數據
--Test:exec file2table 'cn110137','sa','1','myDB..CqsUserBase','e:\share\zj.xls',1
--目錄必須有權限寫
ALTER procedure [dbo].[File2Table]
@servername varchar(200)--服务器名
,@username varchar(200)--用户名,如果用NT验证方式,则为空''
,@password varchar(200)--密码
,@tbname varchar(500)--数据库.dbo.表名,如果不指定:.dbo.表名,则导出数据库的所有用户表
,@filename varchar(1000)--导入/导出路径/文件名,如果@tbname参数指明是导出整个数据库,则这个参数是文件存放路径,文件名自动用表名.txt
,@isout bit--1为导出,0为导入
as
declare @sql varchar(8000)
if @tbname like '%.%.%'--如果指定了表名,则直接导出单个表
begin
set @sql='bcp '+@tbname
+case when @isout=1 then ' out ' else ' in ' end
+' "'+@filename+'" /w'
+' /S '+@servername
+case when isnull(@username,'')='' then '' else ' /U '+@username end
+' /P '+isnull(@password,'')
exec master..xp_cmdshell @sql
end
else
begin --导出整个数据库,定义游标,取出所有的用户表
declare @m_tbname varchar(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='bcp '+@tbname+'..'+@m_tbname
+case when @isout=1 then ' out ' else ' in ' end
+' "'+@filename+@m_tbname+'.xls " /w'
+' /S '+@servername
+case when isnull(@username,'')='' then '' else ' /U '+@username end
+' /P '+isnull(@password,'')
exec master..xp_cmdshell @sql
fetch next from #tb into @m_tbname
end
close #tb
deallocate #tb
end
--------------------------------------------------------------------------------------------------------------
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go
-- =============================================
-- Author: <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- Test:exec pr_wExcel '\\cn110064\\aa\\phone.xls','Sheet1$','where tel is not null'
-- =============================================
ALTER PROCEDURE [dbo].[pr_wExcel]
@filename varchar(500),
@x varchar(100),
@where varchar(500)
AS
BEGIN
SET NOCOUNT ON;
declare @sql varchar(5000)
set @sql='
INSERT INTO
OPENDATASOURCE(''Microsoft.JET.OLEDB.4.0'',
''Extended Properties=Excel 8.0;Data source="'+@filename+'"'')...'+@x+'
(name, phone) VALUES (''ttt'', ''1234'')
'
exec(@sql)
print @sql
END
-----------------------------------------------------------------------------------------------------
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go
-- =============================================
-- Author: <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- Test:exec pr_rExcel '\\10.162.145.10\\share\\phone.xls','Sheet1$','where tel is not null'
-- Test:exec pr_rExcel '\\cn110064\\aa\\phone.xls','Sheet1$','where tel is not null'
-- =============================================
ALTER PROCEDURE [dbo].[pr_rExcel]
@filename varchar(500),
@x varchar(100),
@where varchar(500)
AS
BEGIN
SET NOCOUNT ON;
declare @sql varchar(5000)
set @sql='
SELECT name,tel,notes
FROM OpenDataSource( ''Microsoft.Jet.OLEDB.4.0'',
''Data Source="'+@filename+'";User ID=Admin;Password=;Extended properties=Excel 5.0'')...'+@x+' '+@where+'
'
exec(@sql)
print @sql
END