標籤: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 常用命令