Search code examples
sql-servertransactionssql-view

MS SQL Transactions in Views


Is it possible to create transactions in views? e.g.:

CREATE View NewView
AS
 BEGIN TRANSACTION
  SELECT * FROM TableA
 COMMIT
GO

Solution

  • No, it is not possible to use transaction in Views, as Views are for returning rows from one or more tables, they cannot make changes on your tables hence no need for transactions.

    You can of course put your SELECT query in a transaction:

    BEGIN TRANSACTION
      SELECT * FROM NewView
    COMMIT
    

    But this is completely unnecessary. Here you can find some explanations about what transactions are and when to use them:

    https://www.mssqltips.com/sqlservertutorial/3304/what-is-a-transaction/

    https://learn.microsoft.com/en-us/sql/t-sql/language-elements/transactions-transact-sql?view=sql-server-ver15

    https://www.c-sharpcorner.com/UploadFile/84c85b/understanding-transactions-in-sql-server/