{"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"},"_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}]}}