你肯定有过这样的烦恼,同样的表,不同的数据库,加入你不能执行select insert
那么你肯定需要一条这样的存储过程,之需要传入表明,就会给你生成数据的插入语句。
当然数据表数量太大,你将最好用别的方式

复制代码 代码如下:
Create proc [dbo].[spGenInsertSQL] (@tablename varchar(256))
as
begin
declare @sql varchar(8000)
declare @sqlValues varchar(8000)
set @sql =' ('
set @sqlValues = 'values (''+'
select @sqlValues = @sqlValues + cols + ' + '','' + ' ,@sql = @sql + '[' + name + '],'
from
(select case
when xtype in (48,52,56,59,60,62,104,106,108,122,127)

then 'case when '+ name +' is null then ''NULL'' else ' + 'cast('+ name + ' as varchar)'+' end'

when xtype in (58,61)
--then '''''''''+convert(char(23),'+name+',121)+''''''''' --datetime
then 'case when '+ name +' is null then ''NULL'' else '+''''''''' + ' + 'cast('+ name +' as varchar)'+ '+'''''''''+' end'

when xtype in (167)

then 'case when '+ name +' is null then ''NULL'' else '+''''''''' + ' + 'replace('+ name+','''''''','''''''''''')' + '+'''''''''+' end'

when xtype in (231)

then 'case when '+ name +' is null then ''NULL'' else '+'''N'''''' + ' + 'replace('+ name+','''''''','''''''''''')' + '+'''''''''+' end'

when xtype in (175)

then 'case when '+ name +' is null then ''NULL'' else '+''''''''' + ' + 'cast(replace('+ name+','''''''','''''''''''') as Char(' + cast(length as varchar) + '))+'''''''''+' end'

when xtype in (239)

then 'case when '+ name +' is null then ''NULL'' else '+'''N'''''' + ' + 'cast(replace('+ name+','''''''','''''''''''') as Char(' + cast(length as varchar) + '))+'''''''''+' end'

else '''NULL'''

end as Cols,name

from syscolumns

where id = object_id(@tablename)

) T
set @sql ='select ''INSERT INTO ['+ @tablename + ']' + left(@sql,len(@sql)-1)+') ' + left(@sqlValues,len(@sqlValues)-4) + ')'' from '+@tablename
print @sql
exec (@sql)
end

SQL语句

最后的结果:
INSERT INTO [SysSample] ([Id],[Name],[Age],[Bir],[Photo],[Note],[CreateTime]) values ('0002CA83-AF2F-4D8F-A345-33CA1CC7CF3C','任务调度系统',18,'2013-01-02 21:42:30.013','',NULL,'2013-01-02 21:42:30.013')
INSERT INTO [SysSample] ([Id],[Name],[Age],[Bir],[Photo],[Note],[CreateTime]) values ('0004A6F3-EC28-4D1F-BA40-0FC4B2218C92','任务调度系统',18,'2013-07-09 19:36:00.060','',NULL,'2013-07-09 19:36:00.060')
INSERT INTO [SysSample] ([Id],[Name],[Age],[Bir],[Photo],[Note],[CreateTime]) values ('00094D35-7B51-4EA3-871E-CE17E293B157','任务调度系统',18,'2013-05-16 15:21:20.070','',NULL,'2013-05-16 15:21:20.070')
INSERT INTO [SysSample] ([Id],[Name],[Age],[Bir],[Photo],[Note],[CreateTime]) values ('000BFBB0-B37D-4D6E-9FA2-3069D4F18F84','任务调度系统',18,'2013-04-11 11:41:50.030','',NULL,'2013-04-11 11:41:50.030')
INSERT INTO [SysSample] ([Id],[Name],[Age],[Bir],[Photo],[Note],[CreateTime]) values ('000C2CBC-E358-4469-BC2C-04F4DDCD72CD','任务调度系统',18,'2013-05-06 16:07:00.037','',NULL,'2013-05-06 16:07:00.037')
INSERT INTO [SysSample] ([Id],[Name],[Age],[Bir],[Photo],[Note],[CreateTime]) values ('000CB795-40EC-4783-B7A4-8D298DF63B70','任务调度系统',18,'2013-01-23 20:52:30.030','',NULL,'2013-01-23 20:52:30.030')

更多相关文章

  1. Android(安卓)-- Android(安卓)JUint 与 Sqlite
  2. android 当系统存在多个Launcher时,如何设置开机自动进入默认的La
  3. Android(安卓)SQLiteDatabase的使用
  4. android 通话记录次数
  5. Android(安卓)SQLiteDatabase的使用
  6. android实现关键字搜索功能
  7. 我的android 第14天 - 使用SQLiteDatabase操作SQLite数据库
  8. Android(安卓)编码规范
  9. android contentProvider例子

随机推荐

  1. C#实现操作字符串的方法总结
  2. ASP.NET Core应用程序运行Vue并且部署在I
  3. C#中关于foreach实现的原理详解
  4. C#中pdf生成图片文字水印类的实现实例
  5. IIS如何实现部署asp.net mvc网站的方法
  6. C#编写Windows服务程序的图文详解
  7. C#中值类型与引用类型的详细介绍
  8. ASP.NET Core类库项目中如何实现读取配置
  9. C#实现杨辉三角的示例
  10. C#使用Free Spire.Presentation实现对PPT