{"id":87673,"date":"2020-07-27T17:00:37","date_gmt":"2020-07-27T17:00:37","guid":{"rendered":"https:\/\/www.red-gate.com\/simple-talk\/?p=87673"},"modified":"2020-07-24T14:18:23","modified_gmt":"2020-07-24T14:18:23","slug":"extracting-dacpac-when-sql-server-refuses","status":"publish","type":"post","link":"https:\/\/www.red-gate.com\/simple-talk\/blogs\/extracting-dacpac-when-sql-server-refuses\/","title":{"rendered":"Extracting DACPAC even When SQL Server refuses to"},"content":{"rendered":"<p>There are many situations when you need to extract a DACPAC from an existing database in a SQL Server. These situations are usually related to devOps, we extract the DACPAC to make schema comparisons, generate scripts for schema deployment or deploy directly the DACPAC. It can be used in many different ways during a devOps pipeline.<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-87674\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2020\/07\/dacext1.png\" alt=\"SSMS Menu\" width=\"760\" height=\"518\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>The problem starts when you try to extract a DACPAC using SSMS and it fails. The SSMS extraction makes a validation of the database and if you have any broken reference inside the database &#8211; and this happens a lot &#8211; SSMS blocks the DACPAC creation.<\/p>\n<p>In order to test this, you can create a broken reference inside a test database using the following script:<\/p>\n<div style=\"padding-left: 30px;\"><span style=\"font-family: Courier New; font-size: 10pt;\"> <span style=\"color: blue;\">CREATE<\/span>\u00a0<span style=\"color: blue;\">TABLE<\/span>\u00a0<span style=\"color: maroon;\">nonexistingtable<\/span> <br \/>\n\u00a0\u00a0<span style=\"color: maroon;\">(<\/span> <br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0<span style=\"color: maroon;\">id<\/span>\u00a0\u00a0\u00a0\u00a0<span style=\"color: black; font-style: italic;\">INT<\/span>\u00a0<span style=\"color: blue;\">IDENTITY<\/span><span style=\"color: maroon;\">(<\/span><span style=\"color: black;\">1<\/span><span style=\"color: silver;\">,<\/span>\u00a0<span style=\"color: black;\">1<\/span><span style=\"color: maroon;\">)<\/span><span style=\"color: silver;\">,<\/span> <br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0<span style=\"color: maroon;\">value<\/span>\u00a0<span style=\"color: black; font-style: italic;\">INT<\/span> <br \/>\n\u00a0\u00a0<span style=\"color: maroon;\">)<\/span> <\/p>\n<p><span style=\"color: maroon;\">go<\/span> <\/p>\n<p><span style=\"color: blue;\">CREATE<\/span>\u00a0<span style=\"color: blue;\">VIEW<\/span>\u00a0<span style=\"color: maroon;\">badview<\/span> <br \/>\n<span style=\"color: blue;\">AS<\/span> <br \/>\n\u00a0\u00a0<span style=\"color: blue;\">SELECT<\/span>\u00a0<span style=\"color: maroon;\">id<\/span><span style=\"color: silver;\">,<\/span> <br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span style=\"color: maroon;\">value<\/span> <br \/>\n\u00a0\u00a0<span style=\"color: blue;\">FROM<\/span>\u00a0\u00a0\u00a0<span style=\"color: maroon;\">nonexistingtable<\/span> <\/p>\n<p><span style=\"color: maroon;\">go<\/span> <\/p>\n<p><span style=\"color: blue;\">DROP<\/span>\u00a0<span style=\"color: blue;\">TABLE<\/span>\u00a0<span style=\"color: maroon;\">nonexistingtable<\/span>\u00a0 <\/span><\/div>\n<p style=\"padding-left: 30px;\">\u00a0<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-87678\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2020\/07\/dacext5.png\" alt=\"SSMS Error\" width=\"928\" height=\"638\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>Let&#8217;s analyze some possible solutions for this problem. We need to generate the DACPAC file even with validation errors on some objects.<\/p>\n<p>&nbsp;<\/p>\n<h3><strong>Visual Studio<\/strong><\/h3>\n<p style=\"padding-left: 30px;\">We can use the <strong>SQL Server Object Explorer<\/strong> window inside Visual Studio to make the extraction. When we use this window, we have the option to either validate or not the database. Since you know the database has broken links, you ask to not validate and you will have your DACPAC.<\/p>\n<p style=\"padding-left: 30px;\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-87679\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2020\/07\/dacext6.png\" alt=\"VS Menu\" width=\"570\" height=\"316\" \/><\/p>\n<p>\n<img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-87680\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2020\/07\/dacext7.png\" alt=\"VS Option\" width=\"242\" height=\"160\" \/><\/p>\n<p>&nbsp;<\/p>\n<h3><strong>Azure Data Studio<\/strong><\/h3>\n<p style=\"padding-left: 30px;\">Azure Data Studio has an extension called <strong>SQL Server DACPAC<\/strong> which brings to us the Data-Tier Application Wizard to make operations with DACPAC and BACPAC files.<\/p>\n<p style=\"padding-left: 30px;\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-87675\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2020\/07\/dacext2.png\" alt=\"dacpac extension\" width=\"1266\" height=\"410\" \/><\/p>\n<p style=\"padding-left: 30px;\">There is no option to control the validation on the wizard, but the extraction doesn&#8217;t make the validation at all. While SSMS refuses the extraction, it works well on Azure Data Studio.<\/p>\n<p style=\"padding-left: 30px;\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-87676\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2020\/07\/dacext3.png\" alt=\"dacpac wizard\" width=\"778\" height=\"328\" \/><\/p>\n<p style=\"padding-left: 30px;\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-87677\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2020\/07\/dacext4.png\" alt=\"dacpac result\" width=\"946\" height=\"154\" \/><\/p>\n<p>&nbsp;<\/p>\n<h3><strong>Command Line<\/strong><\/h3>\n<p style=\"padding-left: 30px;\">You can use the command line. <strong>SQLPackage.exe<\/strong> can make a DACPAC extraction from the command line and it has options to avoid the database validation.<\/p>\n<p style=\"padding-left: 30px;\">The SQLPackage.exe can be found inside SQL Server folders or Visual Studio folders. In my machine, for example, it was located at\u00a0<em>F:\\program files (X86)\\Microsoft Visual Studio\\2019\\enterprise\\Common7\\ide\\extensions\\microsoft\\sqldb\\dac\\130<\/em><\/p>\n<p style=\"padding-left: 30px;\">The command line would be like this:<\/p>\n<p style=\"padding-left: 60px;\"><em>.\\sqlpackage \/action:Extract \/TargetFile:&#8221;c:\\Bancos\\adv.dacpac&#8221; \/SourceConnectionString:&#8221;Server=.;Integrated Security=SSPI;Database=AdventureWorks&#8221;<\/em><\/p>\n<p style=\"padding-left: 30px;\">We don&#8217;t need to include any option to disable the validation because the default behaviour is extracting without it.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-87681\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2020\/07\/dacext8.png\" alt=\"sqlpackage\" width=\"1170\" height=\"196\" \/><\/p>\n<h3>\n<strong>Conclusion<\/strong><\/h3>\n<p style=\"padding-left: 30px;\">\nIn the end, SSMS is one of the few tools without the ability to export DACPACs without validation. It&#8217;s sad Microsoft is leaving behind a tool which followed DBA&#8217;s happy and sad moments since 2005<\/p>\n","protected":false},"excerpt":{"rendered":"<p>There are many situations when you need to extract a DACPAC from an existing database in a SQL Server. These situations are usually related to devOps, we extract the DACPAC to make schema comparisons, generate scripts for schema deployment or deploy directly the DACPAC. It can be used in many different ways during a devOps&#8230;&hellip;<\/p>\n","protected":false},"author":50808,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[2],"tags":[],"coauthors":[6810],"class_list":["post-87673","post","type-post","status-publish","format-standard","hentry","category-blogs"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/87673","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/users\/50808"}],"replies":[{"embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/comments?post=87673"}],"version-history":[{"count":3,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/87673\/revisions"}],"predecessor-version":[{"id":87690,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/87673\/revisions\/87690"}],"wp:attachment":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/media?parent=87673"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/categories?post=87673"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/tags?post=87673"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/coauthors?post=87673"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}