将表数据生成Insert脚本 比较好用的生成插入语句的SQL脚本
时间:2024-01-21 13:11:19
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go
-- =============================================
-- Author: 华岭
-- Create date: 2008-10-28
-- Description: 将表数据生成Insert脚本
-- Demo : exec pCreateInsertScript 'BexmCodeType','dictypeid = 61'
-- =============================================
alter proc [dbo].pCreateInsertScript (@tablename varchar(256),@con nvarchar(400))
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 =127 then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar(6),'+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 + ' where 1=1 and ' + isnull(@con,'')
print @sqlstr
exec( @sqlstr)
set nocount off
end
标签:SQL,插入语句
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Persits AspJpeg 1.8+ 轻松实现透明文字去锯齿水印
2009-03-20 14:03:00
一个asp分页源代码例子
2007-09-19 12:25:00
用python删除文件夹中的重复图片(图片去重)
2021-07-23 22:42:36
代码分析Python地图坐标转换
2022-01-15 19:50:48
Python命令行库click的具体使用
2023-07-05 02:51:16
如何修复MySQL数据库表
2009-03-20 13:24:00
element使用自定义icon图标的两种解决方式
2023-07-02 16:55:05
![](https://img.aspxhome.com/file/2023/8/139818_0s.jpg)
Python爬虫:Request Payload和Form Data的简单区别说明
2021-11-14 14:01:24
Highcharts 图表中图例显示状态存储的功能设计详解
2023-05-30 02:01:09
python 实现自动远程登陆scp文件实例代码
2022-04-02 20:25:31
sqlserver数据库优化解析(图文剖析)
2024-01-17 08:55:37
![](https://img.aspxhome.com/file/2023/8/98008_0s.jpg)
最详细的python工具Anaconda+Pycharm安装教程
2022-01-15 10:20:30
![](https://img.aspxhome.com/file/2023/2/75262_0s.png)
numpy中的掩码数组的使用
2022-10-19 23:36:46
![](https://img.aspxhome.com/file/2023/5/118615_0s.png)
Django生成PDF文档显示网页上以及PDF中文显示乱码的解决方法
2022-01-05 22:02:07
![](https://img.aspxhome.com/file/2023/9/80649_0s.jpg)
python 调用c语言函数的方法
2023-12-11 17:24:21
Python 内置函数速查表一览
2021-10-30 07:43:06
Python实战项目用PyQt5制作漫画脸GUI界面
2023-07-05 13:17:19
![](https://img.aspxhome.com/file/2023/9/59309_0s.png)
pandas数据处理进阶详解
2023-04-15 03:40:33
如何巧妙利用SQL Server的EXISTS结构
2009-02-19 17:36:00
Python数据结构与算法之完全树与最小堆实例
2021-01-10 06:58:47