Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I include an ALTER VIEW statement in a transaction for a deploy script?

Tags:

sql

sql-server

I'm running SQL Server 2005 on prod, but developing on 2008, and I need to alter a view to add a column. However I'm having trouble creating the deploy script because I need to wrap it in a transaction like this

begin tran;

alter view [dbo].[v_ViewName] with schemabinding
as 
    select ... 

    /* do other stuff */
commit;

When I do this the SQL IDE underlines the alter statement with an error saying that the 'ALTER VIEW' statement must be the only statement in a batch. ANd if I ignore this and just try and run it anyway it gives this error:

Incorrect syntax near the keyword 'view'.

Any ideas how to get around this?

like image 730
Glenn Slaven Avatar asked Dec 29 '22 08:12

Glenn Slaven


1 Answers

A transaction can span multiple batches:

begin tran;
GO

alter view [dbo].[v_ViewName] 
with schemabinding
as         
  select ...     
GO

/* do other stuff */
GO

commit;
GO
like image 67
Remus Rusanu Avatar answered Apr 07 '23 18:04

Remus Rusanu