Although the program's main purpose is to save data extracted from a DB into a CSV file, the general workflow is as follows:
Package Structure
The program's functionality isn't contained within a single source file. It involves a folder and file structure, as shown below:
Folder/File
Description
ExportCsvOracle.vbs
Execution program.
Config.ini
Configuration information for DB connections and folder paths.
sql (Folder)
Contains files storing the SELECT statements.
csv (Folder)
Stores CSV files extracted from the DB.
Explanation of Source Code
For program execution, configuration information will be loaded from an INI file.
â–¼ Config.ini
The INI file in this case contains the following information:
Connection information to the DB (Oracle)
Folder name (path) for storing SQL statements
Folder name (path) for storing generated CSV files
This information from the INI file is read and stored in variables within the VBS script for later use. For more details on this process, you can refer to a separate article: VBS | Getting Data from an INI File
Connecting to the Oracle DB
In this section, we prepare functions necessary for various operations with the Oracle DB.
In this context, we will only be using the DB connection and disconnection functions.
You can utilize the variables read from the earlier INI file to perform DB operations like this:
Executing SQL Statements
Now that we have established a connection to the Oracle DB, let's proceed to explain how to execute SQL statements and retrieve record sets. The workflow for this section is as follows:
Retrieve SQL files from the SQL storage folder ※ One SELECT statement per SQL file ↓ Extract SQL statements from SQL files ↓ Execute SQL statements and retrieve record sets
First, we will prepare functions needed for each step:
With these functions, you can proceed to execute SQL statements and retrieve record sets. Though I'll skip detailed explanations, the record set will contain information from the extraction result specified in the SQL SELECT statement.
You can call these functions within your main program code:
This completes the creation of CSV files.
Saving Executed SQL Results to CSV Files
Until now, the SQL results have been stored in a record set. However, to save the data in CSV format, you need to convert the record set into a text format compatible with CSV.
The workflow here is as follows
:
①Convert the record set to CSV (text) ↓ ② Write the converted text to a CSV file
You can incorporate these functions into your main program by adding the following code:
This concludes the explanation. Using this sample as a base, you can easily implement tasks like extracting the latest logs from a table in the database every hour. For automated repetitive execution, consider using the Task Scheduler on Windows.