导出sqlserver2005数据表为sql语句

cnhnhsh 2008-05-08 12:21:03
请教高手们:如何把sqlserver2005数据库中的某个表导出为sql语语言的insert语句(当然要包括数据),谢谢。
...全文
832 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
ljm311 2010-07-19
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 cnhnhsh 的回复:]
谢谢,但在sqlserver2005查询中执行EXEC pGetInsertSQL('dept')时提示有语法错误,其中dept是数据表。
[/Quote]
执行方式:exec pGetInsertSQL dept
然后将生成的结果导出为csv即可。
忆轩辕 2008-05-13
  • 打赏
  • 举报
回复
一个简单的方法。。。。。。。select * from 表名

把结果复制到excel表格里面,自己手工完成语句

不过这个方法不适合大数据量的表
utpcb 2008-05-12
  • 打赏
  • 举报
回复
你可以用软代码生成器
pgy8288 2008-05-10
  • 打赏
  • 举报
回复
http://download.csdn.net/source/336474
就是专门导出insert语句的。
wangxuelid 2008-05-08
  • 打赏
  • 举报
回复
jl
dawugui 2008-05-08
  • 打赏
  • 举报
回复
--参考:

SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO

CREATE proc pGetInsertSQL (@TableName varchar(256))
as
begin
set nocount on
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
-- 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 =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
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 '+@tablename
-- print @sqlstr
exec( @sqlstr)
set nocount off
end

GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO
lishuaide0517 2008-05-08
  • 打赏
  • 举报
回复
用个游标写吧
declare @id int
declare @year nvarchar(4)
declare @decrip nvarchar(50)
declare c_cursor cursor
for
select * from tab1
open c_cursor
fetch next from c_cursor into @id,@year,@decrip
while @@fetch_status=0
begin

print 'id:'+cast(@id as nvarchar(10))+' year:'+@year+'\n'
fetch next from c_cursor into @id,@year,@decrip
end
close c_cursor
deallocate c_cursor
pgy8288 2008-05-08
  • 打赏
  • 举报
回复
http://download.csdn.net/source/336474
一个专门针对SQL Server的数据导出小工具.
Novelty 2008-05-08
  • 打赏
  • 举报
回复
这是从microsoft得到的代码http://download.csdn.net/source/445640
cnhnhsh 2008-05-08
  • 打赏
  • 举报
回复
本人的意思是求出从sqlserver2005数据库里导出某个数据表的insert语句代码,而不是数据表的结果集,楼上兄弟的代码还不能满足要求。
cnhnhsh 2008-05-08
  • 打赏
  • 举报
回复
谢谢,但在sqlserver2005查询中执行EXEC pGetInsertSQL('dept')时提示有语法错误,其中dept是数据表。

27,579

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 应用实例
社区管理员
  • 应用实例社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧