拼语句 select 'insert into t(c1,c2,...) select ' + c1 + ',' + c2 + ... from t
exec master..xp_cmdshell 'bcp demo.dbo.student out D:\stu.txt -c /t /T'demo是数据库名.dbo.表名 D:\stu.txt -c/t/T是格式 也可以在运行cmd打开后输入 bcp demo.dbo.student out D:\stu.txt -c /t /T
--将表数据生成SQL脚本的存储过程 CREATE PROCEDURE dbo.UspOutputData @tablename sysname AS declare @column varchar(1000) declare @columndata varchar(1000) declare @sql varchar(4000) declare @xtype tinyint declare @name sysname declare @objectId int declare @objectname sysname declare @ident int set nocount on set @objectId=object_id(@tablename) if @objectId is null -- 判断对象是否存在 begin print 'The object not exists' return end set @objectname=rtrim(object_name(@objectId)) if @objectname is null or charindex(@objectname,@tablename)=0 --此判断不严密 begin print 'object not in current database' return end if OBJECTPROPERTY(@objectId,'IsTable') < > 1 -- 判断对象是否是table begin print 'The object is not table' return end select @ident=status&0x80 from syscolumns where id=@objectid and status&0x80=0x80 if @ident is not null print 'SET IDENTITY_INSERT '+@TableName+' ON' declare syscolumns_cursor cursor for select c.name,c.xtype from syscolumns c where c.id=@objectid order by c.colid open syscolumns_cursor set @column='' set @columndata='' fetch next from syscolumns_cursor into @name,@xtype while @@fetch_status < >-1 begin if @@fetch_status < >-2 begin if @xtype not in(189,34,35,99,98) --timestamp不需处理,image,text,ntext,sql_variant 暂时不处理 begin set @column=@column+case when len(@column)=0 then'' else ','end+@name set @columndata=@columndata+case when len(@columndata)=0 then '' else ','','',' end +case when @xtype in(167,175) then '''''''''+'+@name+'+''''''''' --varchar,char when @xtype in(231,239) then '''N''''''+'+@name+'+''''''''' --nvarchar,nchar when @xtype=61 then '''''''''+convert(char(23),'+@name+',121)+''''''''' --datetime when @xtype=58 then '''''''''+convert(char(16),'+@name+',120)+''''''''' --smalldatetime when @xtype=36 then '''''''''+convert(char(36),'+@name+')+''''''''' --uniqueidentifier else @name end end end fetch next from syscolumns_cursor into @name,@xtype end close syscolumns_cursor deallocate syscolumns_cursor set @sql='set nocount on select ''insert '+@tablename+'('+@column+') values(''as ''--'','+@columndata+','')'' from '+@tablename print '--'+@sql exec(@sql) if @ident is not null print 'SET IDENTITY_INSERT '+@TableName+' OFF' GO exec UspOutputData 你的表名
可以參考:Use master Go if object_ID('[sp_insertsql]') is not null Drop Procedure [sp_insertsql] Go /*生成Insert語句V2.1 Andy 2009-5-4
V2.1 修改了smalldatetime & datetime類型的處理
在V1.0版本的基礎上糾正了插入數據感覺慢的問題。
*/ Create Proc sp_InsertSQL ( @object1 sysname, @object2 sysname=null ) As /* @object1 源表名 @object2 目標表名 */ Set Nocount On Declare @SqlInsert nvarchar(4000), @SqlSelect nvarchar(4000), @SqlPrint nvarchar(4000), @Enter nvarchar(2), @Rows int, @i int, @PrintMaxRows int
If object_id(@object1) Is Null Begin Raiserror 50001 N'無效的表名!' Return End
If Isnull(@object2,'')='' Set @object2='#'+@object1
If object_id('tempdb..#Sql') Is Not Null Drop Table #Sql
Set @Enter=Char(13)+Char(10) Select @SqlInsert=Isnull(@SqlInsert+',','Insert Into '+@object2+' (')+Quotename(Name), @SqlSelect=Isnull(@SqlSelect+'+'',''',' Select ')+'+'+ Case When xtype In(34,48,52,56,59,60,62,104,106,108,122,127,165,173,189) Then 'Isnull(Rtrim('+Quotename(name)+'),''Null'')'+@Enter When xtype =58 Then 'Isnull(''''''''+Convert(nchar(16),'+Quotename(name)+',121)+'''''''',''Null'')'+@Enter --smalldatetime When xtype =61 Then 'Isnull(''''''''+Convert(nchar(23),'+Quotename(name)+',121)+'''''''',''Null'')'+@Enter --datetime When xtype In(35,36,167,175,241) Then 'Isnull(''N''''''+Rtrim('+Quotename(name)+')+'''''''',''Null'')'+@Enter When xtype In(98,99,231,239,231)Then 'Isnull(''N''''''+Rtrim(Replace('+Quotename(name)+','''''''',''''''''''''))+'''''''',''Null'')'+@Enter End From syscolumns Where id=object_id(@object1) Order By colid
Set @SqlInsert=@SqlInsert+')'
Create Table #Sql(ID int Identity(1,1) Primary Key,Sql nvarchar(4000))
Insert Into #Sql Exec (@SqlSelect+' As Sql From '+@object1) Set @Rows=@@Rowcount
If Exists(Select 1 From syscolumns Where id=object_id(@object1) And Colstat=1) Print 'Set Identity_Insert '+Quotename(@object2) +' On'
Set @i=0 Set @PrintMaxRows=50 While @i<@Rows Begin Select @SqlPrint=Char(9)+Char(9)+'Select '+sql From #Sql Where id=@i+1
If @i%@PrintMaxRows=0 And @i>0 Begin Print (Char(9)+@SqlInsert) End If @i%@PrintMaxRows <>@PrintMaxRows-1 And @i<@Rows -1 Set @SqlPrint =@SqlPrint+' Union All '
Print @SqlPrint
Set @i=@i+1 End Print N' Commit Tran'+@Enter+'End Try'+@Enter+'Begin Catch'+@Enter+'Raiserror 50001 N''插入數據過程中發生錯誤.'' '+@Enter+'Rollback Tran'++@Enter+'End Catch' If Exists(Select 1 From syscolumns Where id=object_id(@object1) And Colstat=1)
select 'insert into t(c1,c2,...) select ' + c1 + ',' + c2 + ... from t
也可以在运行cmd打开后输入 bcp demo.dbo.student out D:\stu.txt -c /t /T
@tablename sysname
AS
declare @column varchar(1000)
declare @columndata varchar(1000)
declare @sql varchar(4000)
declare @xtype tinyint
declare @name sysname
declare @objectId int
declare @objectname sysname
declare @ident int set nocount on
set @objectId=object_id(@tablename) if @objectId is null -- 判断对象是否存在
begin
print 'The object not exists'
return
end
set @objectname=rtrim(object_name(@objectId)) if @objectname is null or charindex(@objectname,@tablename)=0 --此判断不严密
begin
print 'object not in current database'
return
end if OBJECTPROPERTY(@objectId,'IsTable') < > 1 -- 判断对象是否是table
begin
print 'The object is not table'
return
end select @ident=status&0x80 from syscolumns where id=@objectid and status&0x80=0x80 if @ident is not null
print 'SET IDENTITY_INSERT '+@TableName+' ON' declare syscolumns_cursor cursor for select c.name,c.xtype from syscolumns c where c.id=@objectid order by c.colid open syscolumns_cursor
set @column=''
set @columndata=''
fetch next from syscolumns_cursor into @name,@xtype while @@fetch_status < >-1
begin
if @@fetch_status < >-2
begin
if @xtype not in(189,34,35,99,98) --timestamp不需处理,image,text,ntext,sql_variant 暂时不处理 begin
set @column=@column+case when len(@column)=0 then'' else ','end+@name set @columndata=@columndata+case when len(@columndata)=0 then '' else ','','','
end +case when @xtype in(167,175) then '''''''''+'+@name+'+''''''''' --varchar,char
when @xtype in(231,239) then '''N''''''+'+@name+'+''''''''' --nvarchar,nchar
when @xtype=61 then '''''''''+convert(char(23),'+@name+',121)+''''''''' --datetime
when @xtype=58 then '''''''''+convert(char(16),'+@name+',120)+''''''''' --smalldatetime
when @xtype=36 then '''''''''+convert(char(36),'+@name+')+''''''''' --uniqueidentifier
else @name end end end fetch next from syscolumns_cursor into @name,@xtype end close syscolumns_cursor
deallocate syscolumns_cursor set @sql='set nocount on select ''insert '+@tablename+'('+@column+') values(''as ''--'','+@columndata+','')'' from '+@tablename print '--'+@sql
exec(@sql) if @ident is not null
print 'SET IDENTITY_INSERT '+@TableName+' OFF' GO exec UspOutputData 你的表名
Go
if object_ID('[sp_insertsql]') is not null
Drop Procedure [sp_insertsql]
Go
/*生成Insert語句V2.1 Andy 2009-5-4
V2.1 修改了smalldatetime & datetime類型的處理
在V1.0版本的基礎上糾正了插入數據感覺慢的問題。
*/
Create Proc sp_InsertSQL
(
@object1 sysname,
@object2 sysname=null
)
As
/*
@object1 源表名
@object2 目標表名
*/
Set Nocount On
Declare @SqlInsert nvarchar(4000),
@SqlSelect nvarchar(4000),
@SqlPrint nvarchar(4000),
@Enter nvarchar(2),
@Rows int,
@i int,
@PrintMaxRows int
If object_id(@object1) Is Null
Begin
Raiserror 50001 N'無效的表名!'
Return
End
If Isnull(@object2,'')=''
Set @object2='#'+@object1
If object_id('tempdb..#Sql') Is Not Null
Drop Table #Sql
Set @Enter=Char(13)+Char(10)
Select @SqlInsert=Isnull(@SqlInsert+',','Insert Into '+@object2+' (')+Quotename(Name),
@SqlSelect=Isnull(@SqlSelect+'+'',''',' Select ')+'+'+
Case
When xtype In(34,48,52,56,59,60,62,104,106,108,122,127,165,173,189) Then 'Isnull(Rtrim('+Quotename(name)+'),''Null'')'+@Enter
When xtype =58 Then 'Isnull(''''''''+Convert(nchar(16),'+Quotename(name)+',121)+'''''''',''Null'')'+@Enter --smalldatetime
When xtype =61 Then 'Isnull(''''''''+Convert(nchar(23),'+Quotename(name)+',121)+'''''''',''Null'')'+@Enter --datetime
When xtype In(35,36,167,175,241) Then 'Isnull(''N''''''+Rtrim('+Quotename(name)+')+'''''''',''Null'')'+@Enter
When xtype In(98,99,231,239,231)Then 'Isnull(''N''''''+Rtrim(Replace('+Quotename(name)+','''''''',''''''''''''))+'''''''',''Null'')'+@Enter
End
From syscolumns
Where id=object_id(@object1)
Order By colid
Set @SqlInsert=@SqlInsert+')'
Create Table #Sql(ID int Identity(1,1) Primary Key,Sql nvarchar(4000))
Insert Into #Sql
Exec (@SqlSelect+' As Sql From '+@object1)
Set @Rows=@@Rowcount
If Exists(Select 1 From syscolumns Where id=object_id(@object1) And Colstat=1)
Print 'Set Identity_Insert '+Quotename(@object2) +' On'
Print N'Begin Try'+@Enter+Char(9)+'Begin Tran'+@Enter+Char(9)+@SqlInsert
Set @i=0
Set @PrintMaxRows=50
While @i<@Rows
Begin
Select @SqlPrint=Char(9)+Char(9)+'Select '+sql From #Sql Where id=@i+1
If @i%@PrintMaxRows=0 And @i>0
Begin
Print (Char(9)+@SqlInsert)
End
If @i%@PrintMaxRows <>@PrintMaxRows-1 And @i<@Rows -1
Set @SqlPrint =@SqlPrint+' Union All '
Print @SqlPrint
Set @i=@i+1
End
Print N' Commit Tran'+@Enter+'End Try'+@Enter+'Begin Catch'+@Enter+'Raiserror 50001 N''插入數據過程中發生錯誤.'' '+@Enter+'Rollback Tran'++@Enter+'End Catch'
If Exists(Select 1 From syscolumns Where id=object_id(@object1) And Colstat=1)
Print 'Set Identity_Insert '+Quotename(@object2) +' Off'
Drop Table #Sql
Go