一般用BCP在处理这个事情,但有时也需要一些特殊的处理,以下是生成表中的一些数据,带有where条件的选择生成数据,是我一个同事修改的,直接拿过来用了: SET QUOTED_IDENTIFIER ON GO SET ANSI_NULLS ON GO Create Proc proc_insert_where (@tablename varchar(256),@where varchar(256)) as -- 表名称 begin Declare @sqlstr varchar(8000), @sqlstr1 varchar(8000), @sqlstr2 varchar(8000),@col varchar(1000),@colid int,@name varchar(20) Select @sqlstr='select ''Insert '+@tablename Select @sqlstr1= ' Values ( ''+', @sqlstr2=' (' --print '5' select @where = isnull(@where,'') Select case -- when a.xtype =173 then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar('+convert(varchar(4),a.length*2+2)+'),'+a.name +')'+' end' when a.xtype =104 then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar(1),'+a.name +')'+' end' when a.xtype =175 then 'case when '+a.name+' is null then ''NULL'' else '+'''''''''+'+'replace('+a.name+','''''''','''''''''''')' + '+'''''''''+' end' when a.xtype =61 then 'case when '+a.name+' is null then ''NULL'' else '+'''''''''+'+'convert(varchar(23),'+a.name +',121)'+ '+'''''''''+' end' when a.xtype =106 then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar('+convert(varchar(4),a.xprec+2)+'),'+a.name +')'+' end' when a.xtype =62 then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar(23),'+a.name +',2)'+' end' when a.xtype =56 then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar(11),'+a.name +')'+' end' when a.xtype =60 then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar(22),'+a.name +')'+' end' when a.xtype =239 then 'case when '+a.name+' is null then ''NULL'' else '+'''''''''+'+'replace('+a.name+','''''''','''''''''''')' + '+'''''''''+' end' when a.xtype =108 then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar('+convert(varchar(4),a.xprec+2)+'),'+a.name +')'+' end' when a.xtype =231 then 'case when '+a.name+' is null then ''NULL'' else '+'''''''''+'+'replace('+a.name+','''''''','''''''''''')' + '+'''''''''+' end' when a.xtype =59 then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar(23),'+a.name +',2)'+' end' when a.xtype =58 then 'case when '+a.name+' is null then ''NULL'' else '+'''''''''+'+'convert(varchar(23),'+a.name +',121)'+ '+'''''''''+' end' when a.xtype =52 then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar(12),'+a.name +')'+' end' when a.xtype =122 then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar(22),'+a.name +')'+' end' when a.xtype =48 then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar(6),'+a.name +')'+' end' -- when a.xtype =165 then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar('+convert(varchar(4),a.length*2+2)+'),'+a.name +')'+' end' when a.xtype =167 then 'case when '+a.name+' is null then ''NULL'' else '+'''''''''+'+'replace('+a.name+','''''''','''''''''''')' + '+'''''''''+' end' else '''NULL''' end as col,a.colid,a.name into #hlq from syscolumns a where a.id = object_id(@tablename) and a.xtype <>189 and a.xtype <>34 and a.xtype <>35 and a.xtype <>36 and a.colstat <> 1 --select * from #hlq declare cur_1 cursor for select * from #hlq open cur_1 fetch next from cur_1 into @col,@colid,@name while @@fetch_status = 0 begin Select @sqlstr1=@sqlstr1+@col+'+'',''+' select @sqlstr2=@sqlstr2+@name +',' --print '1 = '+@sqlstr1 -- print @col fetch next from cur_1 into @col,@colid,@name end close cur_1 deallocate cur_1 --print '@sqlstr'+@sqlstr --print '@sqlstr2'+@sqlstr2 --print '@sqlstr1'+@sqlstr1 --print len(@sqlstr1) if @where = '' or @where = '%' select @sqlstr=@sqlstr+left(@sqlstr2,len(@sqlstr2)-1)+') '+left(@sqlstr1,len(@sqlstr1)-3)+')'' from '+@tablename else select @sqlstr=@sqlstr+left(@sqlstr2,len(@sqlstr2)-1)+') '+left(@sqlstr1,len(@sqlstr1)-3)+')'' from '+@tablename+' ' +@where --print '7' --print @sqlstr exec( @sqlstr) drop table #hlq end GO SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS ON GO