`

如何将sqlserver表中的数据导出sql语句或生成insert into语句

 
阅读更多
create proc proc_insert (@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 =127 then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar(20),'+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



使用方法:

exec proc_insert 你的表名


另外可以利用第三方工具,导出饮食备注型字段的数据数据可以用powerbuilder。在database painter里面,用SQL选出,或者直接打开表,点击生成的list datawindow,然后在菜单file->save rows as->选择SQL,那么生成的SQL语句就包括建表和insert数据的SQL了。
还有最完善的方式就是自己编程实现导入导出,这样可以应对所有情况的数据转换。
分享到:
评论

相关推荐

    从SQLSERVER数据库中导出记录生成insert into 语句

    从SQLSERVER数据库中导出记录生成insert into 语句

    Sql Server导出数据 INSERT 脚本

    可以将数据库中的数据导出insert into脚本, sql server2008以上版本已经自带导出数据库数据成insert脚本功能,该工具针对2005以下版本.使用时先选择数据库服务器地址,在右侧选择Sql Server login,输入数据库账号...

    SQL SERVER:把表里的数据导出成为INSERT INTO脚本的存储过程

    SQL SERVER:把表里的数据导出成为INSERT INTO脚本的存储过程

    将sql server数据表生成sql文件,可用查询分析器直接运行

    可将sql server 数据表生成sql语句 语句中包括建表部分和数据部分 常用作sql server 数据导入 例如:--表 buyi_product_comment 的结构 CREATE TABLE buyi_product_comment(proId varchar(50) not null,companyid ...

    sql server导出成sql语句

    由于sql server2005里没有像oracle那样将数据导出成sql语句(insert into)的功能。所以,写的一个存储过程,该存储过程将提成的表的数据全部生成insert into.....这样的插入语句。以便数据的复制。

    把msSqlServer 中的数据导出成 insert into语句

    把msSqlServer 中的数据导出成 insert into语句

    生成表记录的insert语句

    根据表中的记录生成insert语句的脚本

    通用数据导出工具

    怎样将表中数据导出,形成insert into 表名 values(...)这样的语句?独此一家 , 别无分号 支持sqlserver access oracle mysql 就用这个软件好了~!

    Office数据库升迁 Access 导入 SQL Server

    Office数据库升迁 Access 导入 SQL Server,相信大家都知道 SQL-Server 数据库导入导出功能吧,但当你装了精简版的SQL-Server这个功能就会没有了,也就是无法实现数据库导入导出,那么怎么办 写SQL语句? Insert ...

    excel导入sql2005的几种方法归纳

    [SQL]将Excel表数据导入SQL Server2005的几种方法归纳 数据库 2010-07-27 11:14:26 阅读201 评论0 字号:大中小 订阅 近日在巨轮着手车间负荷数据处理,反馈回来的数据是保存在Excel文件中的,我必须将其导入SQL ...

    sqlserver 导出插入脚本代码

    代码如下: DECLARE @tbImportTables table(tablename varchar(128), deleted tinyint) — append tables which you want to import Insert Into @tbImportTables(tablename, deleted) values(‘tentitytype’, 1) ...

    PL/SQL Developer8.04官网程序_keygen_汉化

    为了处理SQL语句,Oracle将在内存中分配一个区域,这就是上下文区。这个区包含了已经处理完的行数、指向被分析语句的指针,整个区是查询语句返回的数据行集。游标就是指向上下文区句柄或指针。  1 显示游标 显示...

    ORCALE语句大全

    快速加大表中数据 insert into student(SNo,Name,Sex,Birthday,Salary) select * from student; www.2cto.com 改 update student set Name='陈慧琳',Sex='女' where SNo=1002; 删 savepoint a; delete ...

    jsqlformatter:Java SQL格式化程序,美化程序和漂亮打印机

    已针对数百种复杂,真实SQL语句进行了测试待办事项/计划中添加对CREATE,ALTER,DELETE,SELECT INTO语句的支持检测并引用保留的关键字名称根据SQL方言添加格式选项(例如,逗号开头/结尾,贪婪空格,上/下/驼峰式...

    ASP与SQL网页数据库程序设计

    4-3 使用向导创建索引 158 6-4-4 利用“管理索引”窗口管理表索引 161 6-5 创建关联 162 6-6 数据输入 165 6-6-1 在表窗口中输入数据 165 6-6-2 导入数据 166 6-6-3 使用INSERT INTO语句 171...

    ASP和SQL网站数据库程序设计(DOC)

    6-6-3 使用INSERT INTO语句 171 第7章 数据库的备份与管理……………….173 7-1 生成数据库的SQL脚本 174 7-1-1 生成SQL脚本窗口 174 7-1-2 执行生成SQL脚本 179 7-1-3 执行SQL脚本文件 181 7-2 收缩...

    Delphi开发范例宝典目录

    实例071 在TListView控件中对数据排序或统计 84 实例072 在TListView组件中绘制底纹 86 实例073 在列表视图中拖动视图项 87 2.6 TTreeView控件应用典型实例 88 实例074 将数据库数据显示到树视图中 88 ...

    PLSQLDeveloper下载

    为了处理SQL语句,Oracle将在内存中分配一个区域,这就是上下文区。这个区包含了已经处理完的行数、指向被分析语句的指针,整个区是查询语句返回的数据行集。游标就是指向上下文区句柄或指针。 显示游标  显示游标...

Global site tag (gtag.js) - Google Analytics