iT邦幫忙

1

請問 SQL Server 如何批量加上 with (nolock) 效果?

假如現在使用到N個表格需要加上N個 with (nolock),像是

select * from T1  with (nolock)
join T2 with (nolock) ..
join T3 with (nolock) ..
join T4 with (nolock) ..
....

想宣告一次就能讓接下來查詢都有 with (nolock) 效果

PPTaiwan iT邦好手 1 級 ‧ 2021-01-08 10:53:26 檢舉
是不太建議,如果你寫一些 CURD 與 SP 開啟了 set Transaction isolation level read uncommitted 有一些小細節 不加與加 With(nolock) 會有查詢上問題,細節真的要注意!!

別太懶,以後很多技術債都要還的!!
要看實際情境是否允許不使用記錄鎖定的做法,要是使用非鎖定模式,有可能發生無法預期的意外
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 個回答

1
w4560000
iT邦研究生 5 級 ‧ 2021-01-08 09:53:54
最佳解答

set Transaction isolation level read uncommitted

查詢前先設定,查詢就不用再加上 with (nolock)
只是若資料在別的交易rollback後,而你先query出來,會導致你的資料不是正確的

小MIS iT邦研究生 1 級 ‧ 2021-01-08 10:07:28 檢舉

高手!

我要發表回答

立即登入回答