问答文章1 问答文章501 问答文章1001 问答文章1501 问答文章2001 问答文章2501 问答文章3001 问答文章3501 问答文章4001 问答文章4501 问答文章5001 问答文章5501 问答文章6001 问答文章6501 问答文章7001 问答文章7501 问答文章8001 问答文章8501 问答文章9001 问答文章9501

如何为SQL Server表数据生成insert脚本

发布网友 发布时间:2022-05-04 08:09

我来回答

4个回答

热心网友 时间:2022-05-04 09:39

以下存储过程可以实现:

CREATE?PROCEDURE?dbo.UspOutputData?
@tablename?sysname?
AS?
declare?@column?varchar(1000)?
declare?@columndata?varchar(1000)?
declare?@sql?varchar(4000)?
declare?@xtype?tinyint?
declare?@name?sysname?
declare?@objectId?int?
declare?@objectname?sysname?
declare?@ident?int?
set?nocount?on?
set?@objectId=object_id(@tablename)?
if?@objectId?is?null?--?判断对象是否存在?
begin?
print?'The?object?not?exists'?
return?
end?
set?@objectname=rtrim(object_name(@objectId))?
if?@objectname?is?null?or?charindex(@objectname,@tablename)=0?--此判断不严密?
begin?
print?'

热心网友 时间:2022-05-04 10:57

--别人写的存储过程,直接调用可以生成insert语句

-- ======================================================

--根据表中数据生成insert语句的存储过程

--建立存储过程,执行 proc_insert 表名

--感谢Sky_blue

-- ======================================================

--exec proc_insert 'MoveType'

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 =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

热心网友 时间:2022-05-04 12:31

表数据生成i
声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com
金莎丽的淋浴房用完之后开门水会流出来吗? 老家里的开门式淋浴房总是漏水,装修新家想换个质量好点的淋浴房,淋浴房... 淋浴房为何开门不畅?门关不紧怎么办? appa是什么意思英语? ...觉得比较好奇吧。是什么意思?好奇吗。有微信,没聊过 和新认识的男生聊什么跟男生可聊的20个话题 农行用什么pos机有积分 POS机刷卡积分是什么意思? 一个简短的民间小说 小米9.11发布会什么时候可以重播 自己在家染发,难免会蹭到脸上、手上,如何去除染发膏残留在皮肤上的痕迹呢? 恋爱多久才能做到真正了解对方? 如何了解对方的想法? 怎么知道对方是不是那个对的人? 怎么才能知道对方看了我给她发的信息了- 问一问 如何知道对方真实所想到底是什么? 痛风病人尿酸高,日常生活中可以食用醋不?会不会导致尿酸增长? 天天喝醋和尿酸值升高有没有关系我天天早 吃反复加热的甜醋会尿酸高吗? 尿酸高的人能否吃醋,经常吃醋多的人会引起尿酸高 天天喝醋和尿酸值升高有没有关系我天天早晨醋泡蛋液 喝醋吃酸菜会使尿酸加高吗? 醋吃多影响尿酸偏高吗 醋吃多了会引起尿酸检测高吗 食用陈醋过多会导致尿酸过高吗? 吃醋多了尿酸会增高吗 天天吃醋会不会尿酸高 罗纳河老藤干红2011年的可以收藏吗 《圣斗士星矢》老教皇的死有多壮烈? 勃艮第牛肉怎么做? 冰箱结冰多怎么处理 怎样才能让别人回信息? 怎样才能让他回我消息 如何提醒别人回微信 怎么让对方迅速回复你 发信息给喜欢的人,他不回,怎样才能让她回信息? 分手后如何让对方积极回复自己信息? 女孩子总是不回信息,如何做才能让她回我信息? 发信息给他 就是一直故意回复嗯啊哦 该怎样让他正常回我信息 SQL Server里面如何导出包含(insert into)数据的SQL脚本 昆明CCNA培训 我是自学CCNA的,我该怎么报名参加考试呢? sql server geomtry 类型数据怎么导出生成Insert语句 孜然牛肉怎么做好吃又抗寒 孜然洋葱牛肉怎么做 孜然牛肉的来历是什么到底是什么? sql server 2008 如何用表数据生成sql插入语句 炖鸡肉的汤能炖羊肉吗有没相克 股票软件怎么看深证A股 2007年3月27日以后深圳股市行情