sql server中格式化表中的資料

來源:互聯網
上載者:User
server|資料          在資料庫開發中,由於使用者錄入資訊的隨意性,可能產生表內字串類型的
資料兩端存有空格,或大小寫不一致等現象,給以後資料應用過程中造成不必要的麻煩。這裡簡單使用了一個預存程序來解決這些問題。

/* 整理系統資料*/
CREATE  PROCEDURE pro_ClearupData
as
--第一部分 整理字串類型的資料  去除兩端的空格
  declare @tableName  varchar(50)  --表名
  declare @columnName varchar(50)  --列名
  declare cur_find    cursor for select so.name,sc.name
                                   from syscolumns sc, sysobjects so, systypes st
                                  where so.name <> 'dtproperties'
                                    and st.xtype=sc.xtype
                                    and st.name='varchar'
                                    and sc.id=so.id
                                    and so.xtype='u'
   --尋找包含varchar類型欄位的所有使用者表
   open cur_find
   fetch next from cur_find into @tableName,@columnName
   while @@fetch_status=0
   begin
      --去掉欄位的兩端空格
      exec('update '+@tableName+' set '+@columnName+'=ltrim(rtrim('+@columnName+'))')
      fetch next from cur_find into @tableName,@columnName
   end
   close cur_find
   deallocate cur_find
GO


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.