如何将sqlserver表中的数据导出sql语句或生成insert into语句 [转]
输入表名,生成插入语句
drop proc proc_insert //如果存在就删除
go
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
select * from systypes
使用方法:
exec proc_insert 你的表名
选择【执行模式】为“以文本显示结果”,然后将运行后的结果存成.sql,加上用SQL Server生成的数据库脚本就可以了。
另
外可以利用第三方工具,导出饮食备注型字段的数据数据可以用powerbuilder。在database
painter里面,用SQL选出,或者直接打开表,点击生成的list datawindow,然后在菜单file->save rows
as->选择SQL,那么生成的SQL语句就包括建表和insert数据的SQL了。
还有最完善的方式就是自己编程实现导入导出,这样可以应对所有情况的数据转换。
如何将sqlserver表中的数据导出sql语句或生成insert into语句 [转]的更多相关文章
- 【MYSQL 清空所有的的表中的数据的SQL的生成】
MYSQL 清空所有的的表中的数据的SQL的生成 select Concat('TRUNCATE TABLE ', TABLE_NAME, ';') from INFORMATION_SCHEMA.T ...
- Sql server中用现有表中的数据创建Sql的Insert插入语句
之前,在Codeproject发表过一篇关于用现有表中数据创建Insert的Sql语句的存储过程,今天将其搬到这里来,注意本存储过程仅适用于SQL SERVER. 介绍 一些时候,你想导出一些现有表中 ...
- 如何将hive表中的数据导出
近期经常将现场的数据带回公司测试,所以写下该文章,梳理一下思路. 1.首先要查询相应的hive表,比如我要将c_cons这张表导出,我先查出hive中是否有这张表. 查出数据,证明该表在hive中存在 ...
- MSSQL中把表中的数据导出成Insert
use master go if exists (select name from sysobjects where name = 'sp_generate_insert_script') begin ...
- 查询和删除数据表中反复数据的sql
1.查询表中反复数据. select * from people where peopleId in (select peopleId from people group by ...
- 表中相同数据的sql语句
1.查找表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断select * from peoplewhere peopleId in (select peopleId from ...
- 点滴积累【other】---存储过程删除所有表中的数据(sql)
USE [QG_Mis24] GO /****** Object: StoredProcedure [dbo].[p_set1] Script Date: 07/18/2013 13:25:57 ** ...
- Qt中将QTableView中的数据导出为Excel文件
如果你在做一个报表类的程序,可能将内容导出为Excel文件是一项必须的功能.之前使用MFC的时候我就写过一个类,用于将grid中的数据导出为Excel文件.在使用了QtSql模块后,我很容易的将这个类 ...
- Mysql 导出数据库和指定表中的数据
参考地址:http://jingyan.baidu.com/article/b7001fe14240ab0e7282dde9.html [root@youo zw]# mysqldump -u roo ...
随机推荐
- [LeetCode] Search in Rotated Sorted Array II [36]
称号 Follow up for "Search in Rotated Sorted Array": What if duplicates are allowed? Would t ...
- SSL探03
本文探讨了Openssl的Engine机械.Openssl硬件引擎(Engine)可以使用户比較easy地将自己的硬件增加到openssl中去,替换其提供的软件算法. ENGINE 是 OPENSSL ...
- RH133读书笔记(8)-Lab 8 Manage Network Settings
Lab 8 Manage Network Settings Goal: To build skills needed to manually configure networking Estimate ...
- jquery:ajax不接收返回值回
html页面a加元素的假设href=javasrcipt:void(0)会导致ajax没有收到回后台值. : <p class="chatmsg_load_more"> ...
- net大型分布式电子商务架构
net大型分布式电子商务架构 背景 构建具备高可用,高扩展性,高性能,能承载高并发,大流量的分布式电子商务平台,支持用户,订单,采购,物流,配送,财务等多个项目的协作,便于后续运营报表,分析,便于运维 ...
- POJ2195 Going Home 【最小费用流】+【最佳匹配图二部】
Going Home Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 18169 Accepted: 9268 Descr ...
- 初步swift语言学习笔记6(ARC-自己主动引用计数,内存管理)
笔者:fengsh998 原文地址:http://blog.csdn.net/fengsh998/article/details/31824179 转载请注明出处 假设认为文章对你有所帮助.请通过留言 ...
- [SignalR]注册路由
原文:[SignalR]注册路由 1.注册SignalR路由 在Asp.Net中,若是SignalR 1.*版本,在Global.asax文件中定义如下: 在Asp.Net中,若是SignalR 2. ...
- SSH骨架Struts(1)——Struts执行过程
收养Struts骨架Web应用,以举例的方式介绍的基本流程. 一.实例 Login.jsp,进行系统登录的页面 <form action="login.do" method= ...
- strtok、strtok_s、strtok_r 字符串切割函数
1.strtok函数 函数原型:char * strtok (char *str, const char * delimiters); 參数:str,待切割的字符串(c-string):delimit ...