利用SQL存储过程创建表的插入数据语句(用于到别处还原整张表)
2014-11-28 16:09
197 查看
先用SQL Server 自定的创建脚本功能,建立建表语句,
然后运行一下存储过程得到插入数据语句
然后运行一下存储过程得到插入数据语句
<pre name="code" class="sql">GO /****** 对象: StoredProcedure [dbo].[Proc_insert] 脚本日期: 04/15/2015 12:54:19 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO ALTER PROCEDURE [dbo].[Proc_insert] @tablename varchar(256),--表名 @IsPrimaryKey int=0 --字段名 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 IF @IsPrimaryKey=1 Begin set @sqlstr= 'select ''set IDENTITY_INSERT '+ @tablename +' ON '' Union All ' +@sqlstr+ ' Union all select '' set IDENTITY_INSERT '+ @tablename +' OFF''' End exec( @sqlstr) print @sqlstr set nocount off end --运用实例 --exec proc_insert @tablename='CheckNames',@IsPrimaryKey=0
相关文章推荐
- SQL语句创建表并插入指定N条数据(用于测试)
- SQL语句创建表并插入指定N条数据(用于测试)
- SQL语句创建表并插入指定N条数据(用于测试)
- SQL语句创建表并插入指定N条数据(用于测试)
- SQL语句创建表并插入指定N条数据(用于测试)
- 将数据库中的数据生成插入语句(用于数据的导出与导入)
- 在VB.NET中 用代码 利用SQL语句创建数据库、表、存储过程、视图、索引、规则、修改表、查看数据等的方法
- mysql创建数据库,创建数据库表导入xlsx、txt文本,查询、删除、插入数据语句的使用
- 利用写SQL语句创建数据表
- 一个利用创建临时表后插入数据的查询
- 格式化测试数据,组装用于插入表中的sql语句
- mysql数据库中创建、删除、更新、插入数据、等语句
- hql语句不能用于插入数据
- 利用 HTable创建客户端,并向HBase插入数据
- 创建表的语句,以及插入数据(存储过程)
- 数据库生成脚本-------创建语句的同时,把表中的数据也生成插入语句
- SQL学习笔记二 创建表、插入数据的语句
- SQL学习笔记二 创建表、插入数据的语句
- 写了一条Sybase SQL语句,用于生成一个表中所有数据的插入记录的生成
- 创建shell脚本,利用for循环箱向日志文件中插入n条数据