久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

我們可以在 SQL 事務中多次使用“GO"嗎?

Can we use #39;GO#39; multiple times in SQL Transaction?(我們可以在 SQL 事務中多次使用“GO嗎?)
本文介紹了我們可以在 SQL 事務中多次使用“GO"嗎?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我們可以在一個 SQL 事務中多次使用 GO 語句嗎?我有一個很長的 T-SQL 腳本,我想在 SQL Transaction 中運行它.如果一切順利,那么我會提交,否則會回滾.

Can We use GO statement mutiple times in a SQL Transaction. I am having a long T-SQL script and I want to run it in a SQL Transaction. If all goes well then I will commit otherwise will rollback.

但是,在運行該查詢時,我遇到了類似'create function must be the only statement in the batch'的錯誤.因為我正在創建和刪除許多函數和過程.

But, While running that query I got error like 'create function must be the only statement in the batch'. As I am creating and dropping many Functions and Procedures in that.

我沒有在腳本的任何地方使用 GO.我的問題是 - 我可以在那個長腳本中多次使用 GO 語句.因為,GO 創建了一個批處理,如果批處理第一次執行成功,但下次執行失敗,那么rollback transaction 語句是否能夠真正回滾已執行的事務?

I have not used GO anywhere in the script. My question is that - Can I use multiple times GO statement in that long script. Because, GO creates a batch and if batch executes successfully first time but fails next time then will rollback transaction statement be able to actually rollback that has been executed ?

我的腳本結構如下:

PRINT 'Transaction Started'
BEGIN TRY
    BEGIN TRAN

    Drop Function 
    ....
    ....
    Create Function
    ....
    ....
    Drop Procedure
    ....
    ....
    Lots of statements
    ....
    ....

    COMMIT TRAN
    PRINT 'Transaction Succeeded'
END TRY
BEGIN CATCH
    PRINT 'Transaction Failed'
    IF(@@TRANCOUNT > 0)
        ROLLBACK TRAN
END CATCH

我創建此腳本是為了在單個腳本中將一些更改從 newDB 遷移到 oldDB.

I am creating this script to migrate some changes from newDB to oldDB in a single script.

推薦答案

您正在混合概念.GO 不是 Transact-SQL 的概念,不是語言的一部分,并且不被 SQL Server 理解.GO 是工具批處理分隔符.sqlcmd.exe 和 SSMS 默認都使用 GO 作為批處理分隔符.批處理分隔符用于標識 SQL 源文件中的各個批處理.客戶端工具一次一批地向服務器發送(當然省略分隔符).

You are mixing concepts. GO is not a Transact-SQL concept, not part of the language, and not understood by SQL Server. GO is the tools batch delimiter. sqlcmd.exe and SSMS both are using, by default, GO as the batch delimiter. The batch delimiter is used to identify the individual batches inside the SQL source file. The client tool sends to the server one batch at a time (of course, omitting the delimiter).

交易可以跨批次進行.TRY/CATCH 塊不能.CREATE/ALTER 語句必須是批處理中唯一的語句(注釋不是語句,包含在函數過程體中的語句是包含的).

Transactions can span batches. TRY/CATCH blocks cannot. CREATE/ALTER statements must be the only statement in a batch (comments are not statements, and statements contained in a function procedure body are,well, contained).

通過啟動事務并在第一個錯誤時中止執行(-b at sqlcmd.exe start,或使用:on error exit in SSMS).

Something similar to what you want to do can be achieved by starting a transaction and abortign the execution on first error (-b at sqlcmd.exe start, or use :on error exit in SSMS).

但是在長事務中執行 DDL 是行不通的.特別是如果您打算將它與 DML 混合使用.我必須調查的大多數損壞都來自這種組合(Xact、DDL + DML、回滾).我強烈建議不要這樣做.

But doing DDL inside long transactions is not going to work. Specially if you plan to mix it with DML. Most corruptions I had to investigate come from this combination (Xact, DDL + DML, rollback). I strongly recommend against it.

安全部署架構更新的唯一方法是在出現問題時進行備份、部署和恢復.

The sole way to deploy schema updates safely is to take a backup, deploy, restore from backup if something goes wrong.

請注意,Dan 推薦的(動態 SQL)之所以有效,是因為 sp_executesql 會啟動一個新的內部批處理.此批次將滿足 CREATE/ALTER 限制.

Note that what Dan recommends (dynamic SQL) works because sp_executesql starts a new, inner, batch. This batch will satisfy the CREATE/ALTER restrictions.

這篇關于我們可以在 SQL 事務中多次使用“GO"嗎?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Sql server table usage statistics(Sql server 表使用情況統計)
Relative path in t sql?(t sql中的相對路徑?)
Getting the last record in SQL in WHERE condition(在 WHERE 條件下獲取 SQL 中的最后一條記錄)
Query to get XML output for hierarchical data using FOR XML PATH in SQL Server(在 SQL Server 中使用 FOR XML PATH 查詢以獲取分層數據的 XML 輸出)
T-SQL IF statement embedded in a sum() function(嵌入在 sum() 函數中的 T-SQL IF 語句)
Table vs Temp Table Performance(表與臨時表性能)
主站蜘蛛池模板: 91国内外精品自在线播放 | 一级少妇女片 | 国产一区视频在线 | 91精品久久久久久久 | 久久亚洲欧美日韩精品专区 | 91精品国产一区二区三区 | 一二区成人影院电影网 | 欧美黄在线观看 | 激情久久久久 | 欧美日韩精品一区二区三区四区 | 日韩理论电影在线观看 | 高清视频一区二区三区 | 91精品无人区卡一卡二卡三 | 国产午夜影院 | 久久精品亚洲精品国产欧美kt∨ | 精品毛片视频 | 国产一区二区美女 | 拍真实国产伦偷精品 | 97人人爱| 91亚洲精| 91久久久久久 | 91欧美激情一区二区三区成人 | 国产精品美女久久久久久久网站 | 激情久久网 | 成人国产网站 | 精品亚洲一区二区三区四区五区高 | 久热爱 | 在线91| 午夜欧美 | 成人h视频在线 | 久久久久久国产精品mv | 九九色综合 | 网站黄色在线免费观看 | 激情a | 日韩欧美在 | 夜夜爽99久久国产综合精品女不卡 | 97国产精品视频人人做人人爱 | www.一区二区三区 | 国产一在线观看 | 一区二区三区免费网站 | av电影手机版 |