MSSQL表資料匯出成Insert指令碼<改進版>
USE [H3]
GO
/****** Object: StoredProcedure [dbo].[pCreateInsertScript] Script. Date: 01/05/2013 11:44:21 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:
-- Create date:
GO
/****** Object: StoredProcedure [dbo].[pCreateInsertScript] Script. Date: 01/05/2013 11:44:21 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:
-- Create date:
-- Description: 將表資料生成Insert指令碼
-- Demo : exec pCreateInsertScript. 'TestTable','1=1'
-- =============================================
ALTER proc [dbo].[pCreateInsertScript] (@tablename varchar(256),@con nvarchar(400))
as
begin
set nocount on
declare @sqlstr varchar(4000)
declare @sqlstr1 varchar(4000)
declare @sqlstr2 varchar(4000)
select @sqlstr='select ''insert
select @sqlstr1=''
select @sqlstr2='('
select @sqlstr1='values (''+'
select @sqlstr1=@sqlstr1+col+'+'',''+' ,@sqlstr2=@sqlstr2+name +',' from (select case
when a.xtype =36 then 'case when ['+a.[name]+'] is null then ''NULL'' else '+'''''''''+'+'replace('+a.[name]+','''''''','''''''''''')' + '+'''''''''+' end'
--when a.xtype =36 then 'case when ['+a.[name]+'] is null then ''NULL'' else '+'convert(varchar(250),['+a.[name] +'])'+' end'
when a.xtype =173 then 'case when ['+a.[name]+'] is null then ''NULL'' else '+'convert(varchar('+convert(varchar(4),a.length*2+2)+'),['+a.[name] +'])'+' end'
when a.xtype =104 then 'case when ['+a.[name]+'] is null then ''NULL'' else '+'convert(varchar(1),['+a.[name] +'])'+' end'
when a.xtype =175 then 'case when ['+a.[name]+'] is null then ''NULL'' else '+'''''''''+'+'replace('+a.[name]+','''''''','''''''''''')' + '+'''''''''+' end'
when a.xtype =61 then 'case when ['+a.[name]+'] is null then ''NULL'' else '+'''''''''+'+'convert(varchar(23),['+a.[name] +'],121)'+ '+'''''''''+' end'
when a.xtype =106 then 'case when ['+a.[name]+'] is null then ''NULL'' else '+'convert(varchar('+convert(varchar(4),a.xprec+2)+'),['+a.[name] +'])'+' end'
when a.xtype =62 then 'case when ['+a.[name]+'] is null then ''NULL'' else '+'convert(varchar(23),['+a.[name] +'],2)'+' end'
when a.xtype =56 then 'case when ['+a.[name]+'] is null then ''NULL'' else '+'convert(varchar(11),['+a.[name] +'])'+' end'
when a.xtype =60 then 'case when ['+a.[name]+'] is null then ''NULL'' else '+'convert(varchar(22),['+a.[name] +'])'+' end'
when a.xtype =239 then 'case when ['+a.[name]+'] is null then ''NULL'' else '+'''''''''+'+'replace('+a.[name]+','''''''','''''''''''')' + '+'''''''''+' end'
when a.xtype =108 then 'case when ['+a.[name]+'] is null then ''NULL'' else '+'convert(varchar('+convert(varchar(4),a.xprec+2)+'),['+a.[name] +'])'+' end'
when a.xtype =231 then 'case when ['+a.[name]+'] is null then ''NULL'' else '+'''''''''+'+'replace('+a.[name]+','''''''','''''''''''')' + '+'''''''''+' end'
when a.xtype =59 then 'case when ['+a.[name]+'] is null then ''NULL'' else '+'convert(varchar(23),['+a.[name] +'],2)'+' end'
when a.xtype =58 then 'case when ['+a.[name]+'] is null then ''NULL'' else '+'''''''''+'+'convert(varchar(23),['+a.[name] +'],121)'+ '+'''''''''+' end'
when a.xtype =52 then 'case when ['+a.[name]+'] is null then ''NULL'' else '+'convert(varchar(12),['+a.[name] +'])'+' end'
when a.xtype =122 then 'case when ['+a.[name]+'] is null then ''NULL'' else '+'convert(varchar(22),['+a.[name] +'])'+' end'
when a.xtype =127 then 'case when ['+a.[name]+'] is null then ''NULL'' else '+'convert(varchar(6),['+a.[name] +'])'+' end'
when a.xtype =48 then 'case when ['+a.[name]+'] is null then ''NULL'' else '+'convert(varchar(6),['+a.[name] +'])'+' end'
when a.xtype =165 then 'case when ['+a.[name]+'] is null then ''NULL'' else '+'convert(varchar('+convert(varchar(4),a.length*2+2)+'),['+a.[name] +'])'+' end'
when a.xtype =167 then 'case when ['+a.[name]+'] is null then ''NULL'' else '+'''''''''+'+'replace('+a.[name]+','''''''','''''''''''')' + '+'''''''''+' end'
else '''NULL'''
end as col,a.colid,'['+ a.name+']' as name
from syscolumns a where a.id = object_id(@tablename)
and a.xtype <>189 and a.xtype <>34
and a.xtype <>35 --and a.xtype <>36
)t order by colid
select @sqlstr=@sqlstr+left(@sqlstr2,len(@sqlstr2)-1)+') '+left(@sqlstr1,len(@sqlstr1)-3)+')'' from + ' where ' + @con
print @sqlstr
exec( @sqlstr)
set nocount off
end
-- Demo : exec pCreateInsertScript. 'TestTable','1=1'
-- =============================================
ALTER proc [dbo].[pCreateInsertScript] (@tablename varchar(256),@con nvarchar(400))
as
begin
set nocount on
declare @sqlstr varchar(4000)
declare @sqlstr1 varchar(4000)
declare @sqlstr2 varchar(4000)
select @sqlstr='select ''insert
select @sqlstr1=''
select @sqlstr2='('
select @sqlstr1='values (''+'
select @sqlstr1=@sqlstr1+col+'+'',''+' ,@sqlstr2=@sqlstr2+name +',' from (select case
when a.xtype =36 then 'case when ['+a.[name]+'] is null then ''NULL'' else '+'''''''''+'+'replace('+a.[name]+','''''''','''''''''''')' + '+'''''''''+' end'
--when a.xtype =36 then 'case when ['+a.[name]+'] is null then ''NULL'' else '+'convert(varchar(250),['+a.[name] +'])'+' end'
when a.xtype =173 then 'case when ['+a.[name]+'] is null then ''NULL'' else '+'convert(varchar('+convert(varchar(4),a.length*2+2)+'),['+a.[name] +'])'+' end'
when a.xtype =104 then 'case when ['+a.[name]+'] is null then ''NULL'' else '+'convert(varchar(1),['+a.[name] +'])'+' end'
when a.xtype =175 then 'case when ['+a.[name]+'] is null then ''NULL'' else '+'''''''''+'+'replace('+a.[name]+','''''''','''''''''''')' + '+'''''''''+' end'
when a.xtype =61 then 'case when ['+a.[name]+'] is null then ''NULL'' else '+'''''''''+'+'convert(varchar(23),['+a.[name] +'],121)'+ '+'''''''''+' end'
when a.xtype =106 then 'case when ['+a.[name]+'] is null then ''NULL'' else '+'convert(varchar('+convert(varchar(4),a.xprec+2)+'),['+a.[name] +'])'+' end'
when a.xtype =62 then 'case when ['+a.[name]+'] is null then ''NULL'' else '+'convert(varchar(23),['+a.[name] +'],2)'+' end'
when a.xtype =56 then 'case when ['+a.[name]+'] is null then ''NULL'' else '+'convert(varchar(11),['+a.[name] +'])'+' end'
when a.xtype =60 then 'case when ['+a.[name]+'] is null then ''NULL'' else '+'convert(varchar(22),['+a.[name] +'])'+' end'
when a.xtype =239 then 'case when ['+a.[name]+'] is null then ''NULL'' else '+'''''''''+'+'replace('+a.[name]+','''''''','''''''''''')' + '+'''''''''+' end'
when a.xtype =108 then 'case when ['+a.[name]+'] is null then ''NULL'' else '+'convert(varchar('+convert(varchar(4),a.xprec+2)+'),['+a.[name] +'])'+' end'
when a.xtype =231 then 'case when ['+a.[name]+'] is null then ''NULL'' else '+'''''''''+'+'replace('+a.[name]+','''''''','''''''''''')' + '+'''''''''+' end'
when a.xtype =59 then 'case when ['+a.[name]+'] is null then ''NULL'' else '+'convert(varchar(23),['+a.[name] +'],2)'+' end'
when a.xtype =58 then 'case when ['+a.[name]+'] is null then ''NULL'' else '+'''''''''+'+'convert(varchar(23),['+a.[name] +'],121)'+ '+'''''''''+' end'
when a.xtype =52 then 'case when ['+a.[name]+'] is null then ''NULL'' else '+'convert(varchar(12),['+a.[name] +'])'+' end'
when a.xtype =122 then 'case when ['+a.[name]+'] is null then ''NULL'' else '+'convert(varchar(22),['+a.[name] +'])'+' end'
when a.xtype =127 then 'case when ['+a.[name]+'] is null then ''NULL'' else '+'convert(varchar(6),['+a.[name] +'])'+' end'
when a.xtype =48 then 'case when ['+a.[name]+'] is null then ''NULL'' else '+'convert(varchar(6),['+a.[name] +'])'+' end'
when a.xtype =165 then 'case when ['+a.[name]+'] is null then ''NULL'' else '+'convert(varchar('+convert(varchar(4),a.length*2+2)+'),['+a.[name] +'])'+' end'
when a.xtype =167 then 'case when ['+a.[name]+'] is null then ''NULL'' else '+'''''''''+'+'replace('+a.[name]+','''''''','''''''''''')' + '+'''''''''+' end'
else '''NULL'''
end as col,a.colid,'['+ a.name+']' as name
from syscolumns a where a.id = object_id(@tablename)
and a.xtype <>189 and a.xtype <>34
and a.xtype <>35 --and a.xtype <>36
)t order by colid
select @sqlstr=@sqlstr+left(@sqlstr2,len(@sqlstr2)-1)+') '+left(@sqlstr1,len(@sqlstr1)-3)+')'' from + ' where ' + @con
print @sqlstr
exec( @sqlstr)
set nocount off
end
溫馨提示:xtype=36 是自動生成的ID 這裡沒有寫進去
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/22392018/viewspace-749135/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 匯出oracle表記錄為insert指令碼Oracle指令碼
- 資料匯出shell指令碼(上)指令碼
- MySQL匯出資料庫指令碼MySql資料庫指令碼
- 表統計資訊匯出匯入指令碼指令碼
- ORACLE資料表匯出成EXCEL格式(ODBC)OracleExcel
- 海量資料遷移之分割槽表批次insert效能改進
- 海量資料遷移之分割槽表批量insert效能改進
- mssql sqlserver 使用sql指令碼 清空所有資料庫表資料的方法分享SQLServer指令碼資料庫
- mssql生成資料庫字典指令碼-MarkDownSQL資料庫指令碼
- MSSQL資料匯出到MYSQLMySql
- BCP匯入匯出MsSqlSQL
- 從Oracle資料庫中匯出SQL指令碼Oracle資料庫SQL指令碼
- Oracle expdp/impdp匯入匯出備份資料庫指令碼Oracle資料庫指令碼
- mysqldump匯入匯出表資料MySql
- 資料泵匯出匯入表
- DB2資料庫匯出表結構與匯入、匯出表資料DB2資料庫
- expdp 匯出指令碼指令碼
- 大量包含Insert語句的指令碼檔案批量執行匯入資料指令碼
- MySQL表資料匯入與匯出MySql
- Progress資料表的匯入匯出
- [ Shell ] 通過 Shell 指令碼匯出 CDL 網表指令碼
- shell,ant指令碼實現自動資料泵(exp.expdp)匯出匯入資料指令碼
- Mysql匯出表結構、表資料MySql
- 資料匯入shell指令碼(下)指令碼
- ClickHouse 資料表匯出和匯入(qbit)
- 匯入匯出 Oracle 分割槽表資料Oracle
- Oracle使用資料泵匯出匯入表Oracle
- Oracle表中資料匯出成 Txt格式的方案Oracle
- MSSQL匯出所有login賬號和密碼SQL密碼
- 生成insert指令碼的指令碼指令碼
- expdp匯出表的部分資料
- Excel匯入Sqlserver資料庫指令碼ExcelSQLServer資料庫指令碼
- Oracle使用資料泵expdp,impdp進行資料匯出匯入Oracle
- 如何改進你的指令碼程式指令碼
- 批次Insert匯入資料實驗
- EBS 10g/11g 資料庫匯出匯入遷移指令碼 - transform資料庫指令碼ORM
- 使用Dbeaver 進行資料的匯入和匯出
- Oracle pl/sql 複製表 資料匯入 匯出OracleSQL