{"id":8930,"date":"2022-07-19T06:37:00","date_gmt":"2022-07-19T04:37:00","guid":{"rendered":"https:\/\/expert-only.com\/?p=8930"},"modified":"2022-07-25T18:08:02","modified_gmt":"2022-07-25T16:08:02","slug":"alter-view-with-sql-server","status":"publish","type":"post","link":"https:\/\/expert-only.com\/en\/t-sql\/alter-view-with-sql-server\/","title":{"rendered":"Alter View with SQL Server"},"content":{"rendered":"\n<p>How to alter a SQL Server view with a T-SQL script? This code example allows you to modify an existing view. All you must do is use the common ALTER VIEW command. For example, to delete a column from the existing view or to add a column calculated from other columns in the source table.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-modify-a-sql-server-view-with-an-alter-view-script\">Modify a SQL Server view with an Alter View script<\/h2>\n\n\n\n<p>Indeed, this example of a T-SQL script allows you to modify an existing view in a Microsoft database.<\/p>\n\n\n\n<p>For example, to remove the Number of customers column, named NumberCustomers. So that it no longer appears in the list of columns in the new view.<\/p>\n\n\n\n<p>The SQL Server V_Sales view therefore contains sales data. And the source code of the initial view is available in this article which explains how <strong><a href=\"https:\/\/expert-only.com\/en\/t-sql\/create-a-view-with-sql-server\/\">to create a SQL Server view<\/a><\/strong>. The source code of the view must be executed first to execute the following code without any error.<\/p>\n\n\n\n<p>Execute the following code to alter the view as follows:<\/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=\"\">ALTER VIEW [dbo].[SalesByMonth] \nAS \nSELECT \n\t[MonthName], \n\t[NumberMonth],        \n\tSUM([NumberOfClients])   AS [NumberOfClients],      \n\tSUM([Amount_ET])      AS [Amount_ET],     \n\tSUM([Amount_IT])         AS [Amount_IT]\nFROM [dbo].[Sales]\nGROUP BY\n\t[MonthName], \n\t[NumberMonth]\nORDER BY\n\tSUM([NumberOfClients]) DESC;\n<\/pre>\n\n\n\n<p>In fact, the <a href=\"https:\/\/docs.microsoft.com\/en-us\/sql\/relational-databases\/system-information-schema-views\/columns-transact-sql?view=sql-server-ver16\" target=\"_blank\" rel=\"noreferrer noopener\">columns<\/a> are read from the source table. And a view makes it possible to select only the useful data.<\/p>\n\n\n\n<p>Horizontally (with the selected columns) and vertically (with the filter on the data with the SELECT command).<\/p>\n\n\n\n<p>Note that there is a very simple tip to make sure the select in the alter view script works perfectly. The goal is to develop the code view in two steps. <\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>First is to test it as a simple selection till all columns are available and the select query is finished according to the project requirement.<\/li><li>Then integrate the select statement to the view script smoothly. <\/li><\/ul>\n\n\n\n<p>In conclusion, this simple T-SQL example allows you to change the list of columns used in the view.<\/p>\n\n\n\n<p>If necessary, here is a script to perform <strong><a href=\"https:\/\/expert-only.com\/en\/t-sql\/insert-or-update-with-sql-server\/\">a SQL Server insert or update also called a SQL Upsert<\/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=\"Exs9uLfsH7\"><a href=\"https:\/\/expert-only.com\/en\/t-sql\/insert-or-update-with-sql-server\/\">Insert or Update with SQL Server (Upsert)<\/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;Insert or Update with SQL Server (Upsert)&#8221; &#8212; SQL and IT Tutorials\" src=\"https:\/\/expert-only.com\/en\/t-sql\/insert-or-update-with-sql-server\/embed\/#?secret=b8eUjRQJnX#?secret=Exs9uLfsH7\" data-secret=\"Exs9uLfsH7\" width=\"500\" height=\"282\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" scrolling=\"no\"><\/iframe>\n<\/div><\/figure>\n","protected":false},"excerpt":{"rendered":"<div class=\"mh-excerpt\"><p>How to alter a SQL Server view with a T-SQL script? This code example allows you to modify an existing view. All you must do is use the common ALTER VIEW command. For example, to delete a column from <a class=\"mh-excerpt-more\" href=\"https:\/\/expert-only.com\/en\/t-sql\/alter-view-with-sql-server\/\" title=\"Alter View with SQL Server\">&#8230;<\/a><\/p>\n<\/div>","protected":false},"author":1,"featured_media":6145,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[454],"tags":[],"class_list":{"0":"post-8930","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-t-sql"},"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v21.7 (Yoast SEO v26.2) - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Alter View with SQL Server - T-SQL<\/title>\n<meta name=\"description\" content=\"To alter a view with SQL Server, use a script to update the code. It is easy to copy, paste, and adapt the column names and the order.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/expert-only.com\/en\/t-sql\/alter-view-with-sql-server\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Alter View with SQL Server\" \/>\n<meta property=\"og:description\" content=\"To alter a view with SQL Server, use a script to update the code. It is easy to copy, paste, and adapt the column names and the order.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/expert-only.com\/en\/t-sql\/alter-view-with-sql-server\/\" \/>\n<meta property=\"og:site_name\" content=\"SQL and IT Tutorials\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/ExpertOnlyCom\/\" \/>\n<meta property=\"article:published_time\" content=\"2022-07-19T04:37:00+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2022-07-25T16:08:02+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/expert-only.com\/wp-content\/uploads\/2018\/06\/chairs-2181960_1920.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1920\" \/>\n\t<meta property=\"og:image:height\" content=\"1080\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Expert-Only\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@expert_only\" \/>\n<meta name=\"twitter:site\" content=\"@expert_only\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Expert-Only\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/expert-only.com\/en\/t-sql\/alter-view-with-sql-server\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/expert-only.com\/en\/t-sql\/alter-view-with-sql-server\/\"},\"author\":{\"name\":\"Expert-Only\",\"@id\":\"https:\/\/expert-only.com\/en\/#\/schema\/person\/406a9576b52944f018739a42046873ef\"},\"headline\":\"Alter View with SQL Server\",\"datePublished\":\"2022-07-19T04:37:00+00:00\",\"dateModified\":\"2022-07-25T16:08:02+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/expert-only.com\/en\/t-sql\/alter-view-with-sql-server\/\"},\"wordCount\":322,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/expert-only.com\/en\/#organization\"},\"image\":{\"@id\":\"https:\/\/expert-only.com\/en\/t-sql\/alter-view-with-sql-server\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/expert-only.com\/wp-content\/uploads\/2018\/06\/chairs-2181960_1920.jpg\",\"articleSection\":[\"T-SQL\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/expert-only.com\/en\/t-sql\/alter-view-with-sql-server\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/expert-only.com\/en\/t-sql\/alter-view-with-sql-server\/\",\"url\":\"https:\/\/expert-only.com\/en\/t-sql\/alter-view-with-sql-server\/\",\"name\":\"Alter View with SQL Server - T-SQL\",\"isPartOf\":{\"@id\":\"https:\/\/expert-only.com\/en\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/expert-only.com\/en\/t-sql\/alter-view-with-sql-server\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/expert-only.com\/en\/t-sql\/alter-view-with-sql-server\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/expert-only.com\/wp-content\/uploads\/2018\/06\/chairs-2181960_1920.jpg\",\"datePublished\":\"2022-07-19T04:37:00+00:00\",\"dateModified\":\"2022-07-25T16:08:02+00:00\",\"description\":\"To alter a view with SQL Server, use a script to update the code. It is easy to copy, paste, and adapt the column names and the order.\",\"breadcrumb\":{\"@id\":\"https:\/\/expert-only.com\/en\/t-sql\/alter-view-with-sql-server\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/expert-only.com\/en\/t-sql\/alter-view-with-sql-server\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/expert-only.com\/en\/t-sql\/alter-view-with-sql-server\/#primaryimage\",\"url\":\"https:\/\/expert-only.com\/wp-content\/uploads\/2018\/06\/chairs-2181960_1920.jpg\",\"contentUrl\":\"https:\/\/expert-only.com\/wp-content\/uploads\/2018\/06\/chairs-2181960_1920.jpg\",\"width\":1920,\"height\":1080},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/expert-only.com\/en\/t-sql\/alter-view-with-sql-server\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"en\",\"item\":\"https:\/\/expert-only.com\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Alter View with SQL Server\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/expert-only.com\/en\/#website\",\"url\":\"https:\/\/expert-only.com\/en\/\",\"name\":\"SQL and IT Tutorials\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/expert-only.com\/en\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/expert-only.com\/en\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/expert-only.com\/en\/#organization\",\"name\":\"Expert-Only\",\"url\":\"https:\/\/expert-only.com\/en\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/expert-only.com\/en\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/expert-only.com\/wp-content\/uploads\/2017\/09\/cropped-logo_Expert-Only.jpg\",\"contentUrl\":\"https:\/\/expert-only.com\/wp-content\/uploads\/2017\/09\/cropped-logo_Expert-Only.jpg\",\"width\":381,\"height\":174,\"caption\":\"Expert-Only\"},\"image\":{\"@id\":\"https:\/\/expert-only.com\/en\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/ExpertOnlyCom\/\",\"https:\/\/x.com\/expert_only\",\"https:\/\/www.youtube.com\/channel\/UCMS5sR_FwAetB0FmciNvUaA\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/expert-only.com\/en\/#\/schema\/person\/406a9576b52944f018739a42046873ef\",\"name\":\"Expert-Only\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/expert-only.com\/en\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/084b15660763ff5b13bb60b2f52f97bb?s=96&d=identicon&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/084b15660763ff5b13bb60b2f52f97bb?s=96&d=identicon&r=g\",\"caption\":\"Expert-Only\"}}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Alter View with SQL Server - T-SQL","description":"To alter a view with SQL Server, use a script to update the code. It is easy to copy, paste, and adapt the column names and the order.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/expert-only.com\/en\/t-sql\/alter-view-with-sql-server\/","og_locale":"en_US","og_type":"article","og_title":"Alter View with SQL Server","og_description":"To alter a view with SQL Server, use a script to update the code. It is easy to copy, paste, and adapt the column names and the order.","og_url":"https:\/\/expert-only.com\/en\/t-sql\/alter-view-with-sql-server\/","og_site_name":"SQL and IT Tutorials","article_publisher":"https:\/\/www.facebook.com\/ExpertOnlyCom\/","article_published_time":"2022-07-19T04:37:00+00:00","article_modified_time":"2022-07-25T16:08:02+00:00","og_image":[{"width":1920,"height":1080,"url":"https:\/\/expert-only.com\/wp-content\/uploads\/2018\/06\/chairs-2181960_1920.jpg","type":"image\/jpeg"}],"author":"Expert-Only","twitter_card":"summary_large_image","twitter_creator":"@expert_only","twitter_site":"@expert_only","twitter_misc":{"Written by":"Expert-Only","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/expert-only.com\/en\/t-sql\/alter-view-with-sql-server\/#article","isPartOf":{"@id":"https:\/\/expert-only.com\/en\/t-sql\/alter-view-with-sql-server\/"},"author":{"name":"Expert-Only","@id":"https:\/\/expert-only.com\/en\/#\/schema\/person\/406a9576b52944f018739a42046873ef"},"headline":"Alter View with SQL Server","datePublished":"2022-07-19T04:37:00+00:00","dateModified":"2022-07-25T16:08:02+00:00","mainEntityOfPage":{"@id":"https:\/\/expert-only.com\/en\/t-sql\/alter-view-with-sql-server\/"},"wordCount":322,"commentCount":0,"publisher":{"@id":"https:\/\/expert-only.com\/en\/#organization"},"image":{"@id":"https:\/\/expert-only.com\/en\/t-sql\/alter-view-with-sql-server\/#primaryimage"},"thumbnailUrl":"https:\/\/expert-only.com\/wp-content\/uploads\/2018\/06\/chairs-2181960_1920.jpg","articleSection":["T-SQL"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/expert-only.com\/en\/t-sql\/alter-view-with-sql-server\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/expert-only.com\/en\/t-sql\/alter-view-with-sql-server\/","url":"https:\/\/expert-only.com\/en\/t-sql\/alter-view-with-sql-server\/","name":"Alter View with SQL Server - T-SQL","isPartOf":{"@id":"https:\/\/expert-only.com\/en\/#website"},"primaryImageOfPage":{"@id":"https:\/\/expert-only.com\/en\/t-sql\/alter-view-with-sql-server\/#primaryimage"},"image":{"@id":"https:\/\/expert-only.com\/en\/t-sql\/alter-view-with-sql-server\/#primaryimage"},"thumbnailUrl":"https:\/\/expert-only.com\/wp-content\/uploads\/2018\/06\/chairs-2181960_1920.jpg","datePublished":"2022-07-19T04:37:00+00:00","dateModified":"2022-07-25T16:08:02+00:00","description":"To alter a view with SQL Server, use a script to update the code. It is easy to copy, paste, and adapt the column names and the order.","breadcrumb":{"@id":"https:\/\/expert-only.com\/en\/t-sql\/alter-view-with-sql-server\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/expert-only.com\/en\/t-sql\/alter-view-with-sql-server\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/expert-only.com\/en\/t-sql\/alter-view-with-sql-server\/#primaryimage","url":"https:\/\/expert-only.com\/wp-content\/uploads\/2018\/06\/chairs-2181960_1920.jpg","contentUrl":"https:\/\/expert-only.com\/wp-content\/uploads\/2018\/06\/chairs-2181960_1920.jpg","width":1920,"height":1080},{"@type":"BreadcrumbList","@id":"https:\/\/expert-only.com\/en\/t-sql\/alter-view-with-sql-server\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"en","item":"https:\/\/expert-only.com\/en\/"},{"@type":"ListItem","position":2,"name":"Alter View with SQL Server"}]},{"@type":"WebSite","@id":"https:\/\/expert-only.com\/en\/#website","url":"https:\/\/expert-only.com\/en\/","name":"SQL and IT Tutorials","description":"","publisher":{"@id":"https:\/\/expert-only.com\/en\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/expert-only.com\/en\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/expert-only.com\/en\/#organization","name":"Expert-Only","url":"https:\/\/expert-only.com\/en\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/expert-only.com\/en\/#\/schema\/logo\/image\/","url":"https:\/\/expert-only.com\/wp-content\/uploads\/2017\/09\/cropped-logo_Expert-Only.jpg","contentUrl":"https:\/\/expert-only.com\/wp-content\/uploads\/2017\/09\/cropped-logo_Expert-Only.jpg","width":381,"height":174,"caption":"Expert-Only"},"image":{"@id":"https:\/\/expert-only.com\/en\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/ExpertOnlyCom\/","https:\/\/x.com\/expert_only","https:\/\/www.youtube.com\/channel\/UCMS5sR_FwAetB0FmciNvUaA"]},{"@type":"Person","@id":"https:\/\/expert-only.com\/en\/#\/schema\/person\/406a9576b52944f018739a42046873ef","name":"Expert-Only","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/expert-only.com\/en\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/084b15660763ff5b13bb60b2f52f97bb?s=96&d=identicon&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/084b15660763ff5b13bb60b2f52f97bb?s=96&d=identicon&r=g","caption":"Expert-Only"}}]}},"_links":{"self":[{"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/posts\/8930","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=8930"}],"version-history":[{"count":0,"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/posts\/8930\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/media\/6145"}],"wp:attachment":[{"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/media?parent=8930"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/categories?post=8930"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/tags?post=8930"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}