當說到跟蹤SQL語句,有人可能會問起這個功能,也許有人會想到用審計功能,為了跟蹤SQL語句,去啟用審計功能,會成為一種浪費,或許使用者只是想跟蹤某一個串連的所有SQL語句呢?
請看簡單的樣本:
1> set tracefile "d:/abc.txt"
2> go
1> set show_sqltext on
2> set statistics io on
3> go
1> select * from t123
2> go
id col2
----------- --------------------------------
再開啟d:\abc.txt,內容如下:
================================================================================
Application Tracing report for spid 26 from application "isql"
run by login "sa" on host "SEAN-E6400"
Tracing started on 2013/02/07 05:15:34.48
================================================================================
2013/02/07 05:15:50.70
No useful sqltext available.
Total writes for this command: 0
End of Batch 4
2013/02/07 05:15:55.28
SQL Text: select * from t123
Table: t123 scan count 1, logical reads: (regular=1 apf=0 total=1), physical reads: (regular=0 apf=0 total=0), apf IOs used=0
Total writes for this command: 0
End of Batch 5
這樣看來,還是蠻方便的,索引把上邊的on/off開關選項,常用的都列到這裡:
1. set show_sqltext <on/off>
2. set showplan <on/off>
3. set statistics io <on/off>
4. set statistics time <on/off>
5. set statistics plancost <on/off>
需要說明的是:
set tracefile “<file-path>” for <spid> 這是跟蹤對應<spid>的串連的所有SQL語句
set tracefile "<file-path>"只跟蹤當前串連的所有SQL語句。
set tracefile on/off (啟用或關閉)
當然,dbcc traceon(100) 也會輸出一些跟蹤的結果在伺服器端。