码迷,mamicode.com
首页 > 数据库 > 详细

将表中数据生成SQL语句

时间:2014-06-10 12:50:35      阅读:291      评论:0      收藏:0      [点我收藏+]

标签:c   blog   a   http   ext   int   

  在开发过程中,经常需要我们对表中的数据进行转移,如果在同台机器,可以使用SQL自带的导入数据,但是如果想让所有的数据生成可执行的SQL语句,它的移植性最强了。
首先要设计一个存储过程。具体如下:

bubuko.com,布布扣
bubuko.com,布布扣
bubuko.com,布布扣CREATE PROCEDURE dbo.UspOutputData 
bubuko.com,布布扣@tablename sysname 
bubuko.com,布布扣AS 
bubuko.com,布布扣declare @column varchar(1000
bubuko.com,布布扣declare @columndata varchar(1000
bubuko.com,布布扣declare @sql varchar(4000
bubuko.com,布布扣declare @xtype tinyint 
bubuko.com,布布扣declare @name sysname 
bubuko.com,布布扣declare @objectId int 
bubuko.com,布布扣declare @objectname sysname 
bubuko.com,布布扣declare @ident int 
bubuko.com,布布扣
bubuko.com,布布扣set nocount on 
bubuko.com,布布扣set @objectId=object_id(@tablename
bubuko.com,布布扣
bubuko.com,布布扣if @objectId is null -- 判斷對象是否存在 
bubuko.com,布布扣
begin 
bubuko.com,布布扣print The object not exists 
bubuko.com,布布扣return 
bubuko.com,布布扣end 
bubuko.com,布布扣set @objectname=rtrim(object_name(@objectId)) 
bubuko.com,布布扣
bubuko.com,布布扣if @objectname is null or charindex(@objectname,@tablename)=0 --此判断不严密 
bubuko.com,布布扣
begin 
bubuko.com,布布扣print object not in current database 
bubuko.com,布布扣return 
bubuko.com,布布扣end 
bubuko.com,布布扣
bubuko.com,布布扣if OBJECTPROPERTY(@objectId,IsTable< > 1 -- 判斷對象是否是table 
bubuko.com,布布扣
begin 
bubuko.com,布布扣print The object is not table 
bubuko.com,布布扣return 
bubuko.com,布布扣end 
bubuko.com,布布扣
bubuko.com,布布扣select @ident=status&0x80 from syscolumns where id=@objectid and status&0x80=0x80 
bubuko.com,布布扣
bubuko.com,布布扣if @ident is not null 
bubuko.com,布布扣print SET IDENTITY_INSERT +@TableName+ ON 
bubuko.com,布布扣
bubuko.com,布布扣declare syscolumns_cursor cursor
bubuko.com,布布扣
bubuko.com,布布扣for select c.name,c.xtype from syscolumns c where c.id=@objectid order by c.colid 
bubuko.com,布布扣
bubuko.com,布布扣open syscolumns_cursor 
bubuko.com,布布扣set @column=‘‘ 
bubuko.com,布布扣set @columndata=‘‘ 
bubuko.com,布布扣fetch next from syscolumns_cursor into @name,@xtype 
bubuko.com,布布扣
bubuko.com,布布扣while @@fetch_status < >-1 
bubuko.com,布布扣begin 
bubuko.com,布布扣if @@fetch_status < >-2 
bubuko.com,布布扣begin 
bubuko.com,布布扣if @xtype not in(189,34,35,99,98--timestamp不需处理,image,text,ntext,sql_variant 暂时不处理 
bubuko.com,布布扣

bubuko.com,布布扣begin 
bubuko.com,布布扣set @column=@column+case when len(@column)=0 then‘‘ else ,end+@name 
bubuko.com,布布扣
bubuko.com,布布扣set @columndata=@columndata+case when len(@columndata)=0 then ‘‘ else ,‘‘,‘‘,
bubuko.com,布布扣end 
bubuko.com,布布扣
bubuko.com,布布扣+case when @xtype in(167,175then ‘‘‘‘‘‘‘‘‘++@name++‘‘‘‘‘‘‘‘‘ --varchar,char 
bubuko.com,布布扣
when @xtype in(231,239then ‘‘‘N‘‘‘‘‘‘++@name++‘‘‘‘‘‘‘‘‘ --nvarchar,nchar 
bubuko.com,布布扣
when @xtype=61 then ‘‘‘‘‘‘‘‘‘+convert(char(23),+@name+,121)+‘‘‘‘‘‘‘‘‘ --datetime 
bubuko.com,布布扣
when @xtype=58 then ‘‘‘‘‘‘‘‘‘+convert(char(16),+@name+,120)+‘‘‘‘‘‘‘‘‘ --smalldatetime 
bubuko.com,布布扣
when @xtype=36 then ‘‘‘‘‘‘‘‘‘+convert(char(36),+@name+)+‘‘‘‘‘‘‘‘‘ --uniqueidentifier 
bubuko.com,布布扣
else @name end 
bubuko.com,布布扣
bubuko.com,布布扣end 
bubuko.com,布布扣
bubuko.com,布布扣end 
bubuko.com,布布扣
bubuko.com,布布扣fetch next from syscolumns_cursor into @name,@xtype 
bubuko.com,布布扣
bubuko.com,布布扣end 
bubuko.com,布布扣
bubuko.com,布布扣close syscolumns_cursor 
bubuko.com,布布扣deallocate syscolumns_cursor 
bubuko.com,布布扣
bubuko.com,布布扣set @sql=set nocount on select ‘‘insert +@tablename+(+@column+) values(‘‘as ‘‘--‘‘,+@columndata+,‘‘)‘‘ from +@tablename 
bubuko.com,布布扣
bubuko.com,布布扣print --+@sql 
bubuko.com,布布扣exec(@sql
bubuko.com,布布扣
bubuko.com,布布扣if @ident is not null 
bubuko.com,布布扣print SET IDENTITY_INSERT +@TableName+ OFF 
bubuko.com,布布扣
bubuko.com,布布扣GO
bubuko.com,布布扣
bubuko.com,布布扣exec UspOutputData  tableName[表名]
bubuko.com,布布扣
bubuko.com,布布扣
bubuko.com,布布扣

查询结果如下:

bubuko.com,布布扣insert T_user_title(F_ID,F_TitleName,F_Remark,F_Status,F_EditTime,F_InstitutionId) values101 ,    软件工程师 ,    从事ASP.NET软件研发,12007-12-26 10:26:43.000,101)
bubuko.com,布布扣insert T_user_title(F_ID,F_TitleName,F_Remark,F_Status,F_EditTime,F_InstitutionId) values201 ,    销售人员 ,    从事软件销售 ,1 , 2007-12-26 10:26:29.000,101 )
bubuko.com,布布扣insert T_user_title(F_ID,F_TitleName,F_Remark,F_Status,F_EditTime,F_InstitutionId) values301 ,    sfgsdfg ,    asdfasdf3 , 2007-12-25 18:21:48.000,101 )
bubuko.com,布布扣


提示:
这样执行之后,可能你得到的是基于表格内的数据。为了进一步生成可用的SQL语句,只要对SQL简单的进行设置就可以了。
打开查询窗口,右击页面-----》有一选项【将结果保存到】-----》选择【以文本格式显示结果】
得到的结果就如下:

将表中数据生成SQL语句,布布扣,bubuko.com

将表中数据生成SQL语句

标签:c   blog   a   http   ext   int   

原文地址:http://www.cnblogs.com/lyl6796910/p/3778891.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!