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

解决方案 »

  1.   

    小干部的:
    create  proc spGenInsertSQL
    @TableName as varchar(100)
    as
    --declare @TableName varchar(100)
    --set @TableName = 'orders'
    --set @TableName = 'eeducation'
    DECLARE xCursor CURSOR FOR
    SELECT name,xusertype
    FROM syscolumns
    WHERE (id = OBJECT_ID(@TableName))
    declare @F1 varchar(100)
    declare @F2 integer
    declare @SQL varchar(8000)
    set @sql ='SELECT ''INSERT INTO ' + @TableName + ' VALUES('''
    OPEN xCursor
    FETCH xCursor into @F1,@F2
    WHILE @@FETCH_STATUS = 0
    BEGIN
        set @sql =@sql +
                  + case when @F2 IN (35,58,99,167,175,231,239,61) then ' + case when ' + @F1 + ' IS NULL then '''' else '''''''' end + '  else '+' end
                  + 'replace(ISNULL(cast(' + @F1 + ' as varchar),''NULL''),'''''''','''''''''''')' 
                  + case when @F2 IN (35,58,99,167,175,231,239,61) then ' + case when ' + @F1 + ' IS NULL then '''' else '''''''' end + '  else '+' end
                  + char(13) + ''',''' 
        FETCH NEXT FROM xCursor into @F1,@F2
    END
    CLOSE xCursor
    DEALLOCATE xCursor
    set @sql = left(@sql,len(@sql) - 5) + ' + '')'' FROM ' + @TableName
    print @sql
    exec (@sql)
      

  2.   

    太多了!都是转贴:
    create proc proc_insert (@tablename varchar(256))
    as
    begin
    set nocount on
    declare @sqlstr varchar(4000)
    declare @sqlstr1 varchar(4000)
    declare @sqlstr2 varchar(4000)
    select @sqlstr='select ''insert '+@tablename
    select @sqlstr1=''
    select @sqlstr2=' ('
    select @sqlstr1= ' values ( ''+'
    select @sqlstr1=@sqlstr1+col+'+'',''+' ,@sqlstr2=@sqlstr2+name +',' from (select case 
    when a.xtype =173 then 'convert(varchar('+convert(varchar(4),a.length*2+2)+'),'+a.name +')'
    when a.xtype =104 then 'convert(varchar(1),'+a.name +')'
    when a.xtype =175 then '''''''''+'+a.name + '+'''''''''
    when a.xtype =61 then '''''''''+'+'convert(varchar(23),'+a.name +',120)'+ '+'''''''''
    when a.xtype =106 then 'convert(varchar('+convert(varchar(4),a.xprec+2)+'),'+a.name +')'
    when a.xtype =62 then 'convert(varchar(23),'+a.name +',2)'
    when a.xtype =56 then 'convert(varchar(11),'+a.name +')'
    when a.xtype =60 then 'convert(varchar(22),'+a.name +')'
    when a.xtype =239 then '''''''''+'+a.name + '+'''''''''
    when a.xtype =108 then 'convert(varchar('+convert(varchar(4),a.xprec+2)+'),'+a.name +')'
    when a.xtype =231 then '''''''''+'+a.name + '+'''''''''
    when a.xtype =59 then 'convert(varchar(23),'+a.name +',2)'
    when a.xtype =58 then '''''''''+'+'convert(varchar(23),'+a.name +',120)'+ '+'''''''''
    when a.xtype =52 then 'convert(varchar(12),'+a.name +')'
    when a.xtype =122 then 'convert(varchar(22),'+a.name +')'
    when a.xtype =48 then 'convert(varchar(6),'+a.name +')'
    when a.xtype =165 then 'convert(varchar('+convert(varchar(4),a.length*2+2)+'),'+a.name +')'
    when a.xtype =167 then '''''''''+'+a.name + '+'''''''''
    else a.name
    end as col,a.colid,a.name
    from syscolumns a where a.id = object_id(@tablename)  
    )t order by colid

    select @sqlstr=@sqlstr+left(@sqlstr2,len(@sqlstr2)-1)+') '+left(@sqlstr1,len(@sqlstr1)-3)+')'' from '+@tablename
    exec( @sqlstr)
    set nocount off
    end
    go
      

  3.   

    老和尚提供的:create proc proc_insert (@tablename varchar(256))
    as
    begin
    set nocount on
    declare @sqlstr varchar(4000)
    declare @sqlstr1 varchar(4000)
    declare @sqlstr2 varchar(4000)
    select @sqlstr='select ''insert '+@tablename
    select @sqlstr1=''
    select @sqlstr2=' ('
    select @sqlstr1= ' values ( ''+'
    select @sqlstr1=@sqlstr1+col+'+'',''+' ,@sqlstr2=@sqlstr2+name +',' from (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
    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
    )t order by colid

    select @sqlstr=@sqlstr+left(@sqlstr2,len(@sqlstr2)-1)+') '+left(@sqlstr1,len(@sqlstr1)-3)+')'' from '+@tablename
    --  print @sqlstr
    exec( @sqlstr)
    set nocount off
    endGO
      

  4.   

    对 是有这几个帖子的
    http://expert.csdn.net/Expert/topic/1374/1374946.xml?temp=.105343http://expert.csdn.net/Expert/topic/1397/1397315.xml?temp=1.774234E-02
      

  5.   

    turbochen(程序员) ,
         呵呵~~~你都试试,再把哪个好用告诉大家,好吗?
      

  6.   

    我运行下面的脚本:
    SET IDENTITY_INSERT table1 ON
    GO
    INSERT INTO table1 VALUES(1,'xxx公司','xxx公司',NULL)
    INSERT INTO table1 VALUES(46,'xx部','xx部',1)
    GO
    SET IDENTITY_INSERT table1 OFF
    GO
    为什么还出现:
    Server: Msg 8101, Level 16, State 1, Line 1
    An explicit value for the identity column in table 'table1' can only be specified when a column list is used and IDENTITY_INSERT is ON.
    Server: Msg 8101, Level 16, State 1, Line 1
    An explicit value for the identity column in table 'table1' can only be specified when a column list is used and IDENTITY_INSERT is ON.
      

  7.   

    我试过老和尚的和小干部的,应该都可以铁版主的当时试过,好像发现过一些问题,不知道改正过没有大力贴的:--------------------------------------------------------------------
    回复人: pengdali(大力) ( ) 信誉:381  2003-02-25 13:57:00  得分:0 
     
     
      太多了!都是转贴:.....-------------------------------------------------------------------------
    是老和尚的非最后版本,有问题
      

  8.   

    : pengdali(大力) 给的不太好用,生成的数据中超过三十的数据都截掉了。
      

  9.   

    To turbochen(程序员) 老衲和小干部儿的我在5000数据的表上试了都没有问题
      

  10.   

    turbochen(程序员) :
      我这里SET IDENTITY_INSERT table1 ON也不行,不知道什么原因
      这里的程序都没有考虑自增字段问题,也没办法考虑
      

  11.   

    小干部的好象可以:--测试小干部的:
    select count(*) from sysobjects
    exec spGenInsertSQL 'sysobjects'
      

  12.   

    to 经理这样就可以!!!!
    SET IDENTITY_INSERT table1 ON
    INSERT INTO table1 (id,aa,bb,val) VALUES(1,'xxx公司','xxx公司',NULL)
    INSERT INTO table1 (id,aa,bb,val) VALUES(46,'xx部','xx部',1)
    SET IDENTITY_INSERT table1 OFF
      

  13.   

    pengdali(大力) 的那个当字串长度超过30时被截掉了。上面 pengdali(大力) 给的set identity_insert脚本是对的,可以解决问题。
      

  14.   

    这个可以: create  proc spGenInsertSQL
    @TableName as varchar(100)
    as
    --declare @TableName varchar(100)
    --set @TableName = 'orders'
    --set @TableName = 'eeducation'
    DECLARE xCursor CURSOR FOR
    SELECT name,xusertype
    FROM syscolumns
    WHERE (id = OBJECT_ID(@TableName))
    declare @F1 varchar(100)
    declare @F2 integer
    declare @SQL varchar(8000)
    set @sql ='SELECT ''INSERT INTO ' + @TableName + ' VALUES('''
    OPEN xCursor
    FETCH xCursor into @F1,@F2
    WHILE @@FETCH_STATUS = 0
    BEGIN
        set @sql =@sql +
                  + case when @F2 IN (35,58,99,167,175,231,239,61) then ' + case when ' + @F1 + ' IS NULL then '''' else '''''''' end + '  else '+' end
                  + 'replace(ISNULL(cast(' + @F1 + ' as varchar(8000)),''NULL''),'''''''','''''''''''')' 
                  + case when @F2 IN (35,58,99,167,175,231,239,61) then ' + case when ' + @F1 + ' IS NULL then '''' else '''''''' end + '  else '+' end
                  + char(13) + ''',''' 
        FETCH NEXT FROM xCursor into @F1,@F2
    END
    CLOSE xCursor
    DEALLOCATE xCursor
    set @sql = left(@sql,len(@sql) - 5) + ' + '')'' FROM ' + @TableName
    exec (@sql)go
      

  15.   

    --测试:create table table1 (id int identity(1,1),aa varchar(100),bb varchar(100),val int)
    go
    insert table1 values ('12345678901234567890123456789012345678901234567890123456789012345678901234567890','aa',1)
    go
    exec spGenInsertSQL 'table1'
      

  16.   

    第一个是我写的,请使用FAQ中的代码。
    自增字段是可以的。仅当使用了列的列表,并且 IDENTITY_INSERT 为 ON 时,才能在表中为标识列指定显式值。http://expert.csdn.net/Expert/FAQ/FAQ_Index.asp?id=3477
      

  17.   


    部分语句参考竹之草大侠生成表结构的存储过程。 http://expert.csdn.net/Expert/topicview.asp?id=643555
      

  18.   

    应该很简单的问题才是啊,有这么复杂吗???
    我以前做过的就是没有大量的数据,一个表,
    我在查询分析器里选择输出到文件,文件类型就选SQL 文件,然后使用SELECT就好了,注:我是在SQL 2K里.
      

  19.   

    我手里还有一个这样的程序,可以保存为SQL脚本,就是INSERT之类的语句,