10 个解决方案
#1
CREATE proc spGenInsertSQL (@tablename varchar(256))
as
begin
declare @sql varchar(8000)
declare @sqlValues varchar(8000)
set @sql =' ('
set @sqlValues = 'values (''+'
select @sqlValues = @sqlValues + cols + ' + '','' + ' ,@sql = @sql + '[' + name + '],'
from
(select case
when xtype in (48,52,56,59,60,62,104,106,108,122,127)
then 'case when '+ name +' is null then ''NULL'' else ' + 'cast('+ name + ' as varchar)'+' end'
when xtype in (58,61)
then 'case when '+ name +' is null then ''NULL'' else '+''''''''' + ' + 'cast('+ name +' as varchar)'+ '+'''''''''+' end'
when xtype in (167)
then 'case when '+ name +' is null then ''NULL'' else '+''''''''' + ' + 'replace('+ name+','''''''','''''''''''')' + '+'''''''''+' end'
when xtype in (231)
then 'case when '+ name +' is null then ''NULL'' else '+'''N'''''' + ' + 'replace('+ name+','''''''','''''''''''')' + '+'''''''''+' end'
when xtype in (175)
then 'case when '+ name +' is null then ''NULL'' else '+''''''''' + ' + 'cast(replace('+ name+','''''''','''''''''''') as Char(' + cast(length as varchar) + '))+'''''''''+' end'
when xtype in (239)
then 'case when '+ name +' is null then ''NULL'' else '+'''N'''''' + ' + 'cast(replace('+ name+','''''''','''''''''''') as Char(' + cast(length as varchar) + '))+'''''''''+' end'
else '''NULL'''
end as Cols,name
from syscolumns
where id = object_id(@tablename)
) T
set @sql ='select ''INSERT INTO ['+ @tablename + ']' + left(@sql,len(@sql)-1)+') ' + left(@sqlValues,len(@sqlValues)-4) + ')'' from '+@tablename
print @sql
exec (@sql)
end
GO
--exec spGenInsertSQL 表名
#2
-- 导出表中数据到sql文件
create procedure [dbo].[sp_generate_insert_script]
@tablename_mask varchar(30) = NULL
as
begin
declare @tablename varchar (128)
declare @tablename_max varchar (128)
declare @tableid int
declare @columncount numeric (7,0)
declare @columncount_max numeric (7,0)
declare @columnname varchar (30)
declare @columntype int
declare @string varchar (30)
declare @leftpart varchar (8000) /* 8000 is the longest string SQLSrv7 can EXECUTE */
declare @rightpart varchar (8000) /* without having to resort to concatenation */
declare @hasident int
set nocount on
-- take ALL tables when no mask is given (!)
if (@tablename_mask is NULL)
begin
select @tablename_mask = '%'
end
-- create table columninfo now, because it will be used several times
create table #columninfo
(num numeric (7,0) identity,
name varchar(30),
usertype smallint)
select name,
id
into #tablenames
from sysobjects
where type in ('U' ,'S')
and name like @tablename_mask
-- loop through the table #tablenames
select @tablename_max = MAX (name),
@tablename = MIN (name)
from #tablenames
while @tablename <= @tablename_max
begin
select @tableid = id
from #tablenames
where name = @tablename
if (@@rowcount <> 0)
begin
-- Find out whether the table contains an identity column
select @hasident = max( status & 0x80 )
from syscolumns
where id = @tableid
truncate table #columninfo
insert into #columninfo (name,usertype)
select name, type
from syscolumns C
where id = @tableid
and type <> 37 -- do not include timestamps
-- Fill @leftpart with the first part of the desired insert-statement, with the fieldnames
select @leftpart = 'select ''insert into '+@tablename
select @leftpart = @leftpart + '('
select @columncount = MIN (num),
@columncount_max = MAX (num)
from #columninfo
while @columncount <= @columncount_max
begin
select @columnname = name,
@columntype = usertype
from #columninfo
where num = @columncount
if (@@rowcount <> 0)
begin
if (@columncount < @columncount_max)
begin
select @leftpart = @leftpart + @columnname + ','
end
else
begin
select @leftpart = @leftpart + @columnname + ')'
end
end
select @columncount = @columncount + 1
end
select @leftpart = @leftpart + ' values('''
-- Now fill @rightpart with the statement to retrieve the values of the fields, correctly formatted
select @columncount = MIN (num),
@columncount_max = MAX (num)
from #columninfo
select @rightpart = ''
while @columncount <= @columncount_max
begin
select @columnname = name,
@columntype = usertype
from #columninfo
where num = @columncount
if (@@rowcount <> 0)
begin
if @columntype in (39,47) /* char fields need quotes (except when entering NULL);
* use char(39) == ', easier readable than escaping
*/
begin
select @rightpart = @rightpart + '+'
select @rightpart = @rightpart + 'ISNULL(' + replicate( char(39), 4 ) + '+replace(' + @columnname + ',' + replicate( char(39), 4 ) + ',' + replicate( char(39), 6) + ')+' + replicate( char(39), 4 ) + ',''NULL'')'
end
else if @columntype = 35 /* TEXT fields cannot be RTRIM-ed and need quotes */
/* convert to VC 1000 to leave space for other fields */
begin
select @rightpart = @rightpart + '+'
select @rightpart = @rightpart + 'ISNULL(' + replicate( char(39), 4 ) + '+replace(convert(varchar(1000),' + @columnname + ')' + ',' + replicate( char(39), 4 ) + ',' + replicate( char(39), 6 ) + ')+' + replicate( char(39), 4 ) + ',''NULL'')'
end
else if @columntype in (58,61,111) /* datetime fields */
begin
select @rightpart = @rightpart + '+'
select @rightpart = @rightpart + 'ISNULL(' + replicate( char(39), 4 ) + '+convert(varchar(20),' + @columnname + ')+'+ replicate( char(39), 4 ) + ',''NULL'')'
end
else /* numeric types */
begin
select @rightpart = @rightpart + '+'
select @rightpart = @rightpart + 'ISNULL(convert(varchar(99),' + @columnname + '),''NULL'')'
end
if ( @columncount < @columncount_max)
begin
select @rightpart = @rightpart + '+'','''
end
end
select @columncount = @columncount + 1
end
end
select @rightpart = @rightpart + '+'')''' + ' from ' + @tablename
-- Order the select-statements by the first column so you have the same order for
-- different database (easy for comparisons between databases with different creation orders)
select @rightpart = @rightpart + ' order by 1'
-- For tables which contain an identity column we turn identity_insert on
-- so we get exactly the same content
if @hasident > 0
select 'SET IDENTITY_INSERT ' + @tablename + ' ON'
exec ( @leftpart + @rightpart )
if @hasident > 0
select 'SET IDENTITY_INSERT ' + @tablename + ' OFF'
select @tablename = MIN (name)
from #tablenames
where name > @tablename
end
end
#3
--将表数据生成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 你的表名
#4
我最后执行的时候exec spGenInsertSQL ftlw.t_machn_menu
报错:消息 102,级别 15,状态 1,第 1 行
'.' 附近有语法错误。
前面不加库名,又说对象不存在,怎么回事
#5
use ftlw
exec spGenInsertSQL t_machn_menu
exec spGenInsertSQL t_machn_menu
#6
你最好
这种错误还是避免
use ftlw
create proc spGenInsertSQL
exec spGenInsertSQL t_machn_menu
这种错误还是避免
#7
也可以手工生成脚本的
#8
一条一条再插回去很慢的哦,用导入导出工具。
#9
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'
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
#10
e.g.
use AdventureWorks2008
GO
Exec dbo.sp_InsertSQL 'Person.Address','Person.Address'
Go
/*
Set Identity_Insert [Person.Address] On
Begin Try
Begin Tran
Insert Into Person.Address ([AddressID],[AddressLine1],[AddressLine2],[City],[StateProvinceID],[PostalCode],[rowguid],[ModifiedDate])
Select 1,N'1970 Napa Ct.',Null,N'Bothell',79,N'98011',N'9AADCB0D-36CF-483F-84D8-585C2D4EC6E9','1998-01-04 00:00:00.000' Union All
Select 2,N'9833 Mt. Dias Blv.',Null,N'Bothell',79,N'98011',N'32A54B9E-E034-4BFB-B573-A71CDE60D8C0','1999-01-01 00:00:00.000' Union All
Select 3,N'7484 Roundtree Drive',Null,N'Bothell',79,N'98011',N'4C506923-6D1B-452C-A07C-BAA6F5B142A4','2003-04-08 00:00:00.000' Union All
Select 4,N'9539 Glenside Dr',Null,N'Bothell',79,N'98011',N'E5946C78-4BCC-477F-9FA1-CC09DE16A880','1999-03-07 00:00:00.000' Union All
Select 5,N'1226 Shoe St.',Null,N'Bothell',79,N'98011',N'FBAFF937-4A97-4AF0-81FD-B849900E9BB0','1999-01-20 00:00:00.000' Union All
Select 6,N'1399 Firestone Drive',Null,N'Bothell',79,N'98011',N'FEBF8191-9804-44C8-877A-33FDE94F0075','1999-03-17 00:00:00.000' Union All
Select 7,N'5672 Hale Dr.',Null,N'Bothell',79,N'98011',N'0175A174-6C34-4D41-B3C1-4419CD6A0446','2000-01-12 00:00:00.000' Union All
Select 8,N'6387 Scenic Avenue',Null,N'Bothell',79,N'98011',N'3715E813-4DCA-49E0-8F1C-31857D21F269','1999-01-18 00:00:00.000' Union All
Select 9,N'8713 Yosemite Ct.',Null,N'Bothell',79,N'98011',N'268AF621-76D7-4C78-9441-144FD139821A','2002-07-01 00:00:00.000' Union All
Select 10,N'250 Race Court',Null,N'Bothell',79,N'98011',N'0B6B739D-8EB6-4378-8D55-FE196AF34C04','1999-01-03 00:00:00.000' Union All
Select 11,N'1318 Lasalle Street',Null,N'Bothell',79,N'98011',N'981B3303-ACA2-49C7-9A96-FB670785B269','2003-04-01 00:00:00.000' Union All
.... ...
Select 32517,N'177 11th Ave',Null,N'Sammamish',79,N'98074',N'2F76ECD8-B22C-4353-BE61-09FE000E13CE','1999-01-30 00:00:00.000' Union All
Select 32518,N'8040 Hill Ct',Null,N'Redmond',79,N'98052',N'0E5F2D4F-C38F-406C-9169-8CCD754DBF94','1999-02-20 00:00:00.000' Union All
Select 32519,N'137 Mazatlan',Null,N'Seattle',79,N'98104',N'7B2B1EE6-43A4-426D-8A7D-54DD451F716B','1999-03-15 00:00:00.000' Union All
Select 32520,N'5863 Sierra',Null,N'Bellevue',79,N'98004',N'F72A65AD-0FD0-42FD-8760-4B83DE543926','1999-02-16 00:00:00.000' Union All
Select 32521,N'7145 Matchstick Drive',Null,N'Sammamish',79,N'98074',N'868501B7-4B9C-4F27-896B-342689EA5980','1999-02-16 00:00:00.000'
Commit Tran
End Try
Begin Catch
Raiserror 50001 N'插入數據過程中發生錯誤.'
Rollback Tran
End Catch
Set Identity_Insert [Person.Address] Off
*/
#1
CREATE proc spGenInsertSQL (@tablename varchar(256))
as
begin
declare @sql varchar(8000)
declare @sqlValues varchar(8000)
set @sql =' ('
set @sqlValues = 'values (''+'
select @sqlValues = @sqlValues + cols + ' + '','' + ' ,@sql = @sql + '[' + name + '],'
from
(select case
when xtype in (48,52,56,59,60,62,104,106,108,122,127)
then 'case when '+ name +' is null then ''NULL'' else ' + 'cast('+ name + ' as varchar)'+' end'
when xtype in (58,61)
then 'case when '+ name +' is null then ''NULL'' else '+''''''''' + ' + 'cast('+ name +' as varchar)'+ '+'''''''''+' end'
when xtype in (167)
then 'case when '+ name +' is null then ''NULL'' else '+''''''''' + ' + 'replace('+ name+','''''''','''''''''''')' + '+'''''''''+' end'
when xtype in (231)
then 'case when '+ name +' is null then ''NULL'' else '+'''N'''''' + ' + 'replace('+ name+','''''''','''''''''''')' + '+'''''''''+' end'
when xtype in (175)
then 'case when '+ name +' is null then ''NULL'' else '+''''''''' + ' + 'cast(replace('+ name+','''''''','''''''''''') as Char(' + cast(length as varchar) + '))+'''''''''+' end'
when xtype in (239)
then 'case when '+ name +' is null then ''NULL'' else '+'''N'''''' + ' + 'cast(replace('+ name+','''''''','''''''''''') as Char(' + cast(length as varchar) + '))+'''''''''+' end'
else '''NULL'''
end as Cols,name
from syscolumns
where id = object_id(@tablename)
) T
set @sql ='select ''INSERT INTO ['+ @tablename + ']' + left(@sql,len(@sql)-1)+') ' + left(@sqlValues,len(@sqlValues)-4) + ')'' from '+@tablename
print @sql
exec (@sql)
end
GO
--exec spGenInsertSQL 表名
#2
-- 导出表中数据到sql文件
create procedure [dbo].[sp_generate_insert_script]
@tablename_mask varchar(30) = NULL
as
begin
declare @tablename varchar (128)
declare @tablename_max varchar (128)
declare @tableid int
declare @columncount numeric (7,0)
declare @columncount_max numeric (7,0)
declare @columnname varchar (30)
declare @columntype int
declare @string varchar (30)
declare @leftpart varchar (8000) /* 8000 is the longest string SQLSrv7 can EXECUTE */
declare @rightpart varchar (8000) /* without having to resort to concatenation */
declare @hasident int
set nocount on
-- take ALL tables when no mask is given (!)
if (@tablename_mask is NULL)
begin
select @tablename_mask = '%'
end
-- create table columninfo now, because it will be used several times
create table #columninfo
(num numeric (7,0) identity,
name varchar(30),
usertype smallint)
select name,
id
into #tablenames
from sysobjects
where type in ('U' ,'S')
and name like @tablename_mask
-- loop through the table #tablenames
select @tablename_max = MAX (name),
@tablename = MIN (name)
from #tablenames
while @tablename <= @tablename_max
begin
select @tableid = id
from #tablenames
where name = @tablename
if (@@rowcount <> 0)
begin
-- Find out whether the table contains an identity column
select @hasident = max( status & 0x80 )
from syscolumns
where id = @tableid
truncate table #columninfo
insert into #columninfo (name,usertype)
select name, type
from syscolumns C
where id = @tableid
and type <> 37 -- do not include timestamps
-- Fill @leftpart with the first part of the desired insert-statement, with the fieldnames
select @leftpart = 'select ''insert into '+@tablename
select @leftpart = @leftpart + '('
select @columncount = MIN (num),
@columncount_max = MAX (num)
from #columninfo
while @columncount <= @columncount_max
begin
select @columnname = name,
@columntype = usertype
from #columninfo
where num = @columncount
if (@@rowcount <> 0)
begin
if (@columncount < @columncount_max)
begin
select @leftpart = @leftpart + @columnname + ','
end
else
begin
select @leftpart = @leftpart + @columnname + ')'
end
end
select @columncount = @columncount + 1
end
select @leftpart = @leftpart + ' values('''
-- Now fill @rightpart with the statement to retrieve the values of the fields, correctly formatted
select @columncount = MIN (num),
@columncount_max = MAX (num)
from #columninfo
select @rightpart = ''
while @columncount <= @columncount_max
begin
select @columnname = name,
@columntype = usertype
from #columninfo
where num = @columncount
if (@@rowcount <> 0)
begin
if @columntype in (39,47) /* char fields need quotes (except when entering NULL);
* use char(39) == ', easier readable than escaping
*/
begin
select @rightpart = @rightpart + '+'
select @rightpart = @rightpart + 'ISNULL(' + replicate( char(39), 4 ) + '+replace(' + @columnname + ',' + replicate( char(39), 4 ) + ',' + replicate( char(39), 6) + ')+' + replicate( char(39), 4 ) + ',''NULL'')'
end
else if @columntype = 35 /* TEXT fields cannot be RTRIM-ed and need quotes */
/* convert to VC 1000 to leave space for other fields */
begin
select @rightpart = @rightpart + '+'
select @rightpart = @rightpart + 'ISNULL(' + replicate( char(39), 4 ) + '+replace(convert(varchar(1000),' + @columnname + ')' + ',' + replicate( char(39), 4 ) + ',' + replicate( char(39), 6 ) + ')+' + replicate( char(39), 4 ) + ',''NULL'')'
end
else if @columntype in (58,61,111) /* datetime fields */
begin
select @rightpart = @rightpart + '+'
select @rightpart = @rightpart + 'ISNULL(' + replicate( char(39), 4 ) + '+convert(varchar(20),' + @columnname + ')+'+ replicate( char(39), 4 ) + ',''NULL'')'
end
else /* numeric types */
begin
select @rightpart = @rightpart + '+'
select @rightpart = @rightpart + 'ISNULL(convert(varchar(99),' + @columnname + '),''NULL'')'
end
if ( @columncount < @columncount_max)
begin
select @rightpart = @rightpart + '+'','''
end
end
select @columncount = @columncount + 1
end
end
select @rightpart = @rightpart + '+'')''' + ' from ' + @tablename
-- Order the select-statements by the first column so you have the same order for
-- different database (easy for comparisons between databases with different creation orders)
select @rightpart = @rightpart + ' order by 1'
-- For tables which contain an identity column we turn identity_insert on
-- so we get exactly the same content
if @hasident > 0
select 'SET IDENTITY_INSERT ' + @tablename + ' ON'
exec ( @leftpart + @rightpart )
if @hasident > 0
select 'SET IDENTITY_INSERT ' + @tablename + ' OFF'
select @tablename = MIN (name)
from #tablenames
where name > @tablename
end
end
#3
--将表数据生成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 你的表名
#4
我最后执行的时候exec spGenInsertSQL ftlw.t_machn_menu
报错:消息 102,级别 15,状态 1,第 1 行
'.' 附近有语法错误。
前面不加库名,又说对象不存在,怎么回事
#5
use ftlw
exec spGenInsertSQL t_machn_menu
exec spGenInsertSQL t_machn_menu
#6
你最好
这种错误还是避免
use ftlw
create proc spGenInsertSQL
exec spGenInsertSQL t_machn_menu
这种错误还是避免
#7
也可以手工生成脚本的
#8
一条一条再插回去很慢的哦,用导入导出工具。
#9
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'
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
#10
e.g.
use AdventureWorks2008
GO
Exec dbo.sp_InsertSQL 'Person.Address','Person.Address'
Go
/*
Set Identity_Insert [Person.Address] On
Begin Try
Begin Tran
Insert Into Person.Address ([AddressID],[AddressLine1],[AddressLine2],[City],[StateProvinceID],[PostalCode],[rowguid],[ModifiedDate])
Select 1,N'1970 Napa Ct.',Null,N'Bothell',79,N'98011',N'9AADCB0D-36CF-483F-84D8-585C2D4EC6E9','1998-01-04 00:00:00.000' Union All
Select 2,N'9833 Mt. Dias Blv.',Null,N'Bothell',79,N'98011',N'32A54B9E-E034-4BFB-B573-A71CDE60D8C0','1999-01-01 00:00:00.000' Union All
Select 3,N'7484 Roundtree Drive',Null,N'Bothell',79,N'98011',N'4C506923-6D1B-452C-A07C-BAA6F5B142A4','2003-04-08 00:00:00.000' Union All
Select 4,N'9539 Glenside Dr',Null,N'Bothell',79,N'98011',N'E5946C78-4BCC-477F-9FA1-CC09DE16A880','1999-03-07 00:00:00.000' Union All
Select 5,N'1226 Shoe St.',Null,N'Bothell',79,N'98011',N'FBAFF937-4A97-4AF0-81FD-B849900E9BB0','1999-01-20 00:00:00.000' Union All
Select 6,N'1399 Firestone Drive',Null,N'Bothell',79,N'98011',N'FEBF8191-9804-44C8-877A-33FDE94F0075','1999-03-17 00:00:00.000' Union All
Select 7,N'5672 Hale Dr.',Null,N'Bothell',79,N'98011',N'0175A174-6C34-4D41-B3C1-4419CD6A0446','2000-01-12 00:00:00.000' Union All
Select 8,N'6387 Scenic Avenue',Null,N'Bothell',79,N'98011',N'3715E813-4DCA-49E0-8F1C-31857D21F269','1999-01-18 00:00:00.000' Union All
Select 9,N'8713 Yosemite Ct.',Null,N'Bothell',79,N'98011',N'268AF621-76D7-4C78-9441-144FD139821A','2002-07-01 00:00:00.000' Union All
Select 10,N'250 Race Court',Null,N'Bothell',79,N'98011',N'0B6B739D-8EB6-4378-8D55-FE196AF34C04','1999-01-03 00:00:00.000' Union All
Select 11,N'1318 Lasalle Street',Null,N'Bothell',79,N'98011',N'981B3303-ACA2-49C7-9A96-FB670785B269','2003-04-01 00:00:00.000' Union All
.... ...
Select 32517,N'177 11th Ave',Null,N'Sammamish',79,N'98074',N'2F76ECD8-B22C-4353-BE61-09FE000E13CE','1999-01-30 00:00:00.000' Union All
Select 32518,N'8040 Hill Ct',Null,N'Redmond',79,N'98052',N'0E5F2D4F-C38F-406C-9169-8CCD754DBF94','1999-02-20 00:00:00.000' Union All
Select 32519,N'137 Mazatlan',Null,N'Seattle',79,N'98104',N'7B2B1EE6-43A4-426D-8A7D-54DD451F716B','1999-03-15 00:00:00.000' Union All
Select 32520,N'5863 Sierra',Null,N'Bellevue',79,N'98004',N'F72A65AD-0FD0-42FD-8760-4B83DE543926','1999-02-16 00:00:00.000' Union All
Select 32521,N'7145 Matchstick Drive',Null,N'Sammamish',79,N'98074',N'868501B7-4B9C-4F27-896B-342689EA5980','1999-02-16 00:00:00.000'
Commit Tran
End Try
Begin Catch
Raiserror 50001 N'插入數據過程中發生錯誤.'
Rollback Tran
End Catch
Set Identity_Insert [Person.Address] Off
*/