create proc p_exporttb
@tbname sysname, --要匯出的表名
@cols nvarchar(1000)=null, --要匯出的表的列名字串,格式:'col1,col2,col3',如果沒有給出值,則認為是所有字段
@path nvarchar(1000), --檔案存放目錄
@fname nvarchar(250)='', --檔名,預設為表名
@over bit=0 --是否覆蓋已經存在的檔案,如果不覆蓋,則直接追加
asset nocount on
declare @err int,@src nvarchar(255),@desc nvarchar(255),@out int
declare @obj int,@constr nvarchar(1000),@sql varchar(8000),@fdlist varchar(8000)
--引數檢測
if @fname is null or ltrim(rtrim(@fname))='' set @fname=@tbname+'.dbf'
--檢查檔案是否已經存在
if right(@path,1)<>'/' set @path=@path+'/'
create table #tb(a bit,b bit,c bit)
set @sql=@path+@fname
insert into #tb exec master..xp_fileexist @sql
if exists(select 1 from #tb where a=1)
if @over=1
begin
set @sql='del '+@sql
exec master..xp_cmdshell @sql,no_output
endelse
set @over=0
else
set @over=1
--刪除臨時表
drop table #tb
--資料庫建立語句
set @sql=@path+@fname
set @constr='provider=microsoft.jet.oledb.4.0;extended properties="dbase 5.0;'
+';hdr=no;database='+@path+'"'
--連線資料庫
exec @err=sp_oacreate 'adodb.connection',@obj out
if @err<>0 goto lberr
exec @err=sp_oamethod @obj,'open',null,@constr
if @err<>0 goto lberr
--建立表的sql
select @sql='',@fdlist=''
--如下判斷@cols是否有值,如果沒有值則認為是全部的列
if @cols is null or rtrim(ltrim(@cols))=''
select @fdlist=@fdlist+','+a.name
,@sql=@sql+',['+a.name+'] '
+case when b.name in('char','nchar','varchar','nvarchar') then
'text('+cast(case when a.length>250 then 250 else a.length end as varchar)+')'
when b.name in('tynyint','int','bigint','tinyint') then 'int'
when b.name in('smalldatetime','datetime') then 'datetime'
when b.name in('money','smallmoney') then 'money'
else b.name end
from syscolumns a left join systypes b on a.xtype=b.xusertype
where b.name not in('image','text','uniqueidentifier','sql_variant','ntext','varbinary','binary','timestamp')
and object_id(@tbname)=id
else
select @fdlist=@fdlist+','+a.name
,@sql=@sql+',['+a.name+'] '
+case when b.name in('char','nchar','varchar','nvarchar') then
'text('+cast(case when a.length>250 then 250 else a.length end as varchar)+')'
when b.name in('tynyint','int','bigint','tinyint') then 'int'
when b.name in('smalldatetime','datetime') then 'datetime'
when b.name in('money','smallmoney') then 'money'
else b.name end
from syscolumns a left join systypes b on a.xtype=b.xusertype
where b.name not in('image','text','uniqueidentifier','sql_variant','ntext','varbinary','binary','timestamp')
and object_id(@tbname)=id and charindex(a.name,@cols)<>0
select @sql='create table ['+@fname
+']('+substring(@sql,2,8000)+')'
,@fdlist=substring(@fdlist,2,8000)
if @over=1
begin
exec @err=sp_oamethod @obj,'execute',@out out,@sql
if @err<>0 goto lberr
endexec @err=sp_oadestroy @obj
set @sql='openrowset(''microsoft.jet.oledb.4.0'',''dbase 5.0;database='
+@path+''',''select * from ['+@fname+']'')'
--匯入資料
exec('insert into '+@sql+'('+@fdlist+') select '+@fdlist+' from '+@tbname)
return
lberr:
exec sp_oageterrorinfo 0,@src out,@desc out
lbexit:
select cast(@err as varbinary(4)) as 錯誤號
,@src as 錯誤源,@desc as 錯誤描述
select @sql,@constr,@fdlist
set nocount off
go
匯出dbf 檔案
匯出dbf 檔案 jsp頁面 請選擇變動原因 儲存 dbf資料匯出 js exportdbfbd click function success function filedownloadpath top.messager.alert 系統提示 匯出成功!info filedownloadpath 房...
ORACLE匯出儲存過程SQL至檔案
oracle匯出儲存過程sql至檔案 專案部署時,需要遷移儲存過程,利用工具匯出過程sql,只是按照順序匯出,不能體現依賴關係,不能一次執行成功,比較麻煩。通過user source dba source all source查詢oralce資料庫物件sql語句,並匯出至檔案。自己動手,當然知道那些...
匯出dbf格式的檔案
using var session new sessionfactory opensession var studentscores q.find string mtablename wyks string mstrconn provider microsoft.jet.oledb.4.0 data...