如何将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 ...
随机推荐
- HDU 4359 Easy Tree DP? 带权二叉树的构造方法 dp
题意: 给定n deep 1.构造一个n个节点的带权树,且最大深度为deep,每一个节点最多仅仅能有2个儿子 2.每一个节点的值为2^0, 2^1 ··· 2^(n-1) 随意两个节点值不能同样 3 ...
- Ubuntu 14.04 Android 使用Maven一个 用例project
在说明书前面描述SDK通过使用Ant发展. 本文试图在此基础上使用Maven发展. 在这里,我们需要使用maven-android-plugin. 在本文中,参考官方文件: https://code. ...
- js实现鼠标拖拽div-------Day44
假设去问这样一个问题"你认为鼠标操作简单,还是键盘操作简单",相信会有多数人都会回答鼠标吧,毕竟键盘button那么多,假设手小了或者手法不规范了,太easy出问题了,也对操作的速 ...
- head first c<11>在根据网络编程
博文可以在一个大的网络通信实现,但是,一个人只能起到,我们能够给每个clientfork()子进程,实现诸多的服务. 方法: client连到server以后,server启动一个新创建的套接字对话. ...
- java 遍历树节点 同时保留所有的从根到叶节点的路径
直接在代码.稍后细说 数据结构定义: /** * */ package Servlet; import java.util.ArrayList; import java.util.List; /** ...
- Java读书笔记三(字符串)
1.介绍 本篇博客将对JAVA中的字符串类的基本知识进行介绍.主要字符串类的一些经常用法等内容. 2.字符串对象的创建 1.有两种形式.可是在开发中常常习惯于String 变量名的形式来进行操作. & ...
- NSIS:应用软件自动升级功能的探索与实践
原文 NSIS:应用软件自动升级功能的探索与实践 记得以前轻狂曾分享过使用第三方软件实现应用软件自动升级功能 (详细http://www.flighty.cn/html/soft/20110106_1 ...
- Windows Cygwin Redis 安装(转)
在win平台下编译Redis一般有两种方式: 1. 基于MS VC进行编译,生成原生可执行文件 该方式需要创建MSVC项目文件以及对Redis源码进行适当调整. 这里提供一个可行版本,由微软开放团队进 ...
- 整理php操作memcache缓存为基础的方法
php操作memcache共享缓存方法 采用memcache的前提下,是需要在服务器端被配置memcahche环境! 证实memcahce经过正常的连接可以在程序中使用! <?php /** * ...
- 移动web开发经验总结(1) (转)
1.<meta name="viewport" content="initial-scale=1.0, maximum-scale=1.0, minimum-sca ...