select @@VERSION as varsion
declare @str varchar(1000)
select @str=@@version
if charindex('Server 2000',@str)>0
begin
print '2000版本'
/*其他要执行的语句*/
end
else if charindex('Server 2005',@str)>0
begin
print '是2005版本'
end
else if charindex('Server 2008',@str)>0
print '是2008版本'
--I'm creating it in master. You may wish to put it somewhere else
USE master
GO
--Enable clr on your server
-- WITH OVERRIDE forces the change that conflicts with the IO affinity mask
sp_configure 'clr enabled', 0
reconfigure with override
--Enable assemblies with PERMISSION_SET=EXTERNAL_ACCESS
-- to be cataloged in the database
ALTER DATABASE master SET trustworthy ON
--Drop the assembly if it exists already
IF EXISTS (SELECT * FROM sys.assemblies WHERE name = 'Conchango.SqlServer.SqlClrToolkit.GetFileDetails')
DROP ASSEMBLY [Conchango.SqlServer.SqlClrToolkit.GetFileDetails]
GO
--Drop the procedure if it exists already
IF EXISTS (SELECT * FROM sys.procedures WHERE name = 'csp_getfiledetails')
DROP PROCEDURE csp_getfiledetails
GO
SELECT * FROM sys.assemblies WHERE name = 'Conchango.SqlServer.SqlClrToolkit.GetFileDetails'
--Catalog the assembly with PERMISSION_SET=EXTERNAL_ACCESS.
-- This permission set is required because the sproc accesses an external resource (i.e. a file)
-- Script assumes you have saved the assembly to c:\temp, local to SQL Server
CREATE ASSEMBLY [Conchango.SqlServer.SqlClrToolkit.GetFileDetails]
FROM 'C:\Temp\Conchango.SqlServer.SqlClrToolkit.GetFileDetails.dll'
WITH PERMISSION_SET = EXTERNAL_ACCESS
GO
--Have a look at the assembly within the database
-- At this stage the assembly can be deleted from the file system
SELECT * FROM sys.assemblies
SELECT * FROM sys.assembly_files
GO
--Create our procedure from the assembly
CREATE PROCEDURE dbo.csp_getfiledetails( @pFileName nvarchar(4000) )
AS EXTERNAL NAME [Conchango.SqlServer.SqlClrToolkit.GetFileDetails].[Conchango.SqlServer.SqlClrToolkit.GetFileDetails].csp_getfiledetails
go
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[p_binaryIO]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[p_binaryIO]
GO
Create proc p_binaryIO2000
@servename varchar (30),--服务器名称
@username varchar (30), --用户名
@password varchar (30), --密码
@tbname varchar (500), --数据库..表名
@fdname varchar (30), --字段名
@fname varchar (1000), --目录+文件名,处理过程中要使用/覆盖:@filename+_temp
@tj varchar (1000)='', --处理条件.对于数据导入,如果条件中包含@fdname,请指定表名前缀
@isout bit=1 --1导出((默认),0导入
AS
declare @fname_in varchar(1000) --bcp处理应答文件名
,@fsize varchar(20) --要处理的文件的大小
,@m_tbname varchar(50) --临时表名
,@sql varchar(8000)
--则取得导入文件的大小
if @isout=1
set @fsize='0'
else
begin
create table #tb(可选名 varchar(20),大小 int
,创建日期 varchar(10),创建时间 varchar(20)
,上次写操作日期 varchar(10),上次写操作时间 varchar(20)
,上次访问日期 varchar(10),上次访问时间 varchar(20),特性 int)
insert into #tb exec master..xp_getfiledetails @fname
select @fsize=大小 from #tb
drop table #tb
if @fsize is null
begin
print '文件未找到'
return
end
end
--生成数据处理应答文件
set @m_tbname='[##temp'+cast(newid() as varchar(40))+']'
set @sql='select * into '+@m_tbname+' from(
select null as 类型
union all select 0 as 前缀
union all select '+@fsize+' as 长度
union all select null as 结束
union all select null as 格式
) a'
exec(@sql)
select @fname_in=@fname+'_temp'
,@sql='bcp "'+@m_tbname+'" out "'+@fname_in
+'" /S"'+@servename
+case when isnull(@username,'')='' then ''
else '" /U"'+@username end
+'" /P"'+isnull(@password,'')+'" /c'
exec master..xp_cmdshell @sql
--删除临时表
set @sql='drop table '+@m_tbname
exec(@sql)
if @isout=1
begin
set @sql='bcp "select top 1 '+@fdname+' from '
+@tbname+case isnull(@tj,'') when '' then ''
else ' where '+@tj end
+'" queryout "'+@fname
+'" /S"'+@servename
+case when isnull(@username,'')='' then ''
else '" /U"'+@username end
+'" /P"'+isnull(@password,'')
+'" /i"'+@fname_in+'"'
exec master..xp_cmdshell @sql
end
else
begin
--为数据导入准备临时表
set @sql='select top 0 '+@fdname+' into '
+@m_tbname+' from ' +@tbname
exec(@sql)
--将数据导入到临时表
set @sql='bcp "'+@m_tbname+'" in "'+@fname
+'" /S"'+@servename
+case when isnull(@username,'')='' then ''
else '" /U"'+@username end
+'" /P"'+isnull(@password,'')
+'" /i"'+@fname_in+'"'
exec master..xp_cmdshell @sql
--将数据导入到正式表中
set @sql='update '+@tbname
+' set '+@fdname+'=b.'+@fdname
+' from '+@tbname+' a,'
+@m_tbname+' b'
+case isnull(@tj,'') when '' then ''
else ' where '+@tj end
exec(@sql)
--删除数据处理临时表
set @sql='drop table '+@m_tbname
end
--删除数据处理应答文件
set @sql='del '+@fname_in
exec master..xp_cmdshell @sql
go
Create proc p_binaryIO2005
@servename varchar (30),--服务器名称
@username varchar (30), --用户名
@password varchar (30), --密码
@tbname varchar (500), --数据库..表名
@fdname varchar (30), --字段名
@fname varchar (1000), --目录+文件名,处理过程中要使用/覆盖:@filename+_temp
@tj varchar (1000)='', --处理条件.对于数据导入,如果条件中包含@fdname,请指定表名前缀
@isout bit=1 --1导出((默认),0导入
AS
declare @fname_in varchar(1000) --bcp处理应答文件名
,@fsize varchar(20) --要处理的文件的大小
,@m_tbname varchar(50) --临时表名
,@sql varchar(8000)
--则取得导入文件的大小
if @isout=1
set @fsize='0'
else
begin
create table #tb(可选名 varchar(20),大小 int
,创建日期 varchar(10),创建时间 varchar(20)
,上次写操作日期 varchar(10),上次写操作时间 varchar(20)
,上次访问日期 varchar(10),上次访问时间 varchar(20),特性 int)
insert into #tb
exec master..csp_getfiledetails @fname
--exec master..xp_getfiledetails @fname
select @fsize=大小 from #tb
drop table #tb
if @fsize is null
begin
print '文件未找到'
return
end
end
--生成数据处理应答文件
set @m_tbname='[##temp'+cast(newid() as varchar(40))+']'
set @sql='select * into '+@m_tbname+' from(
select null as 类型
union all select 0 as 前缀
union all select '+@fsize+' as 长度
union all select null as 结束
union all select null as 格式
) a'
exec(@sql)
select @fname_in=@fname+'_temp'
,@sql='bcp "'+@m_tbname+'" out "'+@fname_in
+'" /S"'+@servename
+case when isnull(@username,'')='' then ''
else '" /U"'+@username end
+'" /P"'+isnull(@password,'')+'" /c'
exec master..xp_cmdshell @sql
--删除临时表
set @sql='drop table '+@m_tbname
exec(@sql)
if @isout=1
begin
set @sql='bcp "select top 1 '+@fdname+' from '
+@tbname+case isnull(@tj,'') when '' then ''
else ' where '+@tj end
+'" queryout "'+@fname
+'" /S"'+@servename
+case when isnull(@username,'')='' then ''
else '" /U"'+@username end
+'" /P"'+isnull(@password,'')
+'" /i"'+@fname_in+'"'
exec master..xp_cmdshell @sql
end
else
begin
--为数据导入准备临时表
set @sql='select top 0 '+@fdname+' into '
+@m_tbname+' from ' +@tbname
exec(@sql)
--将数据导入到临时表
set @sql='bcp "'+@m_tbname+'" in "'+@fname
+'" /S"'+@servename
+case when isnull(@username,'')='' then ''
else '" /U"'+@username end
+'" /P"'+isnull(@password,'')
+'" /i"'+@fname_in+'"'
exec master..xp_cmdshell @sql
--将数据导入到正式表中
set @sql='update '+@tbname
+' set '+@fdname+'=b.'+@fdname
+' from '+@tbname+' a,'
+@m_tbname+' b'
+case isnull(@tj,'') when '' then ''
else ' where '+@tj end
exec(@sql)
--删除数据处理临时表
set @sql='drop table '+@m_tbname
end
--删除数据处理应答文件
set @sql='del '+@fname_in
exec master..xp_cmdshell @sql
go
--数据导出
exec p_binaryIO '.','sa','','temppic..TABLE1','Pic','d:\zj1.jpg'
--数据导入
exec p_binaryIO '.','sa','','temppic..TABLE1','Pic','d:\zj1.jpg','',0
--这个导入的图片文件必需是通过该函数导出的图片文件,否则会报错。