2

Is there a way to run multiple CREATE VIEW commands and/or multiple GRANT SELECT commands by hitting F5 in SQL Server Management Studio? If so, how can I do this?

1
  • 1
    You just need the batch terminator, which in SSMS is GO unless you altered this default
    – S3S
    Sep 4, 2018 at 20:05

2 Answers 2

6

Multiple CREATE VIEW statements can be executed together if you separate them with keyword 'GO'

CREATE VIEW vw_test1 AS SELECT name FROM dbo.test1;
GO
CREATE VIEW vw)test2 AS SELECT name FROM dbo.test2;
GO
GRANT SELECT ON vw_test.... 
0
3

You can use dynamic SQL. Encapsulate your CREATE VIEW and/or GRANT SELECT statement in text. ex:

DECLARE @Query NVARCHAR(50)
  SET @Query = 'CREATE VIEW ... '
  exec sp_executesql @Query

You can do that for all the queries you need to run and then just press F5

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.