方丈的:
drop proc proc_insert
go
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
end
go

解决方案 »

  1.   

    真是谢谢了!
    另外还得问一下,如果不用xtype判断数据类型可以么?
    感觉inser语句没标识数据类型啊,表结构已是现成做好的了,只要插入的值对就行。
    是不是还是为了安全么
      

  2.   

    最好的方法还是用第三方的专门软件,考虑得比较全面
    推荐:
    mssql数据生成器.exe
      

  3.   

    该软件的说明:SQL  Server  数据库SQL脚本导出器1.0
    (1)      支持所有数据类型(如binary,text,image)
    (2)      支持表结构、索引、所有记录到SQL脚本
    (3)      支持数据库、表的浏览
    (4)      SQL语句的编辑、执行
    (5)      可备份数据库、表结构、记录
      

  4.   

    上面的过程将自动生成以下语句:
    insert t (ID,学生ID,学生姓名,课程ID,课程名称,成绩,教师ID,教师姓名)  values ( 159,'S3','王五','K2','语文',8.100000000000000e+001,'T2','王老师')
    insert t (ID,学生ID,学生姓名,课程ID,课程名称,成绩,教师ID,教师姓名)  values ( 160,'S3','王五','K4','政治',5.300000000000000e+001,'T4','赵老师')
    insert t (ID,学生ID,学生姓名,课程ID,课程名称,成绩,教师ID,教师姓名)  values ( 161,'S4','赵六','K1','数学',9.900000000000000e+001,'T1','张老师')
    insert t (ID,学生ID,学生姓名,课程ID,课程名称,成绩,教师ID,教师姓名)  values ( 162,'S4','赵六','K2','语文',3.300000000000000e+001,'T2','王老师')
    insert t (ID,学生ID,学生姓名,课程ID,课程名称,成绩,教师ID,教师姓名)  values ( 163,'S4','赵六','K4','政治',5.900000000000000e+001,'T4','赵老师')
    insert t (ID,学生ID,学生姓名,课程ID,课程名称,成绩,教师ID,教师姓名)  values ( 164,'s1','张三','K4','政治',7.900000000000000e+001,'T4','赵老师')
    insert t (ID,学生ID,学生姓名,课程ID,课程名称,成绩,教师ID,教师姓名)  values ( 165,'s1','张三','K1','数学',9.800000000000000e+001,'T1','张老师')
    insert t (ID,学生ID,学生姓名,课程ID,课程名称,成绩,教师ID,教师姓名)  values ( 166,'s1','张三','K3','英语',6.900000000000000e+001,'T3','李老师')
    insert t (ID,学生ID,学生姓名,课程ID,课程名称,成绩,教师ID,教师姓名)  values ( 167,'s7','peter','K1','数学',6.400000000000000e+001,'T1','张老师')
    insert t (ID,学生ID,学生姓名,课程ID,课程名称,成绩,教师ID,教师姓名)  values ( 168,'s7','peter','K2','语文',8.100000000000000e+001,'T2','王老师')但是当有identity的字段时候
    需要
    set identity_insert yourtable on
    insert ---
    ----------
    set identity_insert yourtable off

    上面的可否修改一下如果存在identity,则在头尾加上上面的两句呢?
      

  5.   

    http://expert.csdn.net/Expert/topic/2264/2264319.xml?temp=.7615625
      

  6.   

    我认为通过BCP导出文本文件,在导入是再通过BCP导入是比较安全和高效的方法!
      

  7.   

    if exists (select 1 from sysobjects where name='new' and xtype='U')
    drop table new
    else
    begin
    select * into new from old where 1<>1 --复制表结构
    INSERT INTO new
       SELECT *
      FROM old
     select * from new  
    end
      

  8.   

    to: gmlxf(烛光)
    语句是产生了!但不知道怎么用!