{"id":305,"date":"2016-05-24T22:29:07","date_gmt":"2016-05-25T05:29:07","guid":{"rendered":"https:\/\/database.guide\/?p=305"},"modified":"2020-02-14T16:31:55","modified_gmt":"2020-02-14T23:31:55","slug":"how-to-create-a-view-in-sql-server","status":"publish","type":"post","link":"https:\/\/database.guide\/how-to-create-a-view-in-sql-server\/","title":{"rendered":"How to Create a View in SQL Server"},"content":{"rendered":"<p>To create a <a href=\"https:\/\/database.guide\/what-is-a-view\/\">view<\/a> in <a href=\"https:\/\/database.guide\/what-is-sql-server\/\">SQL Server<\/a>:<\/p>\n<ol>\n<li>Open a new query by clicking\u00a0the <kbd>New Query<\/kbd> button in the <a href=\"https:\/\/database.guide\/what-is-sql-server-management-studio\/\"><abbr title=\"SQL Server Management Studio\">SSMS<\/abbr><\/a> toolbar<\/li>\n<li>Type or paste\u00a0a\u00a0<kbd>CREATE VIEW<\/kbd> statement (example below)<\/li>\n<li>Run the script<\/li>\n<\/ol>\n<p>The view will now be created in the <a href=\"https:\/\/database.guide\/what-is-a-database\/\">database<\/a>. You will be able to see it under the Views node in the Object Explorer.<\/p>\n<p>You can now use <kbd>SELECT<\/kbd> statements against the view in future queries.<\/p>\n<p>Below are screenshots of the above steps.<\/p>\n<p><!--more--><\/p>\n<h2>Open a New Query<\/h2>\n<p>Open a new query by clicking\u00a0the <kbd>New Query<\/kbd> button in the <abbr title=\"SQL Server Management Studio\">SSMS<\/abbr> toolbar:<\/p>\n<figure id=\"attachment_299\" aria-describedby=\"caption-attachment-299\" style=\"width: 590px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/database.guide\/wp-content\/uploads\/2016\/05\/sql_server_2014_how_to_use_query_designer_1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-299\" src=\"https:\/\/database.guide\/wp-content\/uploads\/2016\/05\/sql_server_2014_how_to_use_query_designer_1.png\" alt=\"Screenshot of the SSMS with the New Query button highlighted (SQL Server 2014)\" width=\"600\" height=\"402\" srcset=\"https:\/\/database.guide\/wp-content\/uploads\/2016\/05\/sql_server_2014_how_to_use_query_designer_1.png 600w, https:\/\/database.guide\/wp-content\/uploads\/2016\/05\/sql_server_2014_how_to_use_query_designer_1-300x201.png 300w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><\/a><figcaption id=\"caption-attachment-299\" class=\"wp-caption-text\">The SSMS with the New Query button highlighted.<\/figcaption><\/figure>\n<p>&nbsp;<\/p>\n<h2>Type or paste\u00a0a\u00a0CREATE VIEW statement<\/h2>\n<p>This will start with <code>CREATE VIEW (view_name) AS<\/code> followed by the contents of the view.<\/p>\n<p>Here&#8217;s an example:<\/p>\n<pre>CREATE VIEW ToDoList AS\r\nSELECT\tTasks.TaskName, Tasks.Description\r\nFROM\tStatus INNER JOIN\r\n\t\t\tTasks ON Status.StatusId = Tasks.StatusId\r\nWHERE\t(Status.StatusId = 1)\r\n<\/pre>\n<h2>Run the script<\/h2>\n<figure id=\"attachment_307\" aria-describedby=\"caption-attachment-307\" style=\"width: 590px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/database.guide\/wp-content\/uploads\/2016\/05\/sql_server_2014_views_1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-307 size-full\" src=\"https:\/\/database.guide\/wp-content\/uploads\/2016\/05\/sql_server_2014_views_1.png\" alt=\"Screenshot of a SQL view script in SQL Server 2014\" width=\"600\" height=\"495\" srcset=\"https:\/\/database.guide\/wp-content\/uploads\/2016\/05\/sql_server_2014_views_1.png 600w, https:\/\/database.guide\/wp-content\/uploads\/2016\/05\/sql_server_2014_views_1-300x248.png 300w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><\/a><figcaption id=\"caption-attachment-307\" class=\"wp-caption-text\">A SQL view called &#8220;ToDoList&#8221; has just been created, and now appears in the Object Explorer.<\/figcaption><\/figure>\n<h2>Run the View<\/h2>\n<p>You can run a view by selecting from it. Use the <kbd>SELECT<\/kbd> statement in the same way that you&#8217;d use it when selecting the contents of a table.<\/p>\n<figure id=\"attachment_308\" aria-describedby=\"caption-attachment-308\" style=\"width: 423px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/database.guide\/wp-content\/uploads\/2016\/05\/sql_server_2014_views_2.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-308\" src=\"https:\/\/database.guide\/wp-content\/uploads\/2016\/05\/sql_server_2014_views_2.png\" alt=\"Screenshot of running a SQL view in SQL Server 2014\" width=\"433\" height=\"320\" srcset=\"https:\/\/database.guide\/wp-content\/uploads\/2016\/05\/sql_server_2014_views_2.png 433w, https:\/\/database.guide\/wp-content\/uploads\/2016\/05\/sql_server_2014_views_2-300x222.png 300w\" sizes=\"auto, (max-width: 433px) 100vw, 433px\" \/><\/a><figcaption id=\"caption-attachment-308\" class=\"wp-caption-text\">Run a view by using a SELECT statement.<\/figcaption><\/figure>\n<p>So a view is basically a pre-written query that is stored on the database. Instead of\u00a0typing the whole query out every time you want to run it, you can save it in a view. Then all you need to do is run\u00a0a simple <kbd>SELECT<\/kbd> against that view.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>To create a view in SQL Server: Open a new query by clicking\u00a0the New Query button in the SSMS toolbar Type or paste\u00a0a\u00a0CREATE VIEW statement (example below) Run the script The view will now be created in the database. You will be able to see it under the Views node in the Object Explorer. You &#8230; <a title=\"How to Create a View in SQL Server\" class=\"read-more\" href=\"https:\/\/database.guide\/how-to-create-a-view-in-sql-server\/\" aria-label=\"Read more about How to Create a View in SQL Server\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[10,12,24],"class_list":["post-305","post","type-post","status-publish","format-standard","hentry","category-sql-server","tag-how-to","tag-sql-server","tag-views"],"_links":{"self":[{"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts\/305","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/comments?post=305"}],"version-history":[{"count":10,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts\/305\/revisions"}],"predecessor-version":[{"id":370,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts\/305\/revisions\/370"}],"wp:attachment":[{"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/media?parent=305"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/categories?post=305"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/tags?post=305"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}