sql server根据表中数据生成insert语句

本文涉及的产品
云数据库 RDS SQL Server,基础系列 2核4GB
简介: 几个收藏的根据数据库生成Insert语句的存储过程[修正版]-- ======================================================--根据表中数据生成insert语句的存储过程--建立存储过程,执行spGenInsertSQL 表名--...
几个收藏的根据数据库生成Insert语句的存储过程[修正版]
复制代码
 
   
-- ======================================================
--
根据表中数据生成insert语句的存储过程
--
建立存储过程,执行spGenInsertSQL 表名
--
感谢playyuer
--
--感谢szyicol
--
======================================================
CREATE proc [ dbo ] . [ spGenInsertSQL ]
(
@tablename varchar ( 256 ))
as
begin
declare @sql varchar ( 8000 )
declare @sqlValues varchar ( 8000 )

set @sql = ' ( '
set @sqlValues = ' values ( '' + '
select @sqlValues = @sqlValues + cols + ' + '' , '' + ' , @sql = @sql + ' [ ' + name + ' ], '
from
(
select case
when xtype in ( 48 , 52 , 56 , 59 , 60 , 62 , 104 , 106 , 108 , 122 , 127 )
then ' case when [ ' + name + ' ] is null then '' NULL '' else ' + ' cast([ ' + name + ' ] as varchar) ' + ' end '
when xtype in ( 58 , 61 )
then ' case when [ ' + name + ' ] is null then '' NULL '' else ' + ''''''''' + ' + ' cast([ ' + name + ' ] as varchar) ' + ' + ''''''''' + ' end '
when xtype in ( 167 )
then ' case when [ ' + name + ' ] is null then '' NULL '' else ' + ''''''''' + ' + ' replace([ ' + name + ' ], '''''''' , '''''''''''' ) ' + ' + ''''''''' + ' end '
when xtype in ( 231 )
then ' case when [ ' + name + ' ] is null then '' NULL '' else ' + ''' N '''''' + ' + ' replace([ ' + name + ' ], '''''''' , '''''''''''' ) ' + ' + ''''''''' + ' end '
when xtype in ( 175 )
then ' case when [ ' + name + ' ] is null then '' NULL '' else ' + ''''''''' + ' + ' cast(replace([ ' + name + ' ], '''''''' , '''''''''''' ) as Char( ' + cast (length as varchar ) + ' ))+ ''''''''' + ' end '
when xtype in ( 239 )
then ' case when [ ' + name + ' ] is null then '' NULL '' else ' + ''' N '''''' + ' + ' cast(replace([ ' + name + ' ], '''''''' , '''''''''''' ) as Char( ' + cast (length as varchar ) + ' ))+ ''''''''' + ' end '
else ''' NULL '''
end as Cols,name
from syscolumns
where id = object_id ( @tablename )
) T
set @sql = ' select '' INSERT INTO [ ' + @tablename + ' ] ' + left ( @sql , len ( @sql ) - 1 ) + ' ) ' + left ( @sqlValues , len ( @sqlValues ) - 4 ) + ' ) '' from ' + @tablename
-- print @sql
exec ( @sql )
end

-- ======================================================
--
根据表中数据生成insert语句的存储过程
--
建立存储过程,执行proc_insert 表名
--
感谢Sky_blue
--
感谢szyicol
--
======================================================
CREATE proc [ dbo ] . [ proc_insert ] ( @tablename varchar ( 256 ))
as
begin
set nocount on
-- declare @tablename varchar(256)
-- set @tablename = 'AD'
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
复制代码

原文:几个收藏的根据数据库生成Insert语句的存储过程

修正了表中的字段如果是SQL中的关键字(如Order)时,生成的脚本执行会出错的bug

目录
相关文章
|
1月前
|
SQL 人工智能 JSON
Flink 2.1 SQL:解锁实时数据与AI集成,实现可扩展流处理
简介:本文整理自阿里云高级技术专家李麟在Flink Forward Asia 2025新加坡站的分享,介绍了Flink 2.1 SQL在实时数据处理与AI融合方面的关键进展,包括AI函数集成、Join优化及未来发展方向,助力构建高效实时AI管道。
405 43
|
26天前
|
SQL 人工智能 JSON
Flink 2.1 SQL:解锁实时数据与AI集成,实现可扩展流处理
本文整理自阿里云的高级技术专家、Apache Flink PMC 成员李麟老师在 Flink Forward Asia 2025 新加坡[1]站 —— 实时 AI 专场中的分享。将带来关于 Flink 2.1 版本中 SQL 在实时数据处理和 AI 方面进展的话题。
123 0
Flink 2.1 SQL:解锁实时数据与AI集成,实现可扩展流处理
|
2月前
|
SQL
SQL如何只让特定列中只显示一行数据
SQL如何只让特定列中只显示一行数据
|
6月前
|
SQL 自然语言处理 数据库
【Azure Developer】分享两段Python代码处理表格(CSV格式)数据 : 根据每列的内容生成SQL语句
本文介绍了使用Python Pandas处理数据收集任务中格式不统一的问题。针对两种情况:服务名对应多人拥有状态(1/0表示),以及服务名与人名重复列的情况,分别采用双层for循环和字典数据结构实现数据转换,最终生成Name对应的Services列表(逗号分隔)。此方法高效解决大量数据的人工处理难题,减少错误并提升效率。文中附带代码示例及执行结果截图,便于理解和实践。
151 4
|
2月前
|
SQL
SQL中如何删除指定查询出来的数据
SQL中如何删除指定查询出来的数据
|
2月前
|
SQL 关系型数据库 MySQL
SQL如何对不同表的数据进行更新
本文介绍了如何将表A的Col1数据更新到表B的Col1中,分别提供了Microsoft SQL和MySQL的实现方法,并探讨了多表合并后更新的优化方式,如使用MERGE语句提升效率。适用于数据库数据同步与批量更新场景。
|
3月前
|
SQL DataWorks 数据管理
SQL血缘分析实战!数据人必会的3大救命场景
1. 开源工具:Apache Atlas(元数据管理)、Spline(血缘追踪) 2. 企业级方案:阿里DataWorks血缘分析、腾讯云CDW血缘引擎 3. 自研技巧:在ETL脚本中植入版本水印,用注释记录业务逻辑变更 📌 重点总结:
|
4月前
|
SQL 数据挖掘 关系型数据库
【SQL 周周练】一千条数据需要做一天,怎么用 SQL 处理电表数据(如何动态构造自然月)
题目来自于某位发帖人在某 Excel 论坛的求助,他需要将电表缴费数据按照缴费区间拆开后再按月份汇总。当时用手工处理数据,自称一千条数据就需要处理一天。我将这个问题转化为 SQL 题目。
162 12
|
4月前
|
SQL 数据采集 资源调度
【SQL 周周练】爬取短视频发现数据缺失,如何用 SQL 填充
爬虫爬取抖音和快手的短视频数据时,如果遇到数据缺失的情况,如何使用 SQL 语句完成数据的补全。
110 5
|
6月前
|
SQL 容灾 关系型数据库
阿里云DTS踩坑经验分享系列|DTS打通SQL Server数据通道能力介绍
SQL Server 以其卓越的易用性和丰富的软件生态系统,在数据库行业中占据了显著的市场份额。作为一款商业数据库,外部厂商在通过解析原生日志实现增量数据捕获上面临很大的挑战,DTS 在 SQL Sever 数据通道上深研多年,提供了多种模式以实现 SQL Server 增量数据捕获。用户可以通过 DTS 数据传输服务,一键打破自建 SQL Server、RDS SQL Server、Azure、AWS等他云 SQL Server 数据孤岛,实现 SQL Server 数据源的流动。
309 0
阿里云DTS踩坑经验分享系列|DTS打通SQL Server数据通道能力介绍