如何将sqlserver表中的数据导出sql语句或⽣成insertinto语句
[转]
输⼊表名,⽣成插⼊语句
drop proc proc_insert //如果存在就删除
go
create proc proc_insert (@tablename varchar(256))
as
begin
set nocount on
sqlserver备份表语句declare @sqlstr varchar(4000)
declare @sqlstr1 varchar(4000)
declare @sqlstr2 varchar(4000)
select @sqlstr='select ''insert '+@tablename
select @sqlstr1=''
select @sqlstr2=' ('
select @sqlstr1= ' values ( ''+'
select @sqlstr1=@sqlstr1+col+'+'',''+' ,@sqlstr2=@sqlstr2+name +',' from (select case
-- pe =173 then 'case when '+a.name+' is null then ''NULL'' else
'+'convert(varchar('+convert(varchar(4),a.length*2+2)+'),'+a.name +')'+' end'
pe =127 then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar(20),'+a.name +')'+' end'
pe =104 then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar(1),'+a.name +')'+' end'
pe =175 then 'case when '+a.name+' is null then ''NULL'' else '+'''''''''+'+'replace('+a.name+','''''''','''''''''''')' + '+'''''''''+' end'
pe =61 then 'case when '+a.name+' is null then ''NULL'' else '+'''''''''+'+'convert(varchar(23),'+a.name +',121)'+ '+'''''''''+' end' pe =106 then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar('+convert(varchar(4),a.xprec+2)+'),'+a.name +')'+' end'
pe =62 then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar(23),'+a.name +',2)'+' end'
pe =56 then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar(11),'+a.name +')'+' end'
pe =60 then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar(22),'+a.name +')'+' end'
pe =239 then 'case when '+a.name+' is null then ''NULL'' else '+'''''''''+'+'replace('+a.name+','''''''','''''''''''')' + '+'''''''''+' end'
pe =108 then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar('+convert(varchar(4),a.xprec+2)+'),'+a.name +')'+' end'
pe =231 then 'case when '+a.name+' is null then ''NULL'' else '+'''''''''+'+'replace('+a.name+','''''''','''''''''''')' + '+'''''''''+' end'
pe =59 then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar(23),'+a.name +',2)'+' end'
pe =58 then 'case when '+a.name+' is null then ''NULL'' else '+'''''''''+'+'convert(varchar(23),'+a.name +',121)'+ '+'''''''''+' end' pe =52 then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar(12),'+a.name +')'+' end'
pe =122 then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar(22),'+a.name +')'+' end'
pe =48 then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar(6),'+a.name +')'+' end'
-- pe =165 then 'case when '+a.name+' is null then ''NULL'' else
'+'convert(varchar('+convert(varchar(4),a.length*2+2)+'),'+a.name +')'+' end'
pe =167 then 'case when '+a.name+' is null then ''NULL'' else '+'''''''''+'+'replace('+a.name+','''''''','''''''''''')' + '+'''''''''+' end'
else '''NULL'''
end as lid,a.name
from syscolumns a where a.id = object_id(@tablename) pe <>189 pe <>34 pe <>35 pe <>36
)t order by colid
select @sqlstr=@sqlstr+left(@sqlstr2,len(@sqlstr2)-1)+') '+left(@sqlstr1,len(@sqlstr1)-3)+')'' from '+@tablename
-- print @sqlstr
exec( @sqlstr)
set nocount off
end
go
select * from systypes
使⽤⽅法:
exec proc_insert 你的表名
选择【执⾏模式】为“以⽂本显⽰结果”,然后将运⾏后的结果存成.sql,加上⽤SQL Server⽣成的数据库脚本就可以了。
另外可以利⽤第三⽅⼯具,导出饮⾷备注型字段的数据数据可以⽤powerbuilder。在database painter⾥⾯,⽤SQL选出,或者直接打开
表,点击⽣成的list datawindow,然后在菜单file->save rows as->选择SQL,那么⽣成的SQL语句就包括建表和insert数据的SQL了。还有最完善的⽅式就是⾃⼰编程实现导⼊导出,这样可以应对所有情况的数据转换。
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论