SQL SERVER 常用命令

來源:互聯網
上載者:User

標籤:role   inpu   plink   角色   inner   har   spi   死結   文本   

紅色為常用

0、row_number() over 和資料群組合sale/cnt

select *,row_number() over(order by productname) as rownumber
from products
select row_number() over(order by sale/cnt desc) as sort, sale/cnt
from (
select -60 as sale,3 as cnt from dual union
select 24 as sale,6 as cnt from dual union
select 50 as sale,5 as cnt from dual union
select -20 as sale,2 as cnt from dual union
select 40 as sale,8 as cnt from dual);

1. 查看資料庫的版本
select @@version

select @@connections //返回 SQL Server 自上次啟動以來嘗試的串連數,無論串連是成功還是失敗

select @@max_connections //返回 SQL Server 執行個體允許同時進行的最大使用者串連數。返回的數值不一定是當前配置的數值

select @@lock_timeout //返回當前會話的當前鎖定逾時設定(毫秒)。
2.查看資料庫裡使用者和進程的資訊
sp_who
查看SQL Server資料庫裡的活動使用者和進程的資訊
sp_who ‘‘active‘‘
查看SQL Server資料庫裡的鎖的情況
sp_lock

進程號1--50是SQL Server系統內部用的,進程號大於50的才是使用者的串連進程.
spid是進程編號,dbid是資料庫編號,objid是資料對象編號
查看進程正在執行的SQL語句
dbcc inputbuffer ()

推薦大家用經過改進後的sp_who3過程可以直接看到進程啟動並執行SQL語句
sp_who3

檢查死結用sp_who_lock過程
sp_who_lock

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

4. 查看資料庫啟動的參數
sp_configure

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

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


6.查看某資料庫下某個資料對象的大小
sp_spaceused @objname


7.查看資料庫裡所有的預存程序和函數
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過程


8.分析SQL Server SQL 陳述式的方法:
set statistics time {on | off}
set statistics io {on | off}
圖形方式顯示查詢執行計畫

在查詢分析器->查詢->顯示估計的評估計劃(D)-Ctrl-L 或者點擊工具列裡的圖形

文本方式顯示查詢執行計畫
set showplan_all {on | off}

set showplan_text { on | off }
set statistics profile { on | off }

9. 查看資料庫所在機器作業系統參數
exec master..xp_msver

10. 查看資料庫啟動時間
select convert(varchar(30),login_time,120) from master..sysprocesses where spid=1

11. 查看連結的伺服器
sp_helplinkedsrvlogin

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

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

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

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

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

12.收縮資料庫記錄檔的方法
收縮簡單復原模式資料庫日誌,收縮後@database_name_log的大小單位為M
backup log @database_name with no_log
dbcc shrinkfile (@database_name_log, 5)

 

13.已知列名,尋找表

select tab.name from syscolumns as col inner join sysobjects as tab on col.id=tab.id where col.name=‘Item_IDX‘

SQL SERVER 常用命令

聯繫我們

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