SQL Server資料庫管理常用的SQL和T-SQL語句

來源:互聯網
上載者:User

  1. 查看資料庫的版本

  select @@version

  2. 查看資料庫所在機器作業系統參數

  exec master..xp_msver

  3. 查看資料庫啟動的參數

  sp_configure

  4. 查看資料庫啟動時間

  select convert(varchar(30),login_time,120) from master..sysprocesses where spid=1

  查看資料庫伺服器名和執行個體名

  print 'Server Name...............: ' + convert(varchar(30),@@SERVERNAME)

  print 'Instance..................: ' + convert(varchar(30),@@SERVICENAME)

  5. 查看所有資料庫名稱及大小

  sp_helpdb

  重新命名資料庫用的SQL

  sp_renamedb 'old_dbname', 'new_dbname'

  6. 查看所有資料庫使用者登入資訊

  sp_helplogins

  查看所有資料庫使用者所屬的角色資訊

  sp_helpsrvrolemember

  修複遷移伺服器時孤立使用者時,可以用的fix_orphan_user指令碼或者LoneUser過程

  更改某個資料對象的使用者屬主

  sp_changeobjectowner [@objectname =] 'object', [@newowner =] 'owner'

  注意: 更改對象名的任一部分都可能破壞指令碼和預存程序。

  把一台伺服器上的資料庫使用者登入資訊備份出來可以用add_login_to_aserver指令碼

  7. 查看連結的伺服器

  sp_helplinkedsrvlogin

  查看遠端資料庫使用者登入資訊

  sp_helpremotelogin

  8.查看某資料庫下某個資料對象的大小

  sp_spaceused @objname

  還可以用sp_toptables過程看最大的N(預設為50)個表

  查看某資料庫下某個資料對象的索引資訊

  sp_helpindex @objname

  還可以用SP_NChelpindex過程查看更詳細的索引情況

  SP_NChelpindex @objname

  clustered索引是把記錄按物理順序排列的,索引占的空間比較少。

  對索引值DML操作十分頻繁的表我建議用非clustered索引和約束,fillfactor參數都用預設值。

  查看某資料庫下某個資料對象的的約束資訊

  sp_helpconstraint @objname

  9.查看資料庫裡所有的預存程序和函數

  use @database_name

  sp_stored_procedures

  查看預存程序和函數的原始碼

  sp_helptext '@procedure_name'

  查看包含某個字串@str的資料對象名稱

  select distinct object_name(id) from syscomments where text like '%@str%'

  建立加密的預存程序或函數在AS前面加WITH ENCRYPTION參數

 

  解密加密過的預存程序和函數可以用sp_decrypt過程

聯繫我們

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