SQL SERVER – Generate Live Currency Exchange Rates using dbForge Data Generator for SQL Server

SQL
No Comments

When you test an application that implements billing features, meaningful test data is extremely important. This article teaches how to quickly obtain and populate your database with live currency exchange rates in a few simple steps. Let us learn about Generate Live Currency Exchange Rates.

Before diving into the topic in more detail, let’s drop a line about the meaning of the term currency exchange rate and why it is so important in various aspects of our life.

Currency exchange rate is a reference value used for converting money from one currency to another. In other words, the exchange rate is the price for which one national currency can be exchanged for another national currency.

Whether you are a businessman or the owner of a multi-national corporation, currency exchange rates either directly or indirectly affect your business activities for sure. Currency rates can vary quite dramatically even within one day, so having an up-to-minute information on currency values can save you a good deal of money and help you mitigate risks.

Suppose you need to test a corporate database and you need to populate one of the tables with today’s currency exchange rates. This would seem a challenging task if you were to retrieve data from some source and enter them into the table by hand.

Data generation tool

Solarwinds

Luckily, there exists a tool that can provide real-life test data in a few seconds – dbForge Data Generator for SQL Server. This useful sql generator tool includes a great number of built-in data generators that will help you generate meaningful test data of diverse types and for various purposes.

Let’s use the AdventureWorks database to show how real currency exchange rates can be generated by dbForge Data Generator for SQL Server.

There is the Sales.CurrencyRate table in the AdventureWorks database that has the following columns:

  • Average Rate — an average exchange rate on a specific day.
  • EndOfDayRate — a final exchange rate on a specific day.

After you launch dbForge Data Generator for SQL Server and connect to the AdventureWorks database, select the Sales.CurrencyRate table in the left-hand pane and you will see a preview of the data to be generated by default.

Of course, you can adjust generation settings to obtain data that meet your specific needs, but the resulting data in the AverageRate column still do not resemble the real currency rates, and more importantly they are not live and real.

SQL SERVER - Generate Live Currency Exchange Rates using dbForge Data Generator for SQL Server datagen1

Select the right generator

This problem can be easily solved by applying Python generator available in dbForge Data Generator for SQL Server. The Python generator allows you to create your own scripts to generate any custom data you need.

JSON API is another trick we will use to get real-time currency rates for our AverageRate column. There are plenty of such solutions within the boundlessness of the internet. Let’s use fixer.io in our example.

So, instead of the predefined generator automatically assigned for the AverageRate column, select the Python generator and insert the following Phyton script into the text box:

import clr
clr.AddReference("System")
import urllib, json
from urllib2 import urlopen
from System import DateTime

def main(config):
    dtStr = str(CurrencyRateDate)
    dt = DateTime.Parse(dtStr)
    year = dt.Year
    month = dt.Month  
    day = dt.Day
    n1 = str(FromCurrencyCode)
    n2 = str(ToCurrencyCode)

    if not n1 or not n2:
       return "N/A"

    url = "http://api.fixer.io/"+ str(year) +"-"+ str(str(month).zfill(2)) +"-"+ str(str(day).zfill(2))+ "?base="+str(n1)+ "&symbols="+ n1+","+n2

    response = urllib.urlopen(url)
    data = json.read(response.read())

    if not data.has_key("rates"):
       return "N/A"

    return round(data["rates"][n2],2)

Do the same with the EndOfDayRate column. The Python script, in this case, is as follows:

import clr
clr.AddReference("System")
import urllib, json
from urllib2 import urlopen
from System import DateTime

def main(config):
    dtStr = str(CurrencyRateDate)
    dt = DateTime.Parse(dtStr)
    year = dt.Year
    month = dt.Month  
    day = dt.Day
    n1 = str(FromCurrencyCode)
    n2 = str(ToCurrencyCode)

    if not n1 or not n2:
       return "N/A"

    url = "http://api.fixer.io/"+ str(year) +"-"+ str(str(month).zfill(2)) +"-"+ str(str(day).zfill(2))+ "?base="+str(n1)+ "&symbols="+ n1+","+n2

    response = urllib.urlopen(url)
    data = json.read(response.read())

    if not data.has_key("rates"):
       return "N/A"

    return data["rates"][n2]

As easy as pie. Notice that the values in the AverageRate column are exactly what we wanted them to be.

SQL SERVER - Generate Live Currency Exchange Rates using dbForge Data Generator for SQL Server datagen2

Summary

As is evident from the foregoing, dbForge Data Generator for SQL Server opens great capabilities when it comes to generating large amounts of meaningful realistically looking data for various types of databases.

Reference: Pinal Dave (https://blog.sqlauthority.com)

Solarwinds
Previous Post
SQL SERVER – Beautify SQL Code with dbForge SQL Complete
Next Post
SQL SERVER – Script level upgrade for database ‘master’ failed – There is already an object named ‘DatabaseMailUserRole’ in the database

Related Posts

Leave a Reply

Menu