Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Elastic Query in Azure SQL Database and Views

DZone's Guide to

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
Free Resource

Find out how Database DevOps helps your team deliver value quicker while keeping your data safe and your organization compliant. Align DevOps for your applications with DevOps for your SQL Server databases to discover the advantages of true Database DevOps, brought to you in partnership with Redgate

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
AS
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?

CREATE EXTERNAL TABLE dbo.CanICheatThisName (
ID int, 
Val varchar(50)) 
WITH (
DATA_SOURCE = DB2Access);

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
AS
SELECT *
FROM dbo.DB2Table AS dt;

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

CREATE EXTERNAL TABLE dbo.FakeTable (
ID int, 
Val varchar(50)) 
WITH (
DATA_SOURCE = DB2Access);

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

Conclusion

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.

Align DevOps for your applications with DevOps for your SQL Server databases to increase speed of delivery and keep data safe. Discover true Database DevOps, brought to you in partnership with Redgate

Topics:
azure ,query ,sql ,database ,views ,microsoft ,elastic

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

Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}