In this quick post, I will once again demonstrate the use of OLE Automation procedures to consume information on the web and bring it to our SQL Server database so that we can work with this information as needed. If you want to use the Bemean API, which returns data in JSON format, see more in the post. SQL Server 2016 - How to Query Zip Code Information Using the Bemean API and JSON_VALUE Function
This time, I'll show you how to query city, state, neighborhood, add-on, and IBGE code information from an informed zip code.
Querying CEP Data
To perform this operation, I will use the great service of the page viacep.com.br to see the zip code information.
I will filter the input zip code so that it contains only numbers. After that, we make a GET request on the viacep URL and then handle the return via XQuery.
CREATE PROCEDURE dbo.stpConsulta_CEP (
-- Recupera apenas os números do CEP
DECLARE @startingIndex INT = 0
SET @startingIndex = PATINDEX('%[^0-9]%', @Nr_CEP)
IF (@startingIndex <> 0)
SET @Nr_CEP = REPLACE(@Nr_CEP, SUBSTRING(@Nr_CEP, @startingIndex, 1), '')
SET @Url = 'http://viacep.com.br/ws/' + @Nr_CEP + '/xml'
EXEC sys.sp_OACreate 'MSXML2.ServerXMLHTTP', @obj OUT
EXEC sys.sp_OAMethod @obj, 'open', NULL, 'GET', @Url, FALSE
EXEC sys.sp_OAMethod @obj, 'send'
EXEC sys.sp_OAGetProperty @obj, 'responseText', @resposta OUT
EXEC sys.sp_OADestroy @obj
SET @xml = @resposta COLLATE SQL_Latin1_General_CP1251_CS_AS
@xml.value('(/xmlcep/cep)', 'varchar(9)') AS CEP,
@xml.value('(/xmlcep/logradouro)', 'varchar(200)') AS Logradouro,
@xml.value('(/xmlcep/complemento)', 'varchar(200)') AS Complemento,
@xml.value('(/xmlcep/bairro)', 'varchar(200)') AS Bairro,
@xml.value('(/xmlcep/localidade)', 'varchar(200)') AS Cidade,
@xml.value('(/xmlcep/uf)', 'varchar(200)') AS UF,
@xml.value('(/xmlcep/ibge)', 'varchar(200)') AS IBGE
After we create the function, we will use it to test the result:
Thank you and see you next time!
Hello Dirceu, how are you?
I replicated this procedure in my environment and the result is returning empty for any query I make. I know that the post is a little old, but the API continues to work (on the web, queries return address data correctly). Can you validate that for you the procedure still works or also returns NULL as an answer? So I can be sure that the problem is only happening to me.
Is there any performance impact if I want to consume an API, if I use my API to validate address data by zip code, does the bank behave well? Can I work with excessive consumption to validate something via API?
Thanks for the visit!
It has performance impact, yes. Each API query consumes an average of about 480ms. Additionally, Google, for example, has an IP limitation of 1.000 API queries per day. After that, he himself blocks his IP for a certain period of time. For unlimited queries, you must purchase a paid version of the Google API.
I believe that viacep.com.br should have such a limit, as well as most companies that offer this type of service.
A more performative and perhaps inexpensive alternative is to purchase an updated zip code base directly from the Post Office. Instead of making queries via the net, you could simply make a simple select on this purchased post office base.
Thanks for the information, and I already did this process with the post office base, but it is outdated there are new neighborhoods that in new registrations does not fill the fields because it does not find, I need to update the data in the base, today I look for the zip code in the base if I can't find I search the Post Office API and I update my database but there are data that when you issue notes you have an outdated or incomplete address, so is it better to buy buy an update, and how about the google paid API how does it go? where there is information, I thought I only had free.
Congratulations for the excellent post. Much obliged
Igo, tested here and for me is working normally. Which zip code did you use that is giving error?
webservice is not returning values, it has HTTP 502 error