将表中数据生成SQL语句

时间:2024-04-16 10:49:00

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

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

查询结果如下:

将表中数据生成SQL语句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)
将表中数据生成SQL语句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 )
将表中数据生成SQL语句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 )
将表中数据生成SQL语句

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