久久久久久久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(表與臨時表性能)
主站蜘蛛池模板: 欧美不卡视频一区发布 | 热久久性| 亚洲精品视频免费观看 | 国内精品免费久久久久软件老师 | 久久久久亚洲精品国产 | 日韩成人| 国产精品久久久久久久久久 | 色播av| 日韩精品 | 久操福利| 欧美在线一区二区视频 | 国产精品美女久久久久久久久久久 | 日本不卡免费新一二三区 | 国产精品九九九 | 国产999精品久久久 日本视频一区二区三区 | 中文字幕成人在线 | 欧美一级观看 | 9久久 | 欧美午夜精品久久久久久浪潮 | 水蜜桃久久夜色精品一区 | 波多野结衣一区二区三区在线观看 | 美女131mm久久爽爽免费 | 国产一区二区av | 伊人狠狠 | 免费看国产片在线观看 | 一级片在线免费播放 | 精品国产一区二区三区日日嗨 | 午夜一区| h视频在线观看免费 | 国产综合精品一区二区三区 | 欧美一级网站 | 午夜影院网站 | 日韩2020狼一二三 | 欧美福利在线 | 国产一区二区麻豆 | 一级在线观看 | 欧美日韩国产精品一区 | 日韩精品中文字幕一区二区三区 | 久久久久国产精品一区二区 | 天天爽夜夜操 | 日韩在线91 |