SQL大量新增資料庫中所有使用者資料表描述

來源:互聯網
上載者:User

--SQL大量新增資料庫中所有使用者資料表描述

--操作說明:請先準備一資料表為名稱為tblist,表名欄位tbname nvarchar(255),資料表描述欄位chnname nvarchar(255),將所有已存在的資料表和對應描述添加到資料表tblist中

--指令碼編寫步驟:1、遊標讀取所有資料表名;2、讀取指定資料表描述;3、判斷指定資料表描述是否存在,存在則更新,否則就直接添加。

--查詢資料表描述指令碼:select * from sys.extended_properties where name='MS_Description' and minor_id=0

declare @TableName nvarchar(250) --定義當前操作的資料表名

declare @TableDescription nvarchar(250)  --定義當前讀取到的資料表描述變數

--聲明讀取資料庫所有資料表名稱遊標mycursor
declare mycursor cursor for select name from dbo.SysObjects WHERE OBJECTPROPERTY(ID, 'IsUserTable') = 1
 --開啟遊標
open mycursor
--從遊標裡取出資料賦值到我們剛才聲明的資料表名變數中
fetch next from mycursor into @TableName
--如果遊標執行成功 
while (@@fetch_status=0)
begin   
 --擷取指定資料表描述
 IF EXISTS (select * from tblist WHERE tbname=''+@TableName+'')
 begin
 --如果指定資料表描述已存在則賦值讀取到的描述給變數
 select @TableDescription=chnname from tblist WHERE tbname=''+@TableName+''  
 end
 else
 begin
 --如果指定資料表描述已存在則賦值資料表名給變數
 set @TableDescription=@TableName 
 end
 
 --添加或更新資料表描述
 IF EXISTS (select * from sys.extended_properties WHERE name='MS_Description' and major_id=OBJECT_ID(''+@TableName+'') and minor_id=0)
 begin
 --如果指定資料表描述已存在則直接更新
 EXEC sp_updateextendedproperty 'MS_Description',@TableDescription,'user',dbo,'table',@TableName
 end
 else
 begin
 --如果指定資料表描述不存在則直接添加
 EXECUTE sp_addextendedproperty 'MS_Description',@TableDescription,'user','dbo','table',@TableName 
 end
 print '資料表['+@TableName+']描述['+@TableDescription+']添加成功'
 
  --用遊標去取下一條記錄
    fetch next from mycursor into @TableName
end

--關閉遊標
close mycursor
--撤銷遊標
deallocate mycursor

 

聯繫我們

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