How to add multiple IPS in Azure SQL

Nathan Uchôa
1 min readJun 18, 2022

This article aims to insert multiple addresses into the sql firewall
This idea came when I needed to add a lot of Grafana Cloud IPS (https://grafana.com/api/hosted-grafana/source-ips)
to my firewall. The idea is basically to create dozens of rules and a sequential description in each one, let’s go!

First, let’s create a temporary table to store the addresses we are going to add, using master database:

Create Table #Teste (IP varchar(100))

Insert into #Teste values (‘35.232.52.64’)
Insert into #Teste values (‘35.239.61.132’)
Insert into #Teste values (‘104.154.179.160’)
Insert into #Teste values (‘34.134.222.119’)
Insert into #Teste values (‘104.154.18.168’)
Insert into #Teste values (‘35.188.111.61’)
Insert into #Teste values (‘34.121.8.98’)
Insert into #Teste values (‘35.224.152.225’)
Insert into #Teste values (‘34.71.21.236’)

After adding the addresses, let's create the query:

Declare @IP Varchar(50)
Declare @Descricao nvarchar(128)
Declare @Count int
Set @Count = 1

DECLARE IP_CURSOR CURSOR FOR

Select IP From #Teste

OPEN IP_Cursor

FETCH NEXT FROM IP_Cursor Into @IP
WHILE @@FETCH_STATUS = 0
BEGIN

Set @Descricao = ‘Grafana_’ + CONVERT(varchar(3),@Count)

exec sp_set_firewall_rule @Descricao, @IP, @IP

Set @Count = @Count + 1

FETCH NEXT FROM IP_Cursor Into @IP
END
CLOSE IP_Cursor
DEALLOCATE IP_Cursor

--

--