在SQLServer2005中生成脚本时   怎么设置可以使脚本可以包含里面的记录?????

解决方案 »

  1.   

    /*
    调用方法
    exec  proc_insert   TNewSidLog
    */
    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 
      

  2.   

    ****************************************************************************
     软件名称: May Flower Erp
     版权所有: (C) 2005-2006 May Flower ERP 开发组
     功能描述: 获取指定表的创建脚本,包括表和字段的属性、外键(注释掉的)
    ----------------------------------------------------------------------------
     参数列表: 
            1: @TableName 需要创建脚本的表的名称
    ****************************************************************************
    |</PRE>*/
    CREATE PROCEDURE [dbo].[sp_HelpTable](@TableName sysname)
    AS
      SET NOCOUNT ON
      DECLARE @ObjectID int
      DECLARE @TableScript table(Iden int IDENTITY(1, 1), ScriptLine nvarchar(4000))
      SET @ObjectID = object_id(@TableName)
      IF @ObjectID IS NULL OR OBJECTPROPERTY(@ObjectID, 'IsTable') = 0
      BEGIN
        RAISERROR('指定的对象不是表对象', 16, 1)
        RETURN
      END 
      --获取表的创建脚本
      --插入表头
      INSERT INTO @TableScript(ScriptLine)
        SELECT N'CREATE TABLE [' + USER_NAME(OBJECTPROPERTY(@ObjectID, N'OwnerId')) + N'].[' + object_name(@ObjectID) + N']('  --插入字段
      INSERT INTO @TableScript(ScriptLine)
        SELECT N'  [' + a.Name + N'] [' + b.name + N']' + 
               CASE WHEN c.Object_id IS NOT NULL THEN N' IDENTITY(' + CONVERT(nvarchar, c.seed_value) + N', ' + CONVERT(nvarchar, c.increment_value) + N')' 
                    ELSE '' END +
               CASE WHEN b.xusertype IN (167, 175, 231, 239) THEN N'('+CONVERT(nvarchar, a.prec)  + N')'
                    WHEN b.xusertype in (106, 108)           THEN N'('+CONVERT(nvarchar, a.xprec) + N', ' + CONVERT(nvarchar, a.xscale) + N')'
                    ELSE '' END +
               CASE a.isnullable WHEN 1 THEN N'' ELSE N' NOT' END + N' NULL' +
               CASE WHEN d.Name IS NOT NULL THEN N' DEFAULT ' + d.Definition ELSE N'' END +
               N',' 
          FROM sys.syscolumns a
            LEFT JOIN sys.systypes b ON a.xusertype = b.xusertype
            LEFT JOIN sys.identity_columns c ON c.Object_id = a.ID AND c.Column_ID = a.ColID
            LEFT JOIN sys.default_constraints d ON d.Parent_Object_ID = a.ID AND d.Parent_column_ID = a.ColID
        WHERE a.[ID] = @ObjectID
        ORDER BY a.ColOrder  --插入主键和索引
      DECLARE @IndexID int, @IndexScript nvarchar(4000)
      DECLARE IndexCursor CURSOR FOR 
        SELECT b.Index_ID, N'  CONSTRAINT [' + a.Name + N'] ' + 
               CASE a.Type WHEN 'PK' THEN N'PRIMARY KEY ' WHEN 'UQ' THEN N'UNIQUE ' END + 
               CASE b.Type WHEN 1    THEN N'CLUSTERED'   WHEN 2     THEN N'NONCLUSTERED ' END + N'('
          FROM sys.key_constraints a
            LEFT JOIN sys.indexes b ON b.Object_ID = a.Parent_Object_ID AND b.index_id = a.unique_index_id
        WHERE a.Parent_Object_ID = @ObjectID
      OPEN IndexCursor
      FETCH NEXT FROM IndexCursor INTO @IndexID, @IndexScript
      WHILE @@FETCH_STATUS = 0
      BEGIN
        SELECT @IndexScript = @IndexScript + N'[' + INDEX_COL(object_name(@ObjectID), 2 , 1) + N'],'
          FROM sys.index_columns 
        WHERE Object_ID = @ObjectID
          AND Index_ID = 2
        
        SET @IndexScript = LEFT(@IndexScript, LEN(@IndexScript) -1) + N'),'
        INSERT INTO @TableScript(ScriptLine) VALUES(@IndexScript)
        FETCH NEXT FROM IndexCursor INTO @IndexID, @IndexScript
      END
      Close IndexCursor
      DEALLOCATE IndexCursor  insert into @TableScript(ScriptLine) VALUES(')')
      select * from @TableScript 本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/fredrickhu/archive/2009/10/14/4669511.aspx
      

  3.   

    将表数据生成SQL脚本的存储过程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 cursorfor 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' GOexec UspOutputData 你的表名