Over a million developers have joined DZone.

Elastic Query in Azure SQL Database and Views

Creating views that join across databases? Read this article to find out how to make those views play nice with Elastic Query.

· Database Zone

Build fast, scale big with MongoDB Atlas, a hosted service for the leading NoSQL database. Try it now! Brought to you in partnership with MongoDB.

The question came up: How do the constructs necessary for Elastic Query within Azure SQL Database affect your ability to create views that join across databases? Since I haven’t tested this myself, and I haven’t seen anyone else doing anything with it yet, I decided to set up a test. I recreated my cross database queries using the scripts posted here. Let’s create a view:

CREATE VIEW dbo.JoinedView
SELECT dt.Val,
 dt2.Val AS Val2
FROM dbo.DB1Table AS dt
LEFT JOIN dbo.DB2Table AS dt2
 ON dt2.ID = dt.ID;

If I run the query, I get back results. Done. We can create views that join between Azure SQL Databases… But, views are all about masking right? What if I wanted to change the name of the table in my database. Could I do something like this?

ID int, 
Val varchar(50)) 

The CREATE script runs just fine with no errors. When I try to query it, though:

Msg 46823, Level 16, State 2, Line 55
 Error retrieving data from one or more shards.  The underlying error message received was: ‘Invalid object name ‘dbo.CanICheatThisName’.’.

Understanding how the Elastic Query works, of course this fails. So… what about faking it the other way? I’ll create a view in my second database:

CREATE VIEW dbo.FakeTable
FROM dbo.DB2Table AS dt;

Now, I’ll create the EXTERNAL TABLE using the name of the view:

ID int, 
Val varchar(50)) 

When I query this, the data comes across just fine.


Creating a view, or any other query, that joins across databases using Elastic Query works just fine. However, if you want to mask things using a view, you might need to get a little creative in how you implement Elastic Query. The good news is, Elastic Query is somewhat, shall we say, elastic in how you set it up. More so than it immediately appears.

Now it's easier than ever to get started with MongoDB, the database that allows startups and enterprises alike to rapidly build planet-scale apps. Introducing MongoDB Atlas, the official hosted service for the database on AWS. Try it now! Brought to you in partnership with MongoDB.


Published at DZone with permission of Grant Fritchey, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}