Sample code for 30+ languages & platforms
SQL Server

ipstack.com IPv4 Geolocation Lookup

See more Geolocation Examples

Demonstrates how to lookup Geolocation data for an IPv4 address using the ipstack.com REST API.

Chilkat SQL Server Downloads

SQL Server
-- Important: See this note about string length limitations for strings returned by sp_OAMethod calls.
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
    DECLARE @hr int
    DECLARE @iTmp0 int
    -- Important: Do not use nvarchar(max).  See the warning about using nvarchar(max).
    DECLARE @sTmp0 nvarchar(4000)
    DECLARE @success int
    SELECT @success = 0

    -- This example requires the Chilkat API to have been previously unlocked.
    -- See Global Unlock Sample for sample code.

    DECLARE @http int
    EXEC @hr = sp_OACreate 'Chilkat.Http', @http OUT
    IF @hr <> 0
    BEGIN
        PRINT 'Failed to create ActiveX component'
        RETURN
    END

    -- Lookup an IPv4 address: 149.250.207.170  (this was a randomly chosen address)
    DECLARE @jsonStr nvarchar(4000)
    EXEC sp_OAMethod @http, 'QuickGetStr', @jsonStr OUT, 'http://api.ipstack.com/149.250.207.170?access_key=YOUR_ACCESS_KEY'
    EXEC sp_OAGetProperty @http, 'LastMethodSuccess', @iTmp0 OUT
    IF @iTmp0 = 0
      BEGIN
        EXEC sp_OAGetProperty @http, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @http
        RETURN
      END

    DECLARE @json int
    EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @json OUT

    EXEC sp_OASetProperty @json, 'EmitCompact', 0
    EXEC sp_OAMethod @json, 'Load', @success OUT, @jsonStr

    EXEC sp_OAMethod @json, 'Emit', @sTmp0 OUT
    PRINT @sTmp0

    -- Sample output:
    -- Use this online tool to generate parsing code from sample JSON: 
    -- Generate Parsing Code from JSON

    -- {
    --   "ip": "149.250.207.170",
    --   "type": "ipv4",
    --   "continent_code": "EU",
    --   "continent_name": "Europe",
    --   "country_code": "DE",
    --   "country_name": "Germany",
    --   "region_code": null,
    --   "region_name": null,
    --   "city": null,
    --   "zip": null,
    --   "latitude": 51.2993,
    --   "longitude": 9.491,
    --   "location": {
    --     "geoname_id": null,
    --     "capital": "Berlin",
    --     "languages": [
    --       {
    --         "code": "de",
    --         "name": "German",
    --         "native": "Deutsch"
    --       }
    --     ],
    --     "country_flag": "http:\/\/assets.ipstack.com\/flags\/de.svg",
    --     "country_flag_emoji": "\ud83c\udde9\ud83c\uddea",
    --     "country_flag_emoji_unicode": "U+1F1E9 U+1F1EA",
    --     "calling_code": "49",
    --     "is_eu": true
    --   }
    -- }

    DECLARE @ip nvarchar(4000)

    DECLARE @v_type nvarchar(4000)

    DECLARE @continent_code nvarchar(4000)

    DECLARE @continent_name nvarchar(4000)

    DECLARE @country_code nvarchar(4000)

    DECLARE @country_name nvarchar(4000)

    DECLARE @region_code nvarchar(4000)

    DECLARE @region_name nvarchar(4000)

    DECLARE @city nvarchar(4000)

    DECLARE @zip nvarchar(4000)

    DECLARE @latitude nvarchar(4000)

    DECLARE @longitude nvarchar(4000)

    DECLARE @locationGeoname_id nvarchar(4000)

    DECLARE @locationCapital nvarchar(4000)

    DECLARE @locationCountry_flag nvarchar(4000)

    DECLARE @locationCountry_flag_emoji nvarchar(4000)

    DECLARE @locationCountry_flag_emoji_unicode nvarchar(4000)

    DECLARE @locationCalling_code nvarchar(4000)

    DECLARE @locationIs_eu int

    DECLARE @i int

    DECLARE @count_i int

    DECLARE @code nvarchar(4000)

    DECLARE @name nvarchar(4000)

    DECLARE @native nvarchar(4000)

    EXEC sp_OAMethod @json, 'StringOf', @ip OUT, 'ip'
    EXEC sp_OAMethod @json, 'StringOf', @v_type OUT, 'type'
    EXEC sp_OAMethod @json, 'StringOf', @continent_code OUT, 'continent_code'
    EXEC sp_OAMethod @json, 'StringOf', @continent_name OUT, 'continent_name'
    EXEC sp_OAMethod @json, 'StringOf', @country_code OUT, 'country_code'
    EXEC sp_OAMethod @json, 'StringOf', @country_name OUT, 'country_name'
    EXEC sp_OAMethod @json, 'StringOf', @region_code OUT, 'region_code'
    EXEC sp_OAMethod @json, 'StringOf', @region_name OUT, 'region_name'
    EXEC sp_OAMethod @json, 'StringOf', @city OUT, 'city'
    EXEC sp_OAMethod @json, 'StringOf', @zip OUT, 'zip'
    EXEC sp_OAMethod @json, 'StringOf', @latitude OUT, 'latitude'
    EXEC sp_OAMethod @json, 'StringOf', @longitude OUT, 'longitude'
    EXEC sp_OAMethod @json, 'StringOf', @locationGeoname_id OUT, 'location.geoname_id'
    EXEC sp_OAMethod @json, 'StringOf', @locationCapital OUT, 'location.capital'
    EXEC sp_OAMethod @json, 'StringOf', @locationCountry_flag OUT, 'location.country_flag'
    EXEC sp_OAMethod @json, 'StringOf', @locationCountry_flag_emoji OUT, 'location.country_flag_emoji'
    EXEC sp_OAMethod @json, 'StringOf', @locationCountry_flag_emoji_unicode OUT, 'location.country_flag_emoji_unicode'
    EXEC sp_OAMethod @json, 'StringOf', @locationCalling_code OUT, 'location.calling_code'
    EXEC sp_OAMethod @json, 'BoolOf', @locationIs_eu OUT, 'location.is_eu'
    SELECT @i = 0
    EXEC sp_OAMethod @json, 'SizeOfArray', @count_i OUT, 'location.languages'
    WHILE @i < @count_i
      BEGIN
        EXEC sp_OASetProperty @json, 'I', @i
        EXEC sp_OAMethod @json, 'StringOf', @code OUT, 'location.languages[i].code'
        EXEC sp_OAMethod @json, 'StringOf', @name OUT, 'location.languages[i].name'
        EXEC sp_OAMethod @json, 'StringOf', @native OUT, 'location.languages[i].native'
        SELECT @i = @i + 1
      END

    EXEC @hr = sp_OADestroy @http
    EXEC @hr = sp_OADestroy @json


END
GO