服務熱線
153 8323 9821
mssql
事務的使用:
1.begin distributed transaction 指定一個事務的起始。
原型:
Begin destributed tran [ saction] [transaction_name! @tran_name_variable]
例如:
Use northwind
Begin destributed transaction //開始一個分布式事務
Update empoyees //對表empolees 進行更新
Set firstname=’mcdonald’ where homephone = ‘(206) 555-9875’
Commit transaction //結束事務
Go
2.commit transcation 指明事務結束。
3.rollback transcation 撒銷對數據庫作出的所有改變,返回到事務開始之前的狀態。
原型:
Rollback [tran [saction] [transaction_name] @tran_name_variable | savepoint_name |
@savepoint_variable]
參數說明:
Transadtion 給begin transaction上的事務指派的名稱。
@tran_name_bariable 用戶定義的、含有有效事務名稱的變量名稱。
Savepoint_name 是來自SAVE TRANSACTION語句的svepoint_name。
@savepoint_variable 是用戶定義的、含有有效保存點名稱的變量的名稱。
例如:
Begin transaction royaltychange //事務開始
Update titleauthor //更新表
Set royaltyper = 65 //重設參數
From titleauthor,titles
Where royaltyper=75
And titleauthor.title_id=titles.title_id
And title=’The Gourmet Microwave’
Update titleauthor
Set royaltype = 15
From titleauthor, titles
Where royaltyper=25
And titleauthor.title_id=titles.title_id
And title = ‘The Gourmet Microwave’
Save transaction percentchanged //在事務內設置保存點
Update titles
Set price = price* 1.1
Where title =’The Gourmet Microwave’
Select (price * royalty * ytd_sales)* royaltyper
From titles,titleauthor
Where title=’The Gourmet Microwave’
And titles.title_id=titleauthor.title_id
Rollback transaction percentchanged //回到先前保存過的保存點
Commit transaction // 事務結束
4.save transaction 在事務內設置保存點。
5.commit work 標志事務的結束。
6.rollback work 將用戶的事務回滾到事務的起點。
游標的使用:
1.declare cursor 定義游標結構并分配資源。
原型:
Declate cursor_name [insensitive] [scorll] cursor for select_statement [for { read
Only | update }[of column _list]]
或者:
Declare cursor_name cursor [local | global] [forward_only | scroll] [static | keyset |
dynamic] [read_only | scroll_locks | optimistic] for [select _statement
[ for {read only | update } [of column_list]]]
參數說明:
INSENSITIVE 指明要為檢索到的結果集建立一個臨時拷貝,以后的數據從這個臨時拷貝中獲取。原有基表中數據發生了改變,對于游標而言是不可見的。這種不敏感的游標不允許數據更改。
SCROLL 指明游標可以在任意方向上滾動。忽略該選項,則游標只能向前滾動。
SELECT_SATAEMENT 指明SQL語句建立的結果集。
READ ONLY 指明在游標結果集中不允許進行數據更改。
UPDATE 指明游標結果集可以進行修改。
OF COLUMN_LIST 指明結果集中可以進行修改的列。缺省時(使用UPDATE關鍵字),所有的列都可進行修改。
LOCAL 指明游標是局部的,只能在它所聲明的過程中使用。全局的游標在連接激活的任何時候都是可用的。只有池連接結束時,才不再可用。
GLOBAL 使用游標對于整個連接全局可見。
FORWARD_ONLY 指明游標只能向前滾動。
STATIC 與INSENITIVE的游標相同。
KEYSET 指明選取的行的順序。
DYNAMIC 指明游標反映所有對結果集的修改。
SCROLL_LOCK 對修改或刪除加鎖。保證游標操作成功。
OPTIMISTIC 指明哪些通過游標進行的修改或者刪除將不會成功。
例如:
Use northwind
Go
Declare customers_cursor cursor //定義游標