{"id":18393,"date":"2022-11-07T06:25:00","date_gmt":"2022-11-07T05:25:00","guid":{"rendered":"https:\/\/expert-only.com\/?p=18393"},"modified":"2022-11-22T10:23:28","modified_gmt":"2022-11-22T09:23:28","slug":"test-and-drop-a-sql-server-view-if-exists","status":"publish","type":"post","link":"https:\/\/expert-only.com\/en\/t-sql\/test-and-drop-a-sql-server-view-if-exists\/","title":{"rendered":"Test and drop a SQL Server view if it exists in the database"},"content":{"rendered":"\n<h4 class=\"has-text-align-center wp-block-heading\" id=\"h-how-to-test-and-then-drop-a-sql-server-view-if-it-exists-in-the-database\"><strong><em>How to test and then drop a SQL Server view, if it exists in the database?<\/em><\/strong><\/h4>\n\n\n\n<p>Here is how to test the existence and drop a SQL Server view with a T-SQL script. Use this script as an example to test the existence of the view before deleting it. The goal is to not generate an error during the execution of a deployment script for example. And thus, not to execute a script on a object not in the database, or renamed for example.<\/p>\n\n\n\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_84 ez-toc-wrap-center counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/expert-only.com\/en\/t-sql\/test-and-drop-a-sql-server-view-if-exists\/#1-drop-a-sql-server-view-if-it-exists-with-all-versions\" >1. Drop a SQL Server view if it exists with all versions<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/expert-only.com\/en\/t-sql\/test-and-drop-a-sql-server-view-if-exists\/#2-drop-a-view-with-the-ms-sql-version-2016-and-older\" >2. Drop a view with the MS SQL version 2016 and older<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/expert-only.com\/en\/t-sql\/test-and-drop-a-sql-server-view-if-exists\/#3-drop-all-the-views-from-a-ms-sql-database\" >3. Drop all the views from a MS SQL database<\/a><\/li><\/ul><\/nav><\/div>\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"1-drop-a-sql-server-view-if-it-exists-with-all-versions\"><\/span>1. Drop a SQL Server view if it exists with all versions<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Indeed, deleting a non-existent view returns this error in SQL Server Management Studio (SSMS).<\/p>\n\n\n\n<p><em>Cannot drop the view &#8216;dbo.V_SALES&#8217;, because it does not exist or you do not have permission.<\/em><\/p>\n\n\n\n<p>Or the same error message in French:<\/p>\n\n\n\n<p><em>Msg 3701: The view does not exist or you do not have permission.<\/em><\/p>\n\n\n\n<p>Here is the solution, use the SQL Server IF EXISTS function to test the existence of the view before deleting it, as in this example.<\/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=\"\">IF EXISTS ( \n  SELECT 1 \n  FROM sys.objects \n  WHERE object_id = object_id(N'[dbo].[V_SALES]') \n  AND type in (N'V') \n) \nBEGIN DROP VIEW [dbo].[V_SALES]; \nEND;<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"2-drop-a-view-with-the-ms-sql-version-2016-and-older\"><\/span>2. Drop a view with the MS SQL version 2016 and older<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Shorter script with only one line of code, the DROP VIEW IF EXISTS function, which is available since the SQL Server 2016 version.<\/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=\"\">IF EXISTS DROP VIEW [dbo];[V_SALES];\n<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"3-drop-all-the-views-from-a-ms-sql-database\"><\/span>3. Drop all the views from a MS SQL database<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Finally, for this third part on deleting views, here is how to create a dynamic script with a loop to filter and delete all views with a particular prefix or suffix. This multi-part code automates these steps:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Create the variables<\/li>\n\n\n\n<li>Instantiate the variables<\/li>\n\n\n\n<li>Create a slider to select the name of the views with a filter on the name<\/li>\n\n\n\n<li>Browse the slider<\/li>\n\n\n\n<li>For each row in the cursor, build and display a query to delete the view<\/li>\n\n\n\n<li>Close and delete the cursor reference.<\/li>\n<\/ol>\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=\"\">DECLARE\n  @View_Name AS NVARCHAR(50),\n  @Drop_SQL_Query AS NVARCHAR(100),\n  @Drop_SQL_Query_Full AS NVARCHAR(200);\n\nSET @Drop_SQL_Query = 'IF EXISTS DROP VIEW ';\n\nDECLARE View_Name_Cursor CURSOR   FOR \n  SELECT [Name] \n  FROM SYS.VIEWS\n  WHERE [Name] LIKE '%_SALES_%'\n  ORDER BY [Name];\n\nOPEN View_Name_Cursor;\nFETCH NEXT FROM View_Name_Cursor into @View_Name;\n\nWHILE @@FETCH_STATUS = 0  \nBEGIN \n  SET @Drop_SQL_Query_Full = @Drop_SQL_Query + @View_Name;\n  PRINT @Drop_SQL_Query_Full;\n  --EXEC @Drop_SQL_Query_Full;\n  FETCH NEXT FROM View_Name_Cursor into @View_Name;\nEND\n\nCLOSE View_Name_Cursor;  \nDEALLOCATE View_Name_Cursor; \n<\/pre>\n\n\n\n<p>Indeed, this script allows you to drop all the views created in a database. It uses a filter, which can be easily modified to select views based on a suffix, a prefix, a filter as in the example above, or simply all views in the database.<\/p>\n\n\n\n<p>This script does not actually drop the MS SQL views, but dynamically generates the commands to drop the views. To run it directly inside the T-SQL code, simply remove the comments on the following line.<\/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=\"\">EXEC sp_executesql @Drop_SQL_Query_Full;\n<\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"720\" height=\"740\" src=\"https:\/\/expert-only.com\/wp-content\/uploads\/2022\/11\/sq-server-generate-script-to-drop-all-views-from-db-1.jpg\" alt=\"Test and Drop a SQL Server view if it exists - drop all the view in a SQL database\" class=\"wp-image-18383\" srcset=\"https:\/\/expert-only.com\/wp-content\/uploads\/2022\/11\/sq-server-generate-script-to-drop-all-views-from-db-1.jpg 720w, https:\/\/expert-only.com\/wp-content\/uploads\/2022\/11\/sq-server-generate-script-to-drop-all-views-from-db-1-292x300.jpg 292w\" sizes=\"auto, (max-width: 720px) 100vw, 720px\" \/><figcaption class=\"wp-element-caption\">Drop all SQL Server views from a database with a script<\/figcaption><\/figure>\n\n\n\n<p>These are Data Definition Language (<a href=\"https:\/\/cloud.google.com\/bigquery\/docs\/reference\/standard-sql\/data-definition-language\" target=\"_blank\" rel=\"noreferrer noopener\">DDL<\/a>) queries. This more general page on views shows some examples of the <a href=\"https:\/\/expert-only.com\/en\/manage-sql-server-views-with-t-sql-code\/\">main DDL commands for managing views with T-SQL scripts<\/a>. To conclude, this tutorial explains how to test and drop a SQL Server view with versions before SQL Server 2016, and after in one line. Then presents a dynamic script to delete all views in a SQL database.<\/p>\n\n\n\n<figure class=\"wp-block-embed is-type-wp-embed is-provider-sql-and-it-tutorials wp-block-embed-sql-and-it-tutorials\"><div class=\"wp-block-embed__wrapper\">\n<blockquote class=\"wp-embedded-content\" data-secret=\"eUM1xdYMPb\"><a href=\"https:\/\/expert-only.com\/en\/manage-sql-server-views-with-t-sql-code\/\">Manage SQL Server views with T-SQL code<\/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;Manage SQL Server views with T-SQL code&#8221; &#8212; SQL and IT Tutorials\" src=\"https:\/\/expert-only.com\/en\/manage-sql-server-views-with-t-sql-code\/embed\/#?secret=WpS9a1FcL1#?secret=eUM1xdYMPb\" data-secret=\"eUM1xdYMPb\" 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 test and then drop a SQL Server view, if it exists in the database? Here is how to test the existence and drop a SQL Server view with a T-SQL script. Use this script as an example <a class=\"mh-excerpt-more\" href=\"https:\/\/expert-only.com\/en\/t-sql\/test-and-drop-a-sql-server-view-if-exists\/\" title=\"Test and drop a SQL Server view if it exists in the database\">&#8230;<\/a><\/p>\n<\/div>","protected":false},"author":1,"featured_media":6274,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[454],"tags":[],"class_list":{"0":"post-18393","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\/18393","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=18393"}],"version-history":[{"count":0,"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/posts\/18393\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/media\/6274"}],"wp:attachment":[{"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/media?parent=18393"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/categories?post=18393"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/tags?post=18393"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}