问答1 问答5 问答50 问答500 问答1000
网友互助专业问答平台

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

提问网友 发布时间:2023-11-11 06:32
声明:本网页内容为用户发布,旨在传播知识,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。
E-MAIL:1656858193@qq.com
1个回答
热心网友 回答时间:2024-05-16 07:36
使用SQL Server数据库自带的“生成SQL脚本”工具,可以生成创建表、视图、存储过程等的SQL脚本。那么,能否将表中的数据也生成为SQL脚本,在查询分析器中执行这些脚本后自动将数据导入到SQL Server中呢?答案是肯定的,示例如下:
CREATE PROCEDURE dbo.OutputData @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 @tablename + '对象不存在' return end set @objectname=rtrim(object_name(@objectId)) if @objectname is null or charindex(@objectname,@tablename)=0 begin print @tablename + '对象不在当前数据库中' return end if OBJECTPROPERTY(@objectId,'IsTable') < > 1 -- 判断对象是否是表 begin print @tablename + '对象不是表' return end select @ident=status&0x80 from syscolumns where id=@objectid and status&0x80=0x80 if @ident is not null print 'SET IDENTITY_INSERT '+ @TableName + ' ON' --定义游标,循环取数据并生成Insert语句 declare syscolumns_cursor cursor for select c.name,c.xtype from syscolumns c where c.id=@objectid order by c.colid --打开游标 open syscolumns_cursor set @column='' set @columndata='' fetch next from syscolumns_cursor into @name,@xtype while @@fetch_status <> -1 begin if @@fetch_status <> -2 begin if @xtype not in(189,34,35,99,98) --timestamp不需处理,image,text,ntext,sql_variant 暂时不处理 begin set @column=@column + case when len(@column)=0 then '' else ',' end + @name set @columndata = @columndata + case when len(@columndata)=0 then '' else ','','',' end + case when @xtype in(167,175) then '''''''''+'+@name+'+''''''''' --varchar,char when @xtype in(231,239) then '''N''''''+'+@name+'+''''''''' --nvarchar,nchar when @xtype=61 then '''''''''+convert(char(23),'+@name+',121)+''''''''' --datetime when @xtype=58 then '''''''''+convert(char(16),'+@name+',120)+''''''''' --smalldatetime when @xtype=36 then '''''''''+convert(char(36),'+@name+')+''''''''' --uniqueidentifier else @name end end end fetch next from syscolumns_cursor into @name,@xtype end close syscolumns_cursor deallocate syscolumns_cursor set @sql='set nocount on select ''insert '+@tablename+'('+@column+') values(''as ''--'','+@columndata+','')'' from '+@tablename print '--'+@sql exec(@sql) if @ident is not null print 'SET IDENTITY_INSERT '+@TableName+' OFF' 调用时 exec OutputData 'myuser' 其中myUser中当前数据库中存在的表。 另外方丈的: 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 =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 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 =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 --------------------------------------------------------------------------------------- 小干部的: create proc spgeninsertsql @tablename as varchar(100) as --declare @tablename varchar(100) --set @tablename = orders --set @tablename = eecation declare xcursor cursor for select name,xusertype from syscolumns where (id = object_id(@tablename)) declare @f1 varchar(100) declare @f2 integer declare @sql varchar(8000) set @sql =select insert into + @tablename + values( open xcursor fetch xcursor into @f1,@f2 while @@fetch_status = 0 begin set @sql =@sql + + case when @f2 in (35,58,99,167,175,231,239,61) then + case when + @f1 + is null then else end + else + end + replace(isnull(cast( + @f1 + as varchar),null),,) + case when @f2 in (35,58,99,167,175,231,239,61) then + case when + @f1 + is null then else end + else + end + char(13) + , fetch next from xcursor into @f1,@f2 end close xcursor deallocate xcursor set @sql = left(@sql,len(@sql) - 5) + + ) from + @tablename print @sql exec (@sql)

本文如未解决您的问题请添加抖音号:51dongshi(抖音搜索懂视),直接咨询即可。

网吧ip有问题导致steam无法登录怎么办4 用更加造句200 属羊男孩名字叫刘中泽怎样? 会计证分几种267 臣请辟于赵中的辟什么意思岂特百里哉中的特是什么1 石家庄市到石家庄正定国际机场怎么走1 无机酸的作用,使用无机酸的注意事项,无机酸有腐蚀性么3 摩尔庄园肥肥馆机械工坊里的贫铁矿铁。富铁矿。精铁矿。铁屑怎么... 摩尔庄园肥肥馆机械工坊猪放出不了17 摩尔庄园之肥肥馆机械工坊 铁栓如何快速得到? 想知道:石家庄市 从井陉客运站到石家庄正定国际机场怎么走?3 历史上寿命最长的皇帝是谁?265 风工注册过商标吗?还有哪些分类可以注册? 风海注册过商标吗?还有哪些分类可以注册? 微信怎么改?急!1261 我想修改,怎么修改?1243 浏阳市2022年高三毕业总人数 梦幻裁缝50到60总的需要多少帮派贡献? 英雄难度拉法姆怎么打 好久没登录,现在登录了,要好友辅助验证,不记得好友怎么办 怎样修改步骤1814 可以修改吗?如何修改?746 谁能告诉我这玻璃杯怎么拧开啊,拧了好久都拧不开,敲击底部也没...1 玻璃杯热时拧的太紧用什么方法打开6 开启玻璃杯盖有什么妙招 (特紧开不了)11 天津市盛世塑料制品有限公司怎么样? 中国最长寿的皇帝是谁638 古代打仗,行军宿营,士兵枕着牛皮箭筒睡在地上,能及早听到夜袭敌人的马蹄... 微信下载什么免费软件可以关联另一个 求一篇高一课文的翻译!我会加分的! 摩尔庄园机械工坊的猪怎么放回肥肥馆15 手伤残8级的赔偿标准是多少219 2010年虎宝宝,男方姓张,女方姓杨,取四个字的,张杨……6 买单出口的费用包括报关费用吗?13 属羊缺金的男孩名字叫铭泽怎么样1 不是自己工厂的产品,不能开票,报关能报吗?3 具有进出口权的公司,能买单报关吗?买单报关得量多是否会被查出... 文章中加图片会不会被搜索引擎加分?1 长沙请律师价格一般多少?1 Mi 谜语大全1
Top