{"id":7562,"date":"2022-05-05T06:40:00","date_gmt":"2022-05-05T04:40:00","guid":{"rendered":"https:\/\/expert-only.com\/?p=7562"},"modified":"2022-08-01T16:03:00","modified_gmt":"2022-08-01T14:03:00","slug":"script-create-view-sql-server","status":"publish","type":"post","link":"https:\/\/expert-only.com\/en\/t-sql\/script-create-view-sql-server\/","title":{"rendered":"Script to create a view in SQL Server"},"content":{"rendered":"\n<p>To add a new view in a database, use this script written in T-SQL to create a view in SQL Server. A SQL view allows you to display specific columns of an existing table. Think of a view as a virtual table defined by a SQL query selection. It is a set of columns and rows in a table. SQL Server developers create and manage user-defined objects and therefore views.<\/p>\n\n\n\n<p>Here is an example of a script to create a view in a SQL Server database. Use the SQL Create View statement followed by a Select statement to add a view to a database.<\/p>\n\n\n\n<p>In addition, there are three different types of SQL Server views:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>A partitioned view<\/strong> joins data with a partition from multiple tables on one or more SQL Servers. Classic views are partitioned views because it is the underlying query that joins the data.<\/li><li><strong>The indexed view<\/strong> is a materialized view that must be refreshed like a table.<\/li><li><strong>A system view<\/strong> allows SQL users to check the metadata of the MS SQL database catalogue.<\/li><\/ul>\n\n\n\n<p>On another topic, it can be useful to list all the indexes of a SQL Server database for verification and optimization purposes.<\/p>\n\n\n\n<p>It can be useful to <strong><a href=\"https:\/\/expert-only.com\/en\/t-sql\/list-indexes-in-sql-server-database-with-a-query\/\" target=\"_blank\" rel=\"noreferrer noopener\">list all the indexes of a SQL Server database<\/a><\/strong> for double checking and optimisation purposes. <\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-create-a-sql-server-view-with-a-t-sql-script\">Create a SQL Server view with a T-SQL script<\/h2>\n\n\n\n<p>Firstly, let&#8217;s consider the Sales table created in a previous article to show different data types available and a simple sales structure to be used in T-SQL examples.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"mssql\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">CREATE TABLE [dbo].[SALES] \n(\n\t[Year] INT, \n\t[MonthName] NVARCHAR(50), -- alphanumeric\n\t[MonthCurrent] BIT, -- Boolean, 0 or 1 , false \/ true\n\t[NumberMonth] TINYINT, -- very small integer, from 0 to 255\n\t[EmployeeNumber] SMALLINT, -- small integer, minimum -2^15, maximum 2^15\n\t[NumberOfClients] INTEGER, -- integer, minimum -2^31, maximum 2^31\n\t[NumberOfSales] BIGINT, -- big integer, minimum: -2^63, maximum 2^63\n\t[Amount_ET] NUMERIC(15,5), -- numeric, 15 digits, with 5 after the comma \n\t[Amount_IT] DECIMAL(15,5) -- decimal, 15 digits, with 5 after the comma\n);<\/pre>\n\n\n\n<p>Secondly, with this code, create a view to display part of the table contents. Use the script below and adjust it to the needs of the project.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"mssql\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">CREATE VIEW [dbo].[SalesByMonth] \nAS \nSELECT \n   [MonthName], \n   [NumberMonth], \n   SUM([NumberOfClients])    AS [NumberOfClients], \n   SUM([Amount_ET])          AS [Amount_ET], \n   SUM([Amount_IT])          AS [Amount_IT] \nFROM    [dbo].[Sales] \nGROUP BY \n   [MonthName], \n   [NumberMonth] \nORDER BY   [NumberMonth] ASC;\nGO\n<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-script-to-call-a-t-sql-view-from-ssms\">Script to call a T-SQL view from SSMS<\/h3>\n\n\n\n<p>Then, you can use a Select command like this one, for example to select the second part of the year. That is, the months with a number greater than 6, i.e. from July to December.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"mssql\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">SELECT \t*\nFROM \t[dbo].[SalesByMonth]\nWHERE \t[NumberMonth] > 6 ;\n<\/pre>\n\n\n\n<p>Indeed, this short tutorial on SQL Server views explains how to create a view with a script.<\/p>\n\n\n\n<p>To conclude and learn more about MS SQL views, see the definition of SQL views on the dedicated <a href=\"https:\/\/en.wikipedia.org\/wiki\/View_(SQL)\" target=\"_blank\" rel=\"noreferrer noopener\">Wikipedia<\/a> page. This time it is the Standard SQL view, applicable to any ISO compliant provider.<\/p>\n\n\n\n<p>Finally, below is an article with an example of a <strong><a href=\"https:\/\/expert-only.com\/en\/t-sql\/how-to-create-a-sql-server-function\/\">T-SQL script to create a function<\/a><\/strong>.<\/p>\n\n\n\n<figure class=\"wp-block-embed is-type-wp-embed is-provider-tutoriels-et-exemples-sql-server-et-microsoft-it wp-block-embed-tutoriels-et-exemples-sql-server-et-microsoft-it\"><div class=\"wp-block-embed__wrapper\">\n<blockquote class=\"wp-embedded-content\" data-secret=\"w6d53Lh1Vd\"><a href=\"https:\/\/expert-only.com\/en\/t-sql\/how-to-create-a-sql-server-function\/\">How to create a SQL Server function with a script ?<\/a><\/blockquote><iframe loading=\"lazy\" class=\"wp-embedded-content\" sandbox=\"allow-scripts\" security=\"restricted\" style=\"position: absolute; clip: rect(1px, 1px, 1px, 1px);\" title=\"&#8220;How to create a SQL Server function with a script ?&#8221; &#8212; SQL and IT Tutorials\" src=\"https:\/\/expert-only.com\/en\/t-sql\/how-to-create-a-sql-server-function\/embed\/#?secret=Nyvi6wGmW6#?secret=w6d53Lh1Vd\" data-secret=\"w6d53Lh1Vd\" width=\"500\" height=\"282\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" scrolling=\"no\"><\/iframe>\n<\/div><figcaption><strong>T-SQL create function script example<\/strong><\/figcaption><\/figure>\n","protected":false},"excerpt":{"rendered":"<div class=\"mh-excerpt\"><p>To add a new view in a database, use this script written in T-SQL to create a view in SQL Server. A SQL view allows you to display specific columns of an existing table. Think of a view as <a class=\"mh-excerpt-more\" href=\"https:\/\/expert-only.com\/en\/t-sql\/script-create-view-sql-server\/\" title=\"Script to create a view in SQL Server\">&#8230;<\/a><\/p>\n<\/div>","protected":false},"author":1,"featured_media":6196,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[454],"tags":[],"class_list":{"0":"post-7562","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-t-sql"},"_links":{"self":[{"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/posts\/7562","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/comments?post=7562"}],"version-history":[{"count":0,"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/posts\/7562\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/media\/6196"}],"wp:attachment":[{"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/media?parent=7562"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/categories?post=7562"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/tags?post=7562"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}