获取某个表的数据,并产生插入这些数据的SQL(主要用于不同服务器间相同结构表的数据复制)。

获取某个表的数据,并产生插入这些数据的SQL(主要用于不同服务器间相同结构表的数据复制)。

IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[usp_Tool_GetSQL]') AND type in (N'P', N'PC')) DROP PROCEDURE [dbo].[usp_Tool_GetSQL] GO create PROCEDURE [dbo].[usp_Tool_GetSQL] @tablename sysname, @aimtablename sysname = '', @condition nvarchar(500) = '', @orderby nvarchar(500) = '', @showSql char(1) = 'N' AS begin declare @sqlName varchar(8000) declare @sqlValues varchar(8000) declare @strCondition varchar(1000) SET NOCOUNT ON select @sqlName =' (' select @sqlValues = 'VALUES (''+' if(@aimtablename = '') select @aimtablename = @tablename select @sqlValues = @sqlValues + ColValue + ' + '','' + ' ,@sqlName = @sqlName + '[' + ColName + '],' 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) --smalldatetime datetime --then 'case when ['+ name +'] is null then ''NULL'' else '+''''''''' + ' + 'cast(['+ name +'] as varchar)'+ '+'''''''''+' end' then 'case when ['+ name +'] is null then ''NULL'' else '+''''''''' + ' + 'convert(nvarchar(24),['+ name +'],121)'+ '+'''''''''+' end' -- when xtype in (167,175)--(var)char -- then 'case when '+ name +' is null then ''NULL'' else '+''''''''' + ' + 'replace('+ name+','''''''','''''''''''')' + '+''''''''' + ' end' when xtype in (167,175)--(var)char then 'case when ['+ name +'] is null then ''NULL'' else '+'''N'''''' + ' + 'replace(['+ name+'],'''''''','''''''''''')' + '+''''''''' + ' end' when xtype in (231,239)--(nvar)char then 'case when ['+ name +'] is null then ''NULL'' else '+'''N'''''' + ' + 'replace(['+ name+'],'''''''','''''''''''')' + '+''''''''' + ' end' else '''NULL''' end as ColValue,name as ColName from syscolumns where id = object_id(@tablename) --and autoval is null --当该栏位为自增型int时,会出现autoval不为null的情况。 ) T select @sqlValues = left(@sqlValues,len(@sqlValues)-4) if(@showSql='Y') BEGIN print '--SQL1 - GenColoums:' print '--select ''INSERT INTO ['+ @aimtablename + ']' + left(@sqlName,len(@sqlName)-1)+') ' + @sqlValues + ')'' AS INSERTSQL from '+@tablename + space(1) + @condition print '--SQL2 - Not GenColoums:' print '--select ''INSERT INTO ['+ @aimtablename + '] ' + @sqlValues + ')'' AS INSERTSQL from '+@tablename + space(1) + @condition END select @sqlName = left(@sqlName,len(@sqlName)-1) select @strCondition=replace(@condition,'''','''''') /* --from table to table exec ('SELECT ''--['+@tablename+']-->['+@aimtablename+']'' as [ ]') --delete existed records exec('select ''DELETE FROM'+ @tablename + ' ' + @strCondition+''' as [--Delete SQL]') */ --get insert sql create table #tmp(SQL varchar(8000)) insert into #tmp exec('SELECT ''--['+@tablename+']-->['+@aimtablename+']'' as [ ] UNION ALL ' + 'SELECT ''DELETE FROM ['+ @aimtablename + '] ' + @strCondition+''' as [ ]') insert into #tmp exec('SELECT ''INSERT INTO ['+ @aimtablename + ']' + @sqlName +') '+ @sqlValues + ')'' as [ ] from '+ @tablename + ' ' + @condition + ' ' + @orderby) select SQL as [ ] from #tmp drop table #tmp SET NOCOUNT OFF end GO

你可能感兴趣的:(sql,object,服务器,table,null,insert)