您的位置:首页 > 博客中心 > 数据库 >

将表中数据生成SQL语句

时间:2022-03-09 16:46

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

gxlsystem.com,布布扣
gxlsystem.com,布布扣
gxlsystem.com,布布扣CREATE PROCEDURE dbo.UspOutputData 
gxlsystem.com,布布扣@tablename sysname 
gxlsystem.com,布布扣AS 
gxlsystem.com,布布扣declare @column varchar(1000) 
gxlsystem.com,布布扣declare @columndata varchar(1000) 
gxlsystem.com,布布扣declare @sql varchar(4000) 
gxlsystem.com,布布扣declare @xtype tinyint 
gxlsystem.com,布布扣declare @name sysname 
gxlsystem.com,布布扣declare @objectId int 
gxlsystem.com,布布扣declare @objectname sysname 
gxlsystem.com,布布扣declare @ident int 
gxlsystem.com,布布扣
gxlsystem.com,布布扣set nocount on 
gxlsystem.com,布布扣set @objectId=object_id(@tablename) 
gxlsystem.com,布布扣
gxlsystem.com,布布扣if @objectId is null -- 判斷對象是否存在 
gxlsystem.com,布布扣begin 
gxlsystem.com,布布扣print ‘The object not exists‘ 
gxlsystem.com,布布扣return 
gxlsystem.com,布布扣end 
gxlsystem.com,布布扣set @objectname=rtrim(object_name(@objectId)) 
gxlsystem.com,布布扣
gxlsystem.com,布布扣if @objectname is null or charindex(@objectname,@tablename)=0 --此判断不严密 
gxlsystem.com,布布扣begin 
gxlsystem.com,布布扣print ‘object not in current database‘ 
gxlsystem.com,布布扣return 
gxlsystem.com,布布扣end 
gxlsystem.com,布布扣
gxlsystem.com,布布扣if OBJECTPROPERTY(@objectId,‘IsTable‘) < > 1 -- 判斷對象是否是table 
gxlsystem.com,布布扣begin 
gxlsystem.com,布布扣print ‘The object is not table‘ 
gxlsystem.com,布布扣return 
gxlsystem.com,布布扣end 
gxlsystem.com,布布扣
gxlsystem.com,布布扣select @ident=status&0x80 from syscolumns where id=@objectid and status&0x80=0x80 
gxlsystem.com,布布扣
gxlsystem.com,布布扣if @ident is not null 
gxlsystem.com,布布扣print ‘SET IDENTITY_INSERT ‘+@TableName+‘ ON‘ 
gxlsystem.com,布布扣
gxlsystem.com,布布扣declare syscolumns_cursor cursor
gxlsystem.com,布布扣
gxlsystem.com,布布扣for select c.name,c.xtype from syscolumns c where c.id=@objectid order by c.colid 
gxlsystem.com,布布扣
gxlsystem.com,布布扣open syscolumns_cursor 
gxlsystem.com,布布扣set @column=‘‘ 
gxlsystem.com,布布扣set @columndata=‘‘ 
gxlsystem.com,布布扣fetch next from syscolumns_cursor into @name,@xtype 
gxlsystem.com,布布扣
gxlsystem.com,布布扣while @@fetch_status < >-1 
gxlsystem.com,布布扣begin 
gxlsystem.com,布布扣if @@fetch_status < >-2 
gxlsystem.com,布布扣begin 
gxlsystem.com,布布扣if @xtype not in(189,34,35,99,98) --timestamp不需处理,image,text,ntext,sql_variant 暂时不处理 
gxlsystem.com,布布扣
gxlsystem.com,布布扣begin 
gxlsystem.com,布布扣set @column=@column+case when len(@column)=0 then‘‘ else ‘,‘end+@name 
gxlsystem.com,布布扣
gxlsystem.com,布布扣set @columndata=@columndata+case when len(@columndata)=0 then ‘‘ else ‘,‘‘,‘‘,‘
gxlsystem.com,布布扣end 
gxlsystem.com,布布扣
gxlsystem.com,布布扣+case when @xtype in(167,175) then ‘‘‘‘‘‘‘‘‘+‘+@name+‘+‘‘‘‘‘‘‘‘‘ --varchar,char 
gxlsystem.com,布布扣when @xtype in(231,239) then ‘‘‘N‘‘‘‘‘‘+‘+@name+‘+‘‘‘‘‘‘‘‘‘ --nvarchar,nchar 
gxlsystem.com,布布扣when @xtype=61 then ‘‘‘‘‘‘‘‘‘+convert(char(23),‘+@name+‘,121)+‘‘‘‘‘‘‘‘‘ --datetime 
gxlsystem.com,布布扣when @xtype=58 then ‘‘‘‘‘‘‘‘‘+convert(char(16),‘+@name+‘,120)+‘‘‘‘‘‘‘‘‘ --smalldatetime 
gxlsystem.com,布布扣when @xtype=36 then ‘‘‘‘‘‘‘‘‘+convert(char(36),‘+@name+‘)+‘‘‘‘‘‘‘‘‘ --uniqueidentifier 
gxlsystem.com,布布扣else @name end 
gxlsystem.com,布布扣
gxlsystem.com,布布扣end 
gxlsystem.com,布布扣
gxlsystem.com,布布扣end 
gxlsystem.com,布布扣
gxlsystem.com,布布扣fetch next from syscolumns_cursor into @name,@xtype 
gxlsystem.com,布布扣
gxlsystem.com,布布扣end 
gxlsystem.com,布布扣
gxlsystem.com,布布扣close syscolumns_cursor 
gxlsystem.com,布布扣deallocate syscolumns_cursor 
gxlsystem.com,布布扣
gxlsystem.com,布布扣set @sql=‘set nocount on select ‘‘insert ‘+@tablename+‘(‘+@column+‘) values(‘‘as ‘‘--‘‘,‘+@columndata+‘,‘‘)‘‘ from ‘+@tablename 
gxlsystem.com,布布扣
gxlsystem.com,布布扣print ‘--‘+@sql 
gxlsystem.com,布布扣exec(@sql) 
gxlsystem.com,布布扣
gxlsystem.com,布布扣if @ident is not null 
gxlsystem.com,布布扣print ‘SET IDENTITY_INSERT ‘+@TableName+‘ OFF‘ 
gxlsystem.com,布布扣
gxlsystem.com,布布扣GO
gxlsystem.com,布布扣
gxlsystem.com,布布扣exec UspOutputData  tableName[表名]
gxlsystem.com,布布扣
gxlsystem.com,布布扣
gxlsystem.com,布布扣

查询结果如下:

gxlsystem.com,布布扣insert T_user_title(F_ID,F_TitleName,F_Remark,F_Status,F_EditTime,F_InstitutionId) values( 101 ,    ‘软件工程师‘ ,    ‘从事ASP.NET软件研发‘,1, ‘2007-12-26 10:26:43.000‘,101)
gxlsystem.com,布布扣insert T_user_title(F_ID,F_TitleName,F_Remark,F_Status,F_EditTime,F_InstitutionId) values( 201 ,    ‘销售人员‘ ,    ‘从事软件销售‘ ,1 , ‘2007-12-26 10:26:29.000‘,101 )
gxlsystem.com,布布扣insert T_user_title(F_ID,F_TitleName,F_Remark,F_Status,F_EditTime,F_InstitutionId) values( 301 ,    ‘sfgsdfg‘ ,    ‘asdfasdf‘, 3 , ‘2007-12-25 18:21:48.000‘,101 )
gxlsystem.com,布布扣


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

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

本类排行

今日推荐

热门手游