轉:鄒建--對於局部變數,text、ntext 和 image 資料類型無效

來源:互聯網
上載者:User

轉:http://topic.csdn.net/t/20040627/22/3126726.html

問題 

取值問題!!  
  以及用Text在使用Select   查詢   和INSERT   Update解決之道!  
  比喻發通過declare   @content   Text   去Select   @content=content(Text類型)最值呢。。。尋求Text類型欄位的解決之道!!!

  -- text欄位增加處理   
    
   -- 建立測試表   
   create     table    test(id    varchar ( 3 ),detail    text )   
   insert     into    test   
   select     ' 001 ' , ' A*B '    
    
   -- 定義添加的的字串   
   declare     @s_str     varchar ( 8000 ), @postion     int    
   select     @s_str = ' *C '     -- 要添加的字串   
  , @postion = null   -- 追加的位置,null   加在尾部,0   加在首部,其他值則加在指定位置   
    
   -- 字串添加處理   
   declare     @p     varbinary ( 16 )   
   select     @p = textptr (detail)    from    test    where    id = ' 001 '    
   updatetext    test.detail    @p     @postion     0     @s_str    
    
   -- 顯示處理結果   
   select     *     from    test   
   go    
    
   -- 刪除測試表   
   drop     table    test   

 

 

  -- text欄位的替換處理樣本--全表替換   
    
   -- 建立資料測試環境   
   create     table    test(id    varchar ( 3 ),txt    text )   
   insert     into    test   
   select     ' 001 ' , REPLICATE ( ' a ' , 4000 )    +     REPLICATE ( ' b ' , 2000 )    +     REPLICATE ( ' ab ' , 2000 )   
   union     all     select     ' 002 ' , REPLICATE ( ' a ' , 4000 )    +     REPLICATE ( ' b ' , 2000 )    +     REPLICATE ( ' ab ' , 2000 )   
   go    
   select     datalength (txt), *     from    test   
    
   -- 定義替換的字串   
   declare     @s_str     varchar ( 8000 ), @d_str     varchar ( 8000 )   
   select     @s_str = ' a '     -- 要替換的字串   
  , @d_str = ' ab '   -- 替換成的字串   
    
    
   -- 因為只能用patindex,所以對於搜尋字串做處理   
   set     @s_str = ' % ' + @s_str + ' % '    
    
   -- 定義遊標,迴圈處理資料   
   declare     @id     varchar ( 3 )   
   declare    #tb    cursor     for     select    id    from    test   
   open    #tb   
   fetch     next     from    #tb    into     @id    
   while     @@fetch_status = 0    
   begin    
   -- 字串替換處理   
   declare     @p     varbinary ( 16 )  &nbs

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.