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
    You just need the batch terminator, which in SSMS is GO unless you altered this default – scsimon Sep 4 '18 at 20:05

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;
CREATE VIEW vw)test2 AS SELECT name FROM dbo.test2;
GRANT SELECT ON vw_test.... 

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

  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, privacy policy and cookie policy

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