Home
%3CLINGO-SUB%20id%3D%22lingo-sub-751293%22%20slang%3D%22en-US%22%3ELesson%20Learned%20%23102%3A%20External%20Data%20Source%20and%20Allow%20Azure%20Service%20setting%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-751293%22%20slang%3D%22en-US%22%3E%3CP%3EAs%20you%20know%20when%20you%20are%20using%20cross%20database%20queries%20or%20external%20tables%20and%20you%20execute%20a%20query%20the%20connection%20will%20be%20stablished%20from%20the%20database%20server%20that%20initiates%20the%20execution.%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThe%20IP%20of%20this%20database%20server%20may%20be%20different%20because%20this%20is%20dynamic%20and%20not%20fixed.%20For%20this%20reason%2C%20one%20of%20the%20requirements%20is%20to%20enable%20Allow%20Azure%20Service.%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EAs%20a%20workaround%20if%20you%20want%20to%20have%20the%20status%20OFF%20to%20Allow%20Azure%20Service%2C%20I%20would%20like%20to%20suggest%20to%20use%20the%20following%20script%20that%20before%20to%20execute%20a%20query%20using%20an%20external%20table%2Fcross%20database%20query%20you%20could%20call%20the%20stored%20procedure%20that%20needs%20the%20parameters%20of%20client%20IP%20and%20the%20name%20of%20the%20rule.%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ERun%20this%20stored%20procedure%20in%20the%20server%2Fdatabase%20that%20contains%20the%20data.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%3ECREATE%20PROCEDURE%20bValidIP(%40sIP%20as%20varchar(50)%2C%20%40NameRule%20as%20Nvarchar(20))%0AAS%0A%20DECLARE%20%40start_ip_address%20AS%20VARCHAR(50)%20%3D%20''%0A%20DECLARE%20%40AddNewRule%20AS%20INTEGER%20%3D%201%0A%20SET%20%40start_ip_address%20%3D%20ISNULL(%20(%20SELECT%20TOP%201%20start_ip_address%20FROM%20sys.database_firewall_rules%20WHERE%20Name%20%3D%20%40NameRule%20)%20%2C'')%0A%20IF%20%40start_ip_address%20%26lt%3B%26gt%3B%20%40sIP%20%20%0A%20%20%20EXECUTE%20sp_delete_database_firewall_rule%20%40NameRule%0A%20ELSE%0A%20%20SET%20%40AddNewRule%3D0%0A%0A%20IF%20%40AddNewRule%3D1%20%0A%20BEGIN%0A%20%20%20EXECUTE%20sp_set_database_firewall_rule%20%40NameRule%2C%20%40sIP%2C%20%40sIP%3B%20%20%0A%20END%3C%2FPRE%3E%0A%3CP%3EAs%20we%20mentioned%2C%20just%20only%20execute%20this%20stored%20procedure%20in%20the%20server%2Fdatabase%20that%20contains%20the%20data%20before%20execute%20any%20query%20the%20cross%20database%20query%2Fexternal%20table.%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%3EEXECUTE%20bValidIP%20'10.10.10.10'%2C'VALUE1'%3C%2FPRE%3E%0A%3CP%3EEnjoy!%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-TEASER%20id%3D%22lingo-teaser-751293%22%20slang%3D%22en-US%22%3E%3CP%3EAs%20you%20know%20when%20you%20are%20using%20cross%20database%20queries%20or%20external%20tables%20and%20you%20execute%20a%20query%20the%20connection%20will%20be%20stablished%20from%20the%20database%20server%20that%20initiates%20the%20execution.%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThe%20IP%20of%20this%20server%20may%20be%20different%20because%20this%20is%20dynamic%20and%20not%20fixed.%20For%20this%20reason%2C%20one%20of%20the%20requiriments%20is%20to%20enable%20Allow%20Azure%20Service.%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-TEASER%3E

As you know when you are using cross database queries or external tables and you execute a query the connection will be stablished from the database server that initiates the execution. 

The IP of this database server may be different because this is dynamic and not fixed. For this reason, one of the requirements is to enable Allow Azure Service. 

 

As a workaround if you want to have the status OFF to Allow Azure Service, I would like to suggest to use the following script that before to execute a query using an external table/cross database query you could call the stored procedure that needs the parameters of client IP and the name of the rule. 

 

Run this stored procedure in the server/database that contains the data.

 

CREATE PROCEDURE bValidIP(@sIP as varchar(50), @NameRule as Nvarchar(20))
AS
 DECLARE @start_ip_address AS VARCHAR(50) = ''
 DECLARE @AddNewRule AS INTEGER = 1
 SET @start_ip_address = ISNULL( ( SELECT TOP 1 start_ip_address FROM sys.database_firewall_rules WHERE Name = @NameRule ) ,'')
 IF @start_ip_address <> @sIP  
   EXECUTE sp_delete_database_firewall_rule @NameRule
 ELSE
  SET @AddNewRule=0

 IF @AddNewRule=1 
 BEGIN
   EXECUTE sp_set_database_firewall_rule @NameRule, @sIP, @sIP;  
 END

As we mentioned, just only execute this stored procedure in the server/database that contains the data before execute any query the cross database query/external table. 

 

EXECUTE bValidIP '10.10.10.10','VALUE1'

Enjoy!