【SQL Sever】将SQL Sever中的一个数据表的数据导出为insert语句
生活随笔
收集整理的這篇文章主要介紹了
【SQL Sever】将SQL Sever中的一个数据表的数据导出为insert语句
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
例如:這SQL?? Sever中的一張數據表,想要將這張數據表中的數據? 轉化成一個一個的insert語句存儲在txt的文檔中,那么不論走到那里這個insert語句一執行,我們就能將這個數據表中的數據插入到另一個地方了。
1》在新建查詢中,創建一個對象,這個對象就是用來產生這個對象的,名字叫proc_insert,我們可以創建多個不重名的對象,當然也可以刪除這個對象。
1 create proc proc_insert (@tablename varchar(256)) 2 as 3 begin 4 set nocount on 5 declare @sqlstr varchar(4000) 6 declare @sqlstr1 varchar(4000) 7 declare @sqlstr2 varchar(4000) 8 select @sqlstr='select ''insert '+@tablename 9 select @sqlstr1='' 10 select @sqlstr2=' (' 11 select @sqlstr1= ' values ( ''+' 12 select @sqlstr1=@sqlstr1+col+'+'',''+' ,@sqlstr2=@sqlstr2+name +',' from (select case 13 -- 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' 14 when a.xtype =127 then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar(20),'+a.name +')'+' end' 15 when a.xtype =104 then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar(1),'+a.name +')'+' end' 16 when a.xtype =175 then 'case when '+a.name+' is null then ''NULL'' else '+'''''''''+'+'replace('+a.name+','''''''','''''''''''')' + '+'''''''''+' end' 17 when a.xtype =61 then 'case when '+a.name+' is null then ''NULL'' else '+'''''''''+'+'convert(varchar(23),'+a.name +',121)'+ '+'''''''''+' end' 18 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' 19 when a.xtype =62 then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar(23),'+a.name +',2)'+' end' 20 when a.xtype =56 then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar(11),'+a.name +')'+' end' 21 when a.xtype =60 then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar(22),'+a.name +')'+' end' 22 when a.xtype =239 then 'case when '+a.name+' is null then ''NULL'' else '+'''''''''+'+'replace('+a.name+','''''''','''''''''''')' + '+'''''''''+' end' 23 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' 24 when a.xtype =231 then 'case when '+a.name+' is null then ''NULL'' else '+'''''''''+'+'replace('+a.name+','''''''','''''''''''')' + '+'''''''''+' end' 25 when a.xtype =59 then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar(23),'+a.name +',2)'+' end' 26 when a.xtype =58 then 'case when '+a.name+' is null then ''NULL'' else '+'''''''''+'+'convert(varchar(23),'+a.name +',121)'+ '+'''''''''+' end' 27 when a.xtype =52 then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar(12),'+a.name +')'+' end' 28 when a.xtype =122 then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar(22),'+a.name +')'+' end' 29 when a.xtype =48 then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar(6),'+a.name +')'+' end' 30 -- 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' 31 when a.xtype =167 then 'case when '+a.name+' is null then ''NULL'' else '+'''''''''+'+'replace('+a.name+','''''''','''''''''''')' + '+'''''''''+' end' 32 else '''NULL''' 33 end as col,a.colid,a.name 34 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 35 )t order by colid 36 37 select @sqlstr=@sqlstr+left(@sqlstr2,len(@sqlstr2)-1)+') '+left(@sqlstr1,len(@sqlstr1)-3)+')'' from '+@tablename 38 -- print @sqlstr 39 exec( @sqlstr) 40 set nocount off 41 end 42 go View Code2》執行這個對象,讓他產生insert語句
1 exec proc_insert p_phone; View Code效果如下:
3》第一步全選,第二步將結果另存為
4》這樣就生成了一個文本文件了
5》如果這里面的id是自增的,或者不想讓某一列插入,那就將這些代碼放在word中進行替換。
?
END----
轉載于:https://www.cnblogs.com/sxdcgaq8080/p/5909300.html
總結
以上是生活随笔為你收集整理的【SQL Sever】将SQL Sever中的一个数据表的数据导出为insert语句的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Thinking In Java 读书笔
- 下一篇: 给 axios 和 redux-axio