国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 數(shù)據(jù)庫(kù) > SQL Server > 正文

SQL Server 開發(fā)之 復(fù)制表數(shù)據(jù)的SQL腳本生成器

2024-08-31 00:49:16
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

商業(yè)源碼熱門下載www.html.org.cn

使用sql server 2000自帶的“生成sql腳本”工具,可以生成創(chuàng)建表、視圖、存儲(chǔ)過(guò)程等的sql腳本。那么,能否將表中的數(shù)據(jù)也生成為sql腳本,在查詢分析器中執(zhí)行這些腳本后自動(dòng)將數(shù)據(jù)導(dǎo)入到sql server中呢?答案是肯定的。
下面的存儲(chǔ)過(guò)程是一位高人寫的,這位高人的姓氏已無(wú)人知曉,但sql server社區(qū)中偶爾還可看到此不朽之作。
create procedure  dbo.outputdata     
  @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   --   判斷對(duì)象是否存在     
     begin      
      print  @tablename +  '對(duì)象不存在'     
      return      
    end     
  set @objectname=rtrim(object_name(@objectid))     
  if @objectname is null or charindex(@objectname,@tablename)=0
    begin      
      print  @tablename +  '對(duì)象不在當(dāng)前數(shù)據(jù)庫(kù)中'      
      return      
    end        
  if  objectproperty(@objectid,'istable')   <   >   1   --   判斷對(duì)象是否是表     
    begin      
      print  @tablename +  '對(duì)象不是表'     
      return      
    end        
  select   @ident=status&0x80   from   syscolumns   where   [email protected]   and   status&0x80=0x80         
  if @ident is   not   null      
    print   'set   identity_insert   '+ @tablename + '   on'    
  --定義游標(biāo),循環(huán)取數(shù)據(jù)并生成insert語(yǔ)句
  declare  syscolumns_cursor  cursor for   
    select   c.name,c.xtype   from   syscolumns   c     
      where   [email protected]     
      order   by   c.colid    
  --打開游標(biāo)
  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 暫時(shí)不處理     
        begin      
        set   @[email protected] + 
          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   '''''''''+'[email protected]+'+'''''''''                --varchar,char     
                 when   @xtype   in(231,239)   then   '''n''''''+'[email protected]+'+'''''''''             --nvarchar,nchar     
                 when   @xtype=61   then   '''''''''+convert(char(23),'[email protected]+',121)+'''''''''   --datetime     
                 when   @xtype=58   then   '''''''''+convert(char(16),'[email protected]+',120)+'''''''''   --smalldatetime     
                  when   @xtype=36   then   '''''''''+convert(char(36),'[email protected]+')+'''''''''       --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   '[email protected]+'('[email protected]+')   values(''as   ''--'','[email protected]+','')''   from   '[email protected]        
  print   '--'[email protected]     
  exec(@sql)         
  if   @ident   is   not   null      
  print  'set   identity_insert   '[email protected]+'   off'    
調(diào)用時(shí) exec   outputdata   'myuser' 其中myuser中當(dāng)前數(shù)據(jù)庫(kù)中存在的表
   
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 滕州市| 吉木萨尔县| 长宁县| 富蕴县| 蛟河市| 三河市| 上虞市| 辉县市| 石棉县| 青海省| 青龙| 丹凤县| 林周县| 改则县| 普兰店市| 收藏| 志丹县| 思茅市| 漠河县| 神木县| 乌拉特前旗| 四会市| 湖州市| 万年县| 昭通市| 定兴县| 东阿县| 惠东县| 泰宁县| 应城市| 平远县| 永平县| 横山县| 连平县| 平湖市| 五指山市| 嫩江县| 桂东县| 张家口市| 库尔勒市| 汝州市|