{"id":955,"date":"2013-03-13T00:57:03","date_gmt":"2013-03-12T13:57:03","guid":{"rendered":"http:\/\/ntsblog.homedev.com.au\/?p=955"},"modified":"2013-03-14T00:51:38","modified_gmt":"2013-03-13T13:51:38","slug":"sqlcmd-variables-linked-server-connection-intellisense","status":"publish","type":"post","link":"https:\/\/ntsblog.homedev.com.au\/index.php\/2013\/03\/13\/sqlcmd-variables-linked-server-connection-intellisense\/","title":{"rendered":"How to use SQLCMD variables for a Linked Server Connection with Intellisense"},"content":{"rendered":"<div id=\"ntsbl-2538419520\" class=\"ntsbl-before-content ntsbl-entity-placement\"><script async src=\"\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js?client=ca-pub-6288941070289539\" crossorigin=\"anonymous\"><\/script><ins class=\"adsbygoogle\" style=\"display:inline-block;width:728px;height:90px;\" \ndata-ad-client=\"ca-pub-6288941070289539\" \ndata-ad-slot=\"9356781486\"><\/ins> \n<script> \n(adsbygoogle = window.adsbygoogle || []).push({}); \n<\/script>\n<\/div><h2>The Problem<\/h2>\n<p>For a long time I have struggled with how to correctly setup a SQL Server Database project. The main sticking point is to try to script views that use linked server connections.<\/p>\n<p>Generally when you create a view that references a linked server connection you will get the following error:<\/p>\n<p>[sql]<br \/>\nSQL71561: View: [dbo].[vwTest] has an unresolved reference to object [LinkedServer].[RemoteDB].[dbo].[tblTest].<br \/>\n[\/sql]<\/p>\n<h2>The End Game<\/h2>\n<p>I have been able to achieve the holy grail in Visual Studio database projects;<\/p>\n<ol>\n<li>A solution that compiles even with views that run across linked server connections.<\/li>\n<li>The solution has Intellisense working for the linked server database.<\/li>\n<li>The ability to adjust the details of the linked servers when publishing the database via the SQLCMD Variables.<\/li>\n<\/ol>\n<h2>The Solution<\/h2>\n<p>The following instructions are performed on Visual Studio 2012 \/ SQL Server 2012 and SQL Server Data Tools (SSDT)<\/p>\n<h4>Step 1 &#8211; Create a database project for your linked server, (RemoteDB)<\/h4>\n<p>You need a .dacpac file of your target database.<br \/>\nThe easiest way to do this is:<\/p>\n<ol>\n<li>Create a new SQL Server Project<\/li>\n<li>Right Click on the database project and choose Import &#8211;> Database<\/li>\n<li>Create a connection to your remote\/linkedserver target database.<\/li>\n<li>Click finish and visual studio will create a scripted copy of your database.<\/li>\n<li>Build the project and fix any compile issues<\/li>\n<\/ol>\n<p>In my case I was trying to reference an Enterprise level database with a few thousand tables, but rather than generate the entire database I just created the 20 &#8211; 30 tables that are referenced most often and used that as my DB project. Over time as I need more and more tables I will just script and import a few tables at a time, via the Import &#8211;> Script option.<\/p>\n<p>Once successfully built the .dacpac file will be found in the &#8220;bin\\Debug&#8221; directory<\/p>\n<p>Note: I also generally write a post build event and copy the .dacpac file to a central higher level folder to allow any project to reference the .dacpac file from a central location.<\/p>\n<h4>Step 2 &#8211; Add a database reference<\/h4>\n<p>In the main database project add a database reference as follows:<\/p>\n<p><a href=\"https:\/\/ntsblog.homedev.com.au\/wp-content\/uploads\/2013\/03\/DBReference.png\"><img loading=\"lazy\" decoding=\"async\" data-attachment-id=\"957\" data-permalink=\"https:\/\/ntsblog.homedev.com.au\/index.php\/2013\/03\/13\/sqlcmd-variables-linked-server-connection-intellisense\/dbreference\/\" data-orig-file=\"https:\/\/ntsblog.homedev.com.au\/wp-content\/uploads\/2013\/03\/DBReference.png\" data-orig-size=\"583,407\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;}\" data-image-title=\"DBReference\" data-image-description=\"\" data-image-caption=\"\" data-medium-file=\"https:\/\/ntsblog.homedev.com.au\/wp-content\/uploads\/2013\/03\/DBReference-300x209.png\" data-large-file=\"https:\/\/ntsblog.homedev.com.au\/wp-content\/uploads\/2013\/03\/DBReference.png\" src=\"https:\/\/ntsblog.homedev.com.au\/wp-content\/uploads\/2013\/03\/DBReference.png\" alt=\"DBReference\" width=\"583\" height=\"407\" class=\"aligncenter size-full wp-image-957\" srcset=\"https:\/\/ntsblog.homedev.com.au\/wp-content\/uploads\/2013\/03\/DBReference.png 583w, https:\/\/ntsblog.homedev.com.au\/wp-content\/uploads\/2013\/03\/DBReference-300x209.png 300w\" sizes=\"auto, (max-width: 583px) 100vw, 583px\" \/><\/a><br \/>\nSome points about these settings:<\/p>\n<ol>\n<li>Choose your .dacpac file that you generated in step 1.<\/li>\n<li>Database Location &#8211; even though the Database is on a &#8220;Different Database, Different server&#8221;, you need to set it as &#8220;Different Database, same server&#8221;. This setting allows intellisense to work.<\/li>\n<li>Suppress errors &#8211; this is just something in case you cause errors in you .dacpac<\/li>\n<\/ol>\n<h4>3. Create a view<\/h4>\n<p>Create a view and you will be able to do:<\/p>\n<p>select<br \/>\n     *<br \/>\nfrom [$(RemoteDB)].dbo.tblTest<\/p>\n<p>You should get full intellisense on your schema as compiled in you .dacpac file from step 1.<\/p>\n<p><a href=\"https:\/\/ntsblog.homedev.com.au\/wp-content\/uploads\/2013\/03\/Intellisense.png\"><img loading=\"lazy\" decoding=\"async\" data-attachment-id=\"958\" data-permalink=\"https:\/\/ntsblog.homedev.com.au\/index.php\/2013\/03\/13\/sqlcmd-variables-linked-server-connection-intellisense\/intellisense\/\" data-orig-file=\"https:\/\/ntsblog.homedev.com.au\/wp-content\/uploads\/2013\/03\/Intellisense.png\" data-orig-size=\"530,165\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;}\" data-image-title=\"Intellisense\" data-image-description=\"&lt;p&gt;SQLCMD Variables and still get intellisense&lt;\/p&gt;\n\" data-image-caption=\"\" data-medium-file=\"https:\/\/ntsblog.homedev.com.au\/wp-content\/uploads\/2013\/03\/Intellisense-300x93.png\" data-large-file=\"https:\/\/ntsblog.homedev.com.au\/wp-content\/uploads\/2013\/03\/Intellisense.png\" src=\"https:\/\/ntsblog.homedev.com.au\/wp-content\/uploads\/2013\/03\/Intellisense.png\" alt=\"Intellisense\" width=\"530\" height=\"165\" class=\"aligncenter size-full wp-image-958\" srcset=\"https:\/\/ntsblog.homedev.com.au\/wp-content\/uploads\/2013\/03\/Intellisense.png 530w, https:\/\/ntsblog.homedev.com.au\/wp-content\/uploads\/2013\/03\/Intellisense-300x93.png 300w\" sizes=\"auto, (max-width: 530px) 100vw, 530px\" \/><\/a><\/p>\n<h4>4. Setup your SQLCMD Value<\/h4>\n<p>Our replacement variable value is the two-part name for the LinkedServer].[RemoteDB<br \/>\n<strong>NOTE: Leave the leading and trailing values off as the outer braces will be added from your sql script as you have to enter <em>[<\/em>$(RemotedDB)<em>]<\/em><\/strong><br \/>\nThis way when the project is generated your solution will generate the correct 4 part name to your linked server objects<\/p>\n<p><a href=\"https:\/\/ntsblog.homedev.com.au\/wp-content\/uploads\/2013\/03\/SQLCmdVars.png\"><img loading=\"lazy\" decoding=\"async\" data-attachment-id=\"972\" data-permalink=\"https:\/\/ntsblog.homedev.com.au\/index.php\/2013\/03\/13\/sqlcmd-variables-linked-server-connection-intellisense\/sqlcmdvars\/\" data-orig-file=\"https:\/\/ntsblog.homedev.com.au\/wp-content\/uploads\/2013\/03\/SQLCmdVars.png\" data-orig-size=\"598,89\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;}\" data-image-title=\"SQLCmdVars\" data-image-description=\"\" data-image-caption=\"\" data-medium-file=\"https:\/\/ntsblog.homedev.com.au\/wp-content\/uploads\/2013\/03\/SQLCmdVars-300x44.png\" data-large-file=\"https:\/\/ntsblog.homedev.com.au\/wp-content\/uploads\/2013\/03\/SQLCmdVars.png\" src=\"https:\/\/ntsblog.homedev.com.au\/wp-content\/uploads\/2013\/03\/SQLCmdVars.png\" alt=\"SQLCMD Variables\" width=\"598\" height=\"89\" class=\"aligncenter size-full wp-image-972\" srcset=\"https:\/\/ntsblog.homedev.com.au\/wp-content\/uploads\/2013\/03\/SQLCmdVars.png 598w, https:\/\/ntsblog.homedev.com.au\/wp-content\/uploads\/2013\/03\/SQLCmdVars-300x44.png 300w\" sizes=\"auto, (max-width: 598px) 100vw, 598px\" \/><\/a><\/p>\n<h4>5. Publish<\/h4>\n<p>Publish the solution:<\/p>\n<ol>\n<li>Choose Publish<\/li>\n<li>Click &#8220;Load Variables&#8221; to load in the values set in step 4.<\/li>\n<li>Choose the database to deploy to<\/li>\n<li>Click &#8220;Create Profile&#8221;, to save your publishing profile<\/li>\n<li>Click &#8220;Generate Script&#8221;<\/li>\n<li>Review the Generated Script and you will note that for some reason the single ] has been changed to ]], remove the extra ]<\/li>\n<li>Right-Click on the window and choose &#8220;Excecute&#8221; to run the script<\/li>\n<\/ol>\n<p>Because of the double brace ]] issue, (which I have raise with Microsoft on <a href=\"http:\/\/connect.microsoft.com\/VisualStudio\/feedback\/details\/781268\/ssdt-and-sqlcmd-variables-convert-a-single-to-a#tabs\" title=\"Microsoft Connect\" target=\"_blank\">Microsoft Connect<\/a>  ), you would be able to just click Publish.<\/p>\n<p>The value in the SQLCMD Variable will be replaced into the script when deployed, and your table references will become 4-Part  table names with [LinkedServer].[RemoteDB].[Schema].[Table] as the output.<\/p>\n<p>Please Download a working sample of the solution.<\/p>\n<h3>\n<a href=\"https:\/\/ntsblog.homedev.com.au\/wp-content\/uploads\/2013\/03\/DatabaseProject.7z\">Download<\/a><br \/>\n<\/h3>\n<p>Let me know if this works for you.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The Problem For a long time I have struggled with how to correctly setup a SQL Server Database project. The main sticking point is to [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[13],"tags":[],"class_list":["post-955","post","type-post","status-publish","format-standard","hentry","category-sql-server"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/ntsblog.homedev.com.au\/index.php\/wp-json\/wp\/v2\/posts\/955","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ntsblog.homedev.com.au\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ntsblog.homedev.com.au\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ntsblog.homedev.com.au\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/ntsblog.homedev.com.au\/index.php\/wp-json\/wp\/v2\/comments?post=955"}],"version-history":[{"count":0,"href":"https:\/\/ntsblog.homedev.com.au\/index.php\/wp-json\/wp\/v2\/posts\/955\/revisions"}],"wp:attachment":[{"href":"https:\/\/ntsblog.homedev.com.au\/index.php\/wp-json\/wp\/v2\/media?parent=955"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ntsblog.homedev.com.au\/index.php\/wp-json\/wp\/v2\/categories?post=955"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ntsblog.homedev.com.au\/index.php\/wp-json\/wp\/v2\/tags?post=955"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}